-
Notifications
You must be signed in to change notification settings - Fork 19
/
parser.py
40 lines (28 loc) · 936 Bytes
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
USABLE_TYPES = set([float, int])
def trim_preceding_hyphens(st):
i = 0
while st[i] == "-":
i += 1
return st[i:]
def arg_to_varname(st: str):
st = trim_preceding_hyphens(st)
st = st.replace("-", "_")
return st.split("=")[0]
def argv_to_vars(argv):
var_names = []
for arg in argv:
if arg.startswith("-") and arg_to_varname(arg) != "config":
var_names.append(arg_to_varname(arg))
return var_names
def produce_override_string(args, override_args):
lines = []
for v in override_args:
if v != "multigpu":
v_arg = getattr(args, v)
if type(v_arg) in USABLE_TYPES:
lines.append(v + ": " + str(v_arg))
else:
lines.append(v + ": " + f'"{str(v_arg)}"')
else:
lines.append("multigpu: " + str(args.multigpu))
return "\n# ===== Overrided ===== #\n" + "\n".join(lines)