-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from brian-rose/attrdict
Python 3.10 compatibility
- Loading branch information
Showing
21 changed files
with
580 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ dependencies: | |
- fortran-compiler | ||
- toolchain3 | ||
- future | ||
- attrdict | ||
- pytest | ||
- codecov | ||
- pytest-cov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ dependencies: | |
- gfortran_osx-64 | ||
- libgfortran | ||
- future | ||
- attrdict | ||
- pytest | ||
- codecov | ||
- pytest-cov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,3 @@ dependencies: | |
- scipy | ||
- flang | ||
- future | ||
- attrdict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
attrdict contains several mapping objects that allow access to their | ||
keys as attributes. | ||
""" | ||
from .mapping import AttrMap | ||
from .dictionary import AttrDict | ||
from .default import AttrDefault | ||
|
||
|
||
__all__ = ['AttrMap', 'AttrDict', 'AttrDefault'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
""" | ||
A subclass of MutableAttr that has defaultdict support. | ||
""" | ||
from collections.abc import Mapping | ||
|
||
import six | ||
|
||
from .mixins import MutableAttr | ||
|
||
|
||
__all__ = ['AttrDefault'] | ||
|
||
|
||
class AttrDefault(MutableAttr): | ||
""" | ||
An implementation of MutableAttr with defaultdict support | ||
""" | ||
def __init__(self, default_factory=None, items=None, sequence_type=tuple, | ||
pass_key=False): | ||
if items is None: | ||
items = {} | ||
elif not isinstance(items, Mapping): | ||
items = dict(items) | ||
|
||
self._setattr('_default_factory', default_factory) | ||
self._setattr('_mapping', items) | ||
self._setattr('_sequence_type', sequence_type) | ||
self._setattr('_pass_key', pass_key) | ||
self._setattr('_allow_invalid_attributes', False) | ||
|
||
def _configuration(self): | ||
""" | ||
The configuration for a AttrDefault instance | ||
""" | ||
return self._sequence_type, self._default_factory, self._pass_key | ||
|
||
def __getitem__(self, key): | ||
""" | ||
Access a value associated with a key. | ||
Note: values returned will not be wrapped, even if recursive | ||
is True. | ||
""" | ||
if key in self._mapping: | ||
return self._mapping[key] | ||
elif self._default_factory is not None: | ||
return self.__missing__(key) | ||
|
||
raise KeyError(key) | ||
|
||
def __setitem__(self, key, value): | ||
""" | ||
Add a key-value pair to the instance. | ||
""" | ||
self._mapping[key] = value | ||
|
||
def __delitem__(self, key): | ||
""" | ||
Delete a key-value pair | ||
""" | ||
del self._mapping[key] | ||
|
||
def __len__(self): | ||
""" | ||
Check the length of the mapping. | ||
""" | ||
return len(self._mapping) | ||
|
||
def __iter__(self): | ||
""" | ||
Iterated through the keys. | ||
""" | ||
return iter(self._mapping) | ||
|
||
def __missing__(self, key): | ||
""" | ||
Add a missing element. | ||
""" | ||
if self._pass_key: | ||
self[key] = value = self._default_factory(key) | ||
else: | ||
self[key] = value = self._default_factory() | ||
|
||
return value | ||
|
||
def __repr__(self): | ||
""" | ||
Return a string representation of the object. | ||
""" | ||
return six.u( | ||
"AttrDefault({default_factory}, {pass_key}, {mapping})" | ||
).format( | ||
default_factory=repr(self._default_factory), | ||
pass_key=repr(self._pass_key), | ||
mapping=repr(self._mapping), | ||
) | ||
|
||
def __getstate__(self): | ||
""" | ||
Serialize the object. | ||
""" | ||
return ( | ||
self._default_factory, | ||
self._mapping, | ||
self._sequence_type, | ||
self._pass_key, | ||
self._allow_invalid_attributes, | ||
) | ||
|
||
def __setstate__(self, state): | ||
""" | ||
Deserialize the object. | ||
""" | ||
(default_factory, mapping, sequence_type, pass_key, | ||
allow_invalid_attributes) = state | ||
|
||
self._setattr('_default_factory', default_factory) | ||
self._setattr('_mapping', mapping) | ||
self._setattr('_sequence_type', sequence_type) | ||
self._setattr('_pass_key', pass_key) | ||
self._setattr('_allow_invalid_attributes', allow_invalid_attributes) | ||
|
||
@classmethod | ||
def _constructor(cls, mapping, configuration): | ||
""" | ||
A standardized constructor. | ||
""" | ||
sequence_type, default_factory, pass_key = configuration | ||
return cls(default_factory, mapping, sequence_type=sequence_type, | ||
pass_key=pass_key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
A dict that implements MutableAttr. | ||
""" | ||
from .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 |
Oops, something went wrong.