forked from pstage-ocr-team6/ocr-teamcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flags.py
48 lines (34 loc) · 878 Bytes
/
flags.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
41
42
43
44
45
46
47
48
"""
Original code from clovaai/SATRN
"""
import os
import yaml
import collections
def dict_to_namedtuple(d):
""" Convert dictionary to named tuple.
"""
FLAGSTuple = collections.namedtuple('FLAGS', sorted(d.keys()))
for k, v in d.items():
if k == 'prefix':
v = os.path.join('./', v)
if type(v) is dict:
d[k] = dict_to_namedtuple(v)
elif type(v) is str:
try:
d[k] = eval(v)
except:
d[k] = v
nt = FLAGSTuple(**d)
return nt
class Flags:
""" Flags object.
"""
def __init__(self, config_file):
try:
with open(config_file, 'r') as f:
d = yaml.safe_load(f)
except:
d = config_file
self.flags = dict_to_namedtuple(d)
def get(self):
return self.flags