-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_util.py
42 lines (33 loc) · 1.24 KB
/
config_util.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
import collections
from copy import deepcopy
import yaml
def config_copy(config):
if isinstance(config, dict):
return {k: config_copy(v) for k, v in config.items()}
elif isinstance(config, list):
return [config_copy(v) for v in config]
else:
return deepcopy(config)
def recursive_dict_update(d, u):
for k, v in u.items():
if isinstance(v, collections.Mapping):
d[k] = recursive_dict_update(d.get(k, {}), v)
else:
d[k] = v
return d
def get_config(algorithm):
config_dir = '{0}/{1}'
config_dir2 = '{0}/{1}/{2}'
with open(config_dir.format('config', "{}.yaml".format('default')), "r") as f:
try:
default_config = yaml.load(f)
except yaml.YAMLError as exc:
assert False, "default.yaml error: {}".format(exc)
with open(config_dir2.format('config', 'algs', "{}.yaml".format(algorithm)), "r") as f:
try:
config_dict2 = yaml.load(f)
except yaml.YAMLError as exc:
assert False, "{}.yaml error: {}".format('sc2', exc)
alg_config = config_dict2
final_config_dict = recursive_dict_update(default_config, alg_config)
return final_config_dict