@@ -8,13 +8,10 @@ class ConfigurationError(Exception):
8
8
9
9
10
10
class Config :
11
- """Base configuration class.
11
+ """Base class for Standardizer configuration .
12
12
13
- Config classes that inherit from this class define configuration as their
14
- class attributes. Particular attributes can be overriden on an per-instance
15
- basis by providing a config overrides at initialization time.
16
-
17
- Configs inheriting from this config support basic dictionary operations.
13
+ Not all standardizers will (can) use the same parameters so refer to their
14
+ respective documentation for a more complete list.
18
15
19
16
Parameters
20
17
----------
@@ -24,66 +21,33 @@ class attributes. Particular attributes can be overriden on an per-instance
24
21
Keyword arguments, assigned as configuration key-values.
25
22
"""
26
23
27
- def __init__ (self , config = None , method = "default" , ** kwargs ):
24
+ def __init__ (self , config = None , ** kwargs ):
28
25
# This is a bit hacky, but it makes life a lot easier because it
29
26
# enables automatic loading of the default configuration and separation
30
27
# of default config from instance bound config
31
28
keys = list (set (dir (self .__class__ )) - set (dir (Config )))
32
29
33
30
# First fill out all the defaults by copying cls attrs
34
- self ._conf = {k : copy . copy ( getattr (self , k ) ) for k in keys }
31
+ self ._conf = {k : getattr (self , k ) for k in keys }
35
32
36
33
# Then override with any user-specified values
37
- self .update (config = config , method = method , ** kwargs )
38
-
39
- @classmethod
40
- def from_configs (cls , * args ):
41
- config = cls ()
42
- for conf in args :
43
- config .update (config = conf , method = "extend" )
44
- return config
34
+ if config is not None :
35
+ self ._conf .update (config )
36
+ self ._conf .update (kwargs )
45
37
38
+ # now just shortcut the most common dict operations
46
39
def __getitem__ (self , key ):
47
40
return self ._conf [key ]
48
41
49
- # now just shortcut the most common dict operations
50
- def __getattribute__ (self , key ):
51
- hasconf = "_conf" in object .__getattribute__ (self , "__dict__" )
52
- if hasconf :
53
- conf = object .__getattribute__ (self , "_conf" )
54
- if key in conf :
55
- return conf [key ]
56
- return object .__getattribute__ (self , key )
57
-
58
42
def __setitem__ (self , key , value ):
59
43
self ._conf [key ] = value
60
44
61
- def __repr__ (self ):
62
- res = f"{ self .__class__ .__name__ } ("
63
- for k , v in self .items ():
64
- res += f"{ k } : { v } , "
65
- return res [:- 2 ] + ")"
66
-
67
45
def __str__ (self ):
68
46
res = f"{ self .__class__ .__name__ } ("
69
47
for k , v in self .items ():
70
48
res += f"{ k } : { v } , "
71
49
return res [:- 2 ] + ")"
72
50
73
- def _repr_html_ (self ):
74
- repr = f"""
75
- <table style='tr:nth-child(even){{background-color: #dddddd;}};'>
76
- <caption>{ self .__class__ .__name__ } </caption>
77
- <tr>
78
- <th>Key</th>
79
- <th>Value</th>
80
- </tr>
81
- """
82
- for k , v in self .items ():
83
- repr += f"<tr><td>{ k } </td><td>{ v } \n "
84
- repr += "</table>"
85
- return repr
86
-
87
51
def __len__ (self ):
88
52
return len (self ._conf )
89
53
@@ -107,7 +71,7 @@ def __or__(self, other):
107
71
elif isinstance (other , dict ):
108
72
return self .__class__ (config = self ._conf | other )
109
73
else :
110
- raise TypeError ("unsupported operand type(s) for |: {type(self)}and {type(other)}" )
74
+ raise TypeError ("unsupported operand type(s) for |: {type(self)} " " and {type(other)}" )
111
75
112
76
def keys (self ):
113
77
"""A set-like object providing a view on config's keys."""
@@ -121,68 +85,22 @@ def items(self):
121
85
"""A set-like object providing a view on config's items."""
122
86
return self ._conf .items ()
123
87
124
- def copy (self ):
125
- return self .__class__ (config = self ._conf .copy ())
126
-
127
- def update (self , config = None , method = "default" , ** kwargs ):
128
- """Update this config from dict/other config/iterable and
129
- apply any explicit keyword overrides.
130
-
131
- A dict-like update. If ``conf`` is given and has a ``.keys()``
132
- method, performs:
133
-
134
- for k in conf: this[k] = conf[k]
135
-
136
- If ``conf`` is given but lacks a ``.keys()`` method, performs:
88
+ def update (self , conf = None , ** kwargs ):
89
+ """Update this config from dict/other config/iterable.
137
90
138
- for k, v in conf: this[k] = v
91
+ A dict-like update. If ``conf`` is present and has a ``.keys()``
92
+ method, then does: ``for k in conf: this[k] = conf[k]``. If ``conf``
93
+ is present but lacks a ``.keys()`` method, then does:
94
+ ``for k, v in conf: this[k] = v``.
139
95
140
- In both cases, explicit overrides are applied at the end:
141
-
142
- for k in kwargs: this[k] = kwargs[k]
96
+ In either case, this is followed by:
97
+ ``for k in kwargs: this[k] = kwargs[k]``
143
98
"""
144
- # Python < 3.9 does not support set operations for dicts
145
- # [fixme]: Update this to: other = conf | kwargs
146
- # and remove current implementation when 3.9 gets too old. Order of
147
- # conf and kwargs matter to correctly apply explicit overrides
148
-
149
- # Check if both conf and kwargs are given, just conf or just
150
- # kwargs. If none are given do nothing to comply with default
151
- # dict behavior
152
- if config is not None and kwargs :
153
- other = {** config , ** kwargs }
154
- elif config is not None :
155
- other = config
156
- elif kwargs is not None :
157
- other = kwargs
158
- else :
159
- return
160
-
161
- # then, see if we the given config and overrides are a subset of this
162
- # config or it's superset. Depending on the selected method then raise
163
- # errors, ignore or extend the current config if the given config is a
164
- # superset (or disjoint) from the current one.
165
- subset = {k : v for k , v in other .items () if k in self ._conf }
166
- superset = {k : v for k , v in other .items () if k not in subset }
167
-
168
- if method .lower () == "default" :
169
- if superset :
170
- raise ConfigurationError (
171
- "Tried setting the following fields, not a part of "
172
- f"this configuration options: { superset } "
173
- )
174
- conf = other # == subset
175
- elif method .lower () == "subset" :
176
- conf = subset
177
- elif method .lower () == "extend" :
178
- conf = other
179
- else :
180
- raise ValueError (
181
- "Method expected to be one of 'default', " f"'subset' or 'extend'. Got { method } instead."
182
- )
183
-
184
- self ._conf .update (conf )
99
+ if conf is not None :
100
+ self ._conf .update (conf )
101
+ self ._conf .update (kwargs )
185
102
186
103
def toDict (self ):
187
104
"""Return this config as a dict."""
188
105
return self ._conf
106
+
0 commit comments