|
1 | | -def ati(array): |
2 | | - """ati(array) -> list |
3 | | - Convert all the elements in the array and return them in a list. |
4 | | - """ |
| 1 | +"""Some utility functions.""" |
| 2 | +import sys |
| 3 | +import random |
| 4 | +from typing import cast, Any, Dict, Iterable, Tuple, Union |
| 5 | + |
| 6 | +__all__ = [ |
| 7 | + "ati", "list_like", "int_like", "strtolines", "make_unicode", |
| 8 | + "unpack_kwargs", "process_args" |
| 9 | +] |
| 10 | + |
| 11 | + |
| 12 | +def ati(array: Iterable[Any]): |
| 13 | + """Convert all the elements in the array and return them in a list.""" |
5 | 14 | return [int(i) for i in array] |
6 | 15 |
|
7 | 16 |
|
8 | | -def list_like(data): |
9 | | - """list_like(data) -> bool |
10 | | - Judge whether the object data is like a list or a tuple. |
11 | | - object data -> the data to judge |
12 | | - """ |
| 17 | +def list_like(data: Any): |
| 18 | + """Judge whether the object data is like a list or a tuple.""" |
13 | 19 | return isinstance(data, (tuple, list)) |
14 | 20 |
|
15 | 21 |
|
16 | | -def int_like(data): |
17 | | - isint = False |
18 | | - try: |
19 | | - isint = isint or isinstance(data, long) |
20 | | - except NameError: |
21 | | - pass |
22 | | - isint = isint or isinstance(data, int) |
23 | | - return isint |
| 22 | +def int_like(data: Any): |
| 23 | + """Judge whether the object data is like a int.""" |
| 24 | + return isinstance(data, int) |
24 | 25 |
|
25 | 26 |
|
26 | | -def strtolines(str): |
27 | | - lines = str.split('\n') |
28 | | - for i in range(len(lines)): |
| 27 | +def strtolines(string: str): |
| 28 | + """ |
| 29 | + Split the string by the newline character, remove trailing spaces from each line, |
| 30 | + and remove any blank lines at the end of the the string. |
| 31 | + """ |
| 32 | + lines = string.split("\n") |
| 33 | + for i, _ in enumerate(lines): |
29 | 34 | lines[i] = lines[i].rstrip() |
30 | 35 |
|
31 | | - while len(lines) > 0 and len(lines[len(lines) - 1]) == 0: |
32 | | - del lines[len(lines) - 1] |
| 36 | + while len(lines) > 0 and len(lines[-1]) == 0: |
| 37 | + lines.pop() |
33 | 38 | return lines |
34 | 39 |
|
35 | 40 |
|
36 | | -def make_unicode(data): |
| 41 | +def make_unicode(data: Any): |
| 42 | + """Convert the data to a string.""" |
37 | 43 | return str(data) |
38 | 44 |
|
39 | 45 |
|
40 | | -def unpack_kwargs(funcname, kwargs, arg_pattern): |
| 46 | +def unpack_kwargs( |
| 47 | + funcname: str, |
| 48 | + kwargs: Dict[str, Any], |
| 49 | + arg_pattern: Iterable[Union[str, Tuple[str, Any]]], |
| 50 | +): |
| 51 | + """Parse the keyword arguments.""" |
41 | 52 | rv = {} |
42 | 53 | kwargs = kwargs.copy() |
43 | 54 | for tp in arg_pattern: |
44 | 55 | if list_like(tp): |
45 | | - k, v = tp |
46 | | - rv[k] = kwargs.get(k, v) |
47 | | - try: |
48 | | - del kwargs[k] |
49 | | - except KeyError: |
50 | | - pass |
| 56 | + k, v = cast(Tuple[str, Any], tp) |
| 57 | + rv[k] = kwargs.pop(k, v) |
51 | 58 | else: |
52 | | - error = False |
| 59 | + tp = cast(str, tp) |
53 | 60 | try: |
54 | | - rv[tp] = kwargs[tp] |
55 | | - del kwargs[tp] |
56 | | - except KeyError as e: |
57 | | - error = True |
58 | | - if error: |
| 61 | + rv[tp] = kwargs.pop(tp) |
| 62 | + except KeyError: |
59 | 63 | raise TypeError( |
60 | | - '{}() missing 1 required keyword-only argument: \'{}\''. |
61 | | - format(funcname, tp)) |
| 64 | + f"{funcname}() missing 1 required keyword-only argument: '{tp}'" |
| 65 | + ) from None |
62 | 66 | if kwargs: |
63 | 67 | raise TypeError( |
64 | | - '{}() got an unexpected keyword argument \'{}\''.format( |
65 | | - funcname, |
66 | | - next(iter(kwargs.items()))[0])) |
| 68 | + f"{funcname}() got an unexpected keyword argument '{next(iter(kwargs.items()))[0]}'" |
| 69 | + ) |
67 | 70 | return rv |
| 71 | + |
| 72 | + |
| 73 | +def process_args(): |
| 74 | + """ |
| 75 | + Process the command line arguments. |
| 76 | + Now we support: |
| 77 | + - randseed: set the random seed |
| 78 | + """ |
| 79 | + for s in sys.argv: |
| 80 | + if s.startswith("--randseed="): |
| 81 | + random.seed(s.split("=")[1]) |
0 commit comments