/
container.py
361 lines (257 loc) · 9.04 KB
/
container.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from ..docstring import _docstring_substitution_definitions
from ..functions import deepcopy
from ..meta import DocstringRewriteMeta
class Container(metaclass=DocstringRewriteMeta):
"""Abstract base class for storing components.
.. warning:: The ``custom`` component dictionary is only shallow
copied when initialised from the *source* parameter,
regardless of the value of the *copy* parameter. This
is to avoid potentially expensive deep copies of the
dictionary values.
A subclass of `Container` that requires custom
dictionary values to be deep copied should ensure
that this occurs its `__init__` method.
.. versionadded:: (cfdm) 1.7.0
"""
def __init__(self, source=None, copy=True):
"""**Initialiation**
:Parameters:
{{init source: optional}}
{{init copy: `bool`, optional}}
"""
self._components = {}
if source is not None:
# WARNING: The 'custom' dictionary is only shallow copied
# from source
try:
custom = source._get_component("custom", {})
except AttributeError:
custom = {}
else:
custom = custom.copy()
else:
custom = {}
self._set_component("custom", custom, copy=False)
def __deepcopy__(self, memo):
"""Called by the `copy.deepcopy` function.
x.__deepcopy__() <==> copy.deepcopy(x)
.. versionadded:: (cfdm) 1.7.0
**Examples**
>>> import copy
>>> f = {{package}}.{{class}}()
>>> g = copy.deepcopy(f)
"""
return self.copy()
def __docstring_substitutions__(self):
"""Defines docstring substitutions for a class and subclasses.
That is, defines docstring substitutions that apply to this
class and all of its subclasses.
These are in addtion to, and take precendence over, docstring
substitutions defined by the base classes of this class.
See `_docstring_substitutions` for details.
.. versionaddedd:: (cfdm) 1.8.7.0
:Returns:
`dict`
The docstring substitutions that have been applied.
"""
return _docstring_substitution_definitions
def __docstring_package_depth__(self):
"""Returns the package depth for {{package}} substitutions.
See `_docstring_package_depth` for details.
"""
return 1
def _default(self, default, message=None):
"""Return a value or raise an Exception for a default case.
.. versionadded:: (cfdm) 1.7.0
:Parameters:
default:
The value to return, or to raise if set to an `Exception`
instance.
message: `str`, optional
The error message to raise with *default* if it is an
`Exception` instance.
:Returns:
The value of *default* if it is not an `Exception`
instance.
**Examples**
>>> f = {{package}}.{{class}}()
>>> f._default(AttributeError()) # Raises Exception
Traceback (most recent call last):
...
AttributeError
>>> f._default(ValueError("Missing item")) # Raises Exception
Traceback (most recent call last):
...
ValueError: Missing item
>>> f._default(ValueError(), message="No component") # Raises Exception
Traceback (most recent call last):
...
ValueError: No component
>>> f._default(False)
False
>>> f._default('Not set')
'Not set'
>>> f._default(1)
1
"""
if isinstance(default, Exception):
if message is not None and not default.args:
default = type(default)(message)
raise default
return default
def _del_component(self, component, default=ValueError()):
"""Remove a component.
.. versionadded:: (cfdm) 1.7.0
.. seealso:: `_get_component`, `_has_component`, `_set_component`
:Parameters:
component:
The name of the component to be removed.
default: optional
Return *default* if the component has not been set.
{{default Exception}}
:Returns:
The removed component. If unset then *default* is
returned, if provided.
**Examples**
>>> f = {{package}}.{{class}}()
>>> f._set_component('foo', 'bar')
>>> f._has_component('foo')
True
>>> f._get_component('foo')
'bar'
>>> f._del_component('foo')
'bar'
>>> f._has_component('foo')
False
"""
try:
return self._components.pop(component)
except KeyError:
if default is None:
return
return self._default(
default,
f"{self.__class__.__name__} has no {component!r} component",
)
@property
def _custom(self):
"""Customisable storage for additional attributes.
See https://ncas-cms.github.io/cfdm/extensions.html for more
information.
.. versionadded:: (cfdm) 1.7.4
**Examples**
>>> f = {{package}}.{{class}}()
>>> f._custom
{}
>>> f._custom['feature_1'] = 1
>>> g = f.copy()
>>> g._custom['feature_2'] = 2
>>> f._custom
{'feature_1': 1}
>>> g._custom
{'feature_1': 1, 'feature_2': 2}
>>> del g._custom['feature_1']
>>> g._custom
{'feature_2': 2}
>>> f._custom
{'feature_1': 1}
"""
return self._get_component("custom")
def _get_component(self, component, default=ValueError()):
"""Return a component.
.. versionadded:: (cfdm) 1.7.0
.. seealso:: `_del_component`, `_has_component`, `_set_component`
:Parameters:
component:
The name of the component to be returned.
default: optional
Return *default* if the component has not been set.
{{default Exception}}
:Returns:
The component. If unset then *default* is returned, if
provided.
**Examples**
>>> f = {{package}}.{{class}}()
>>> f._set_component('foo', 'bar')
>>> f._has_component('foo')
True
>>> f._get_component('foo')
'bar'
>>> f._del_component('foo')
'bar'
>>> f._has_component('foo')
False
"""
try:
return self._components[component]
except KeyError:
if default is None:
return
return self._default(
default,
f"{self.__class__.__name__} has no {component!r} component",
)
def _has_component(self, component):
"""Whether a component has been set.
.. versionadded:: (cfdm) 1.7.0
.. seealso:: `_del_component`, `_get_component`, `_set_component`
:Parameters:
component:
The name of the component.
:Returns:
`bool`
True if the component has been set, otherwise False.
**Examples**
>>> f = {{package}}.{{class}}()
>>> f._set_component('foo', 'bar')
>>> f._has_component('foo')
True
>>> f._get_component('foo')
'bar'
>>> f._del_component('foo')
'bar'
>>> f._has_component('foo')
False
"""
return component in self._components
def _set_component(self, component, value, copy=True):
"""Set a component.
.. versionadded:: (cfdm) 1.7.0
.. seealso:: `_del_component`, `_get_component`, `_has_component`
:Parameters:
component: `str`
The name of the component.
value:
The value for the component.
:Returns:
`None`
**Examples**
>>> f = {{package}}.{{class}}()
>>> f._set_component('foo', 'bar')
>>> f._has_component('foo')
True
>>> f._get_component('foo')
'bar'
>>> f._del_component('foo')
'bar'
>>> f._has_component('foo')
False
"""
if copy:
value = deepcopy(value)
self._components[component] = value
# ----------------------------------------------------------------
# Methods
# ----------------------------------------------------------------
def copy(self):
"""Return a deep copy.
``f.copy()`` is equivalent to ``copy.deepcopy(f)``.
.. versionadded:: (cfdm) 1.7.0
:Returns:
`{{class}}`
The deep copy.
**Examples**
>>> f = {{package}}.{{class}}()
>>> g = f.copy()
"""
return type(self)(source=self, copy=True)