autograder.util.parse

 1import typing
 2
 3def string_list(raw_value: typing.Any, delim: str = ', ') -> typing.List[str]:
 4    """ Parse a list of strings. """
 5
 6    value = raw_value
 7
 8    if (value is None):
 9        return []
10
11    if (isinstance(value, (list, tuple))):
12        return [str(item) for item in value]
13
14    value = str(value).strip()
15
16    if (len(value) == 0):
17        return []
18
19    if (len(value) == 1):
20        raise ValueError(f"Cannot parse a list from this string, it is too short: '{raw_value}'.")
21
22    # Strip opening and closing brackets.
23    value = value[1:-1]
24
25    if (len(value) == 0):
26        return []
27
28    items = []
29    for item in value.split(delim):
30        # Strip quotes.
31        items.append(item[1:-1])
32
33    return items
def string_list(raw_value: Any, delim: str = ', ') -> List[str]:
 4def string_list(raw_value: typing.Any, delim: str = ', ') -> typing.List[str]:
 5    """ Parse a list of strings. """
 6
 7    value = raw_value
 8
 9    if (value is None):
10        return []
11
12    if (isinstance(value, (list, tuple))):
13        return [str(item) for item in value]
14
15    value = str(value).strip()
16
17    if (len(value) == 0):
18        return []
19
20    if (len(value) == 1):
21        raise ValueError(f"Cannot parse a list from this string, it is too short: '{raw_value}'.")
22
23    # Strip opening and closing brackets.
24    value = value[1:-1]
25
26    if (len(value) == 0):
27        return []
28
29    items = []
30    for item in value.split(delim):
31        # Strip quotes.
32        items.append(item[1:-1])
33
34    return items

Parse a list of strings.