forked from zhiwehu/Python-programming-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparse_tutorial.py
More file actions
38 lines (30 loc) · 1.06 KB
/
argparse_tutorial.py
File metadata and controls
38 lines (30 loc) · 1.06 KB
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
import argparse
def attr_flag(s):
"""
Parse attributes parameters.
"""
if s == "*":
return s
attr = s.split(',')
assert len(attr) == len(set(attr))
attributes = []
for x in attr:
if '.' not in x:
attributes.append((x, 2))
else:
split = x.split('.')
print(split)
assert len(split) == 2 and len(split[0]) > 0
assert split[1].isdigit() and int(split[1]) >= 2
attributes.append((split[0], int(split[1])))
return sorted(attributes, key=lambda x: (x[1], x[0]))
parser = argparse.ArgumentParser(description='Simple tutorial to understand arg types')
parser.add_argument("--attr", type=attr_flag, default="Gender,Daylight,CSK.3",
help="Attributes to classify")
parser.add_argument("--img_size", type=int, default=256,
help="Image sizes (images have to be squared)")
params = parser.parse_args()
#convert from argparse.Namespace to dict
dict_params = vars(params)
for k, v in dict_params.items():
print(k, v)