File size: 1,462 Bytes
593db5a |
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 |
"""
A dict that implements MutableAttr.
"""
from attrdict.mixins import MutableAttr
import six
__all__ = ['AttrDict']
class AttrDict(dict, MutableAttr):
"""
A dict that implements MutableAttr.
"""
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self._setattr('_sequence_type', tuple)
self._setattr('_allow_invalid_attributes', False)
def _configuration(self):
"""
The configuration for an attrmap instance.
"""
return self._sequence_type
def __getstate__(self):
"""
Serialize the object.
"""
return (
self.copy(),
self._sequence_type,
self._allow_invalid_attributes
)
def __setstate__(self, state):
"""
Deserialize the object.
"""
mapping, sequence_type, allow_invalid_attributes = state
self.update(mapping)
self._setattr('_sequence_type', sequence_type)
self._setattr('_allow_invalid_attributes', allow_invalid_attributes)
def __repr__(self):
return six.u('AttrDict({contents})').format(
contents=super(AttrDict, self).__repr__()
)
@classmethod
def _constructor(cls, mapping, configuration):
"""
A standardized constructor.
"""
attr = cls(mapping)
attr._setattr('_sequence_type', configuration)
return attr
|