-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_helper.py
63 lines (49 loc) · 1.69 KB
/
config_helper.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
49
50
51
52
53
54
55
56
57
58
59
60
import configparser
def parse_configuration(config_path):
config_dict = {}
config = configparser.ConfigParser()
config.read(config_path)
for section in config.sections():
config_dict[section] = parse_configuration_section(config, section)
config_dict["handwritten_dataset"]["source_extensions"] = parse_tuple(
config_dict["handwritten_dataset"]["source_extensions"])
config_dict["handwritten_dataset"]["target_extensions"] = parse_tuple(
config_dict["handwritten_dataset"]["target_extensions"])
return config_dict
def parse_configuration_section(config, section):
section_dict = {}
options = config.options(section)
for option in options:
try:
value = config.get(section, option)
int_value = parse_int(value)
float_value = parse_float(value)
if value == 'True':
section_dict[option] = True
elif value == 'False':
section_dict[option] = False
elif float_value is not None:
section_dict[option] = float_value
elif int_value is not None:
section_dict[option] = int_value
else:
section_dict[option] = value
except:
print("exception on %s!" % option)
section_dict[option] = None
return section_dict
def parse_int(s, base=10, value=None):
try:
return int(s, base)
except ValueError:
return value
def parse_float(s, value=None):
if '.' in s:
try:
return float(s)
except ValueError:
pass
return value
def parse_tuple(s):
parts = s.split(",")
return tuple(parts)