-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
53 lines (46 loc) · 1.83 KB
/
utils.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
import logging
import os
import platform
import copy
def deep_update(original, new_dict, new_keys_allowed, whitelist):
"""Updates original dict with values from new_dict recursively.
If new key is introduced in new_dict, then if new_keys_allowed is not
True, an error will be thrown. Further, for sub-dicts, if the key is
in the whitelist, then new subkeys can be introduced.
Args:
original (dict): Dictionary with default values.
new_dict (dict): Dictionary with values to be updated
new_keys_allowed (bool): Whether new keys are allowed.
whitelist (list): List of keys that correspond to dict values
where new subkeys can be introduced. This is only at
the top level.
"""
for k, value in new_dict.items():
if k not in original:
if not new_keys_allowed:
raise Exception("Unknown config parameter `{}` ".format(k))
if isinstance(original.get(k), dict):
if k in whitelist:
deep_update(original[k], value, True, [])
else:
deep_update(original[k], value, new_keys_allowed, [])
else:
original[k] = value
return original
def merge_dicts(d1, d2):
"""Returns a new dict that is d1 and d2 deep merged."""
merged = copy.deepcopy(d1)
deep_update(merged, d2, True, [])
return merged
def setup_logger(name, level=logging.INFO):
log_dir = "logs/"
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
handler = logging.FileHandler(log_dir + name + "-" + platform.node() + '.log')
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
if not logger.handlers:
logger.addHandler(handler)
return logger