-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathConfig.py
More file actions
53 lines (47 loc) · 2 KB
/
Config.py
File metadata and controls
53 lines (47 loc) · 2 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import json
from Parameters import ARGS
class Config(dict):
def __init__(self, init_dict=None, ARGS=ARGS, config_file_name=None):
"""
Takes in command-line arguments as well as the configuration file and parses them.
Parameters
-----------------
:param init_dict: <dict>: a dictionary of configuration settings to use
:param ARGS: <key-val>: the command-line arguments to take, structured like a dictionary
:param config_file_name: <str>: relative filepath to config file
"""
if init_dict:
self.load_config(init_dict)
config_file_name = './configs/' + (config_file_name or (ARGS and ARGS.config) or '')
config_file_name = config_file_name.replace('configs/configs', 'configs')
if ARGS:
ARGS.config = ''
if config_file_name != './configs/':
self.load_helper(config_file_name, ARGS)
def load_helper(self, path=None, ARGS=None):
if path is None:
base_config = json.load(open('./configs/default.json', 'r'))
self.load_config(base_config)
else:
config_dict = json.load(open(path, 'r'))
if config_dict['parent_config']:
self.load_helper(('./configs/' + config_dict['parent_config'])
.replace('./configs/configs/', './configs/'))
self.load_config(config_dict, ARGS)
def load_config(self, kv, ARGS=None):
"""
Loads in a configuration from a dictionary
:param kv: <key-val>: a key-val data structure
:return: None
"""
for key in kv:
if key == 'gpu' and ARGS and ARGS.gpu != -1:
self['gpu'] = ARGS
if type(kv[key]) is dict:
if key in self:
self[key].load_config(kv[key])
else:
self[key] = Config(init_dict=kv[key], ARGS=ARGS, config_file_name=None)
else:
self[key] = kv[key]
config = Config()