autograder.util.load

 1import typing
 2
 3import edq.util.dirent
 4
 5def load_tsv(path: str, max_len: int, min_len: int = 1) -> typing.List[typing.List[str]]:
 6    """
 7    Read a TSV file and return a list of lists of the stripped fields in the TSV file.
 8    Raise an error if a line has more fields than the maximum length
 9    or less fields than the minimum length.
10    """
11
12    rows = []
13
14    with open(path, 'r', encoding = edq.util.dirent.DEFAULT_ENCODING) as file:
15        lineno = 0
16        for line in file:
17            lineno += 1
18
19            line = line.strip()
20
21            if (line == ""):
22                continue
23
24            row = line.split("\t")
25            row = [field.strip() for field in row]
26
27            if (len(row) < min_len):
28                raise ValueError(f"File ('{path}') line ({lineno}) has too few values. Min is {min_len}, found {len(row)}.")
29
30            if (len(row) > max_len):
31                raise ValueError(f"File ('{path}') line ({lineno}) has too many values. Max is {max_len}, found {len(row)}.")
32
33            rows.append(row)
34
35    return rows
def load_tsv(path: str, max_len: int, min_len: int = 1) -> List[List[str]]:
 6def load_tsv(path: str, max_len: int, min_len: int = 1) -> typing.List[typing.List[str]]:
 7    """
 8    Read a TSV file and return a list of lists of the stripped fields in the TSV file.
 9    Raise an error if a line has more fields than the maximum length
10    or less fields than the minimum length.
11    """
12
13    rows = []
14
15    with open(path, 'r', encoding = edq.util.dirent.DEFAULT_ENCODING) as file:
16        lineno = 0
17        for line in file:
18            lineno += 1
19
20            line = line.strip()
21
22            if (line == ""):
23                continue
24
25            row = line.split("\t")
26            row = [field.strip() for field in row]
27
28            if (len(row) < min_len):
29                raise ValueError(f"File ('{path}') line ({lineno}) has too few values. Min is {min_len}, found {len(row)}.")
30
31            if (len(row) > max_len):
32                raise ValueError(f"File ('{path}') line ({lineno}) has too many values. Max is {max_len}, found {len(row)}.")
33
34            rows.append(row)
35
36    return rows

Read a TSV file and return a list of lists of the stripped fields in the TSV file. Raise an error if a line has more fields than the maximum length or less fields than the minimum length.