-
Notifications
You must be signed in to change notification settings - Fork 0
/
configModule.py
124 lines (102 loc) · 3.46 KB
/
configModule.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import yaml
import re
import copy
loader = yaml.FullLoader
loader.add_implicit_resolver(
u'tag:yaml.org,2002:float',
re.compile(u'''^(?:
[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
|\\.[0-9_]+(?:[eE][-+][0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*
|[-+]?\\.(?:inf|Inf|INF)
|\\.(?:nan|NaN|NAN))$''', re.X),
list(u'-+0123456789.'))
class Config(object):
def __init__(self, dict_config=None):
super().__init__()
self.set_attribute(dict_config)
@staticmethod
def from_yaml(path):
with open(path, 'r') as stream:
return Config(yaml.load(stream, Loader=loader))
@staticmethod
def from_dict(dict):
return Config(dict)
@staticmethod
def get_empty():
return Config()
def __getattr__(self, item):
return self.__dict__[item]
def __setattr__(self, key, value):
self.set_attribute({key:value})
def set_attribute(self, dict_config):
if dict_config is None:
return
for key in dict_config.keys():
if isinstance(dict_config[key], dict):
self.__dict__[key] = Config(dict_config[key])
else:
self.__dict__[key] = dict_config[key]
def get(self, key, default, possible_none=True):
"""
same dictionary.get. this method Configruation Not safe!!!
:param key:
:param default:
:param possible_none: if true, None also returned
:return:
"""
out = self.__dict__.get(key, default)
if possible_none is False and out is None:
return default
return out
def keys(self):
return self.__dict__.keys()
def __getitem__(self, key):
return self.__dict__[key]
def __setitem__(self, key, value):
self.__dict__[key] = value
def __delitem__(self, key):
del self.__dict__[key]
def __contains__(self, key):
return key in self.__dict__
def __len__(self):
return len(self.__dict__)
def __repr__(self):
return repr(self.__dict__)
def copy(self):
return Config.from_dict(self.extraction_dictionary(self))
def update(self, dict_config):
"""
Combines the current configuration with the parameter configuration.
This operation performs a deepcopy on the parameter configuration.
Therefore, it is configuration safe.
:param dict_config:
:return:
"""
for key in dict_config.keys():
if key in self.__dict__.keys():
if isinstance(dict_config[key], Config):
self.__dict__[key].update(dict_config[key])
else:
self.__dict__[key] = copy.deepcopy(dict_config[key])
else:
self.__setitem__(key, copy.deepcopy(dict_config[key]))
return self
@classmethod
def extraction_dictionary(cls, config):
out = {}
for key in config.keys():
if isinstance(config[key], Config):
out[key] = cls.extraction_dictionary(config[key])
else:
out[key] = copy.deepcopy(config[key])
return out
def __deepcopy__(self, memo):
cls = self.__class__
result = cls.__new__(cls)
memo[id(self)] = result
for k, v in self.__dict__.items():
setattr(result, k, copy.deepcopy(v, memo))
return result