-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84364cb
commit 7f6db75
Showing
4 changed files
with
340 additions
and
340 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
''' | ||
attridict.py: | ||
Alvin Shaita <[email protected]> | ||
attridict.py: | ||
Alvin Shaita <[email protected]> | ||
''' | ||
|
||
__author__ = "Alvin Shaita" | ||
__email__ = "[email protected]" | ||
__author__ = "Alvin Shaita" | ||
__email__ = "[email protected]" | ||
|
||
|
||
from mixins import MapMixin | ||
|
@@ -13,83 +13,83 @@ | |
__all__ = ["AttriDict"] | ||
|
||
class AttriDict(dict, MapMixin): | ||
'''AttriDict''' | ||
'''AttriDict''' | ||
|
||
__version__ = "0.0.9" | ||
__version__ = "0.0.9" | ||
|
||
__all__ = ["to_dict"] | ||
__all__ = ["to_dict"] | ||
|
||
def __init__(self, *args, **kwargs): | ||
if args and not isinstance(*args, dict): | ||
raise AttributeError( | ||
"non dict argument provided" | ||
) | ||
def __init__(self, *args, **kwargs): | ||
if args and not isinstance(*args, dict): | ||
raise AttributeError( | ||
"non dict argument provided" | ||
) | ||
|
||
super(type(self), self).__init__(*args, **kwargs) | ||
super(type(self), self).__init__(*args, **kwargs) | ||
|
||
def _attrify(self, obj): | ||
""" | ||
Convert dict instance objects to attridict | ||
""" | ||
if isinstance(obj, dict): | ||
obj = type(self)(obj) | ||
elif isinstance(obj, (list, set, tuple)): | ||
obj = type(obj)(self._attrify(i) for i in obj) | ||
def _attrify(self, obj): | ||
""" | ||
Convert dict instance objects to attridict | ||
""" | ||
if isinstance(obj, dict): | ||
obj = type(self)(obj) | ||
elif isinstance(obj, (list, set, tuple)): | ||
obj = type(obj)(self._attrify(i) for i in obj) | ||
|
||
return obj | ||
return obj | ||
|
||
def _valid_key(self, key): | ||
""" | ||
Check if the key provided is a valid key | ||
""" | ||
if key in dir(self): | ||
return False | ||
def _valid_key(self, key): | ||
""" | ||
Check if the key provided is a valid key | ||
""" | ||
if key in dir(self): | ||
return False | ||
|
||
return True | ||
return True | ||
|
||
def to_dict(self): | ||
""" | ||
Return a dict equivalent of the attridict object | ||
""" | ||
return dict(self) | ||
def to_dict(self): | ||
""" | ||
Return a dict equivalent of the attridict object | ||
""" | ||
return dict(self) | ||
|
||
def copy(self): | ||
new_obj = type(self)(self) | ||
return new_obj | ||
def copy(self): | ||
new_obj = type(self)(self) | ||
return new_obj | ||
|
||
|
||
|
||
try: | ||
import yaml | ||
import yaml | ||
|
||
# yaml serialization | ||
def represent_attridict(dumper, data): | ||
return dumper.represent_mapping("!attridict", data) | ||
# yaml serialization | ||
def represent_attridict(dumper, data): | ||
return dumper.represent_mapping("!attridict", data) | ||
|
||
yaml.representer.Representer.add_representer(AttriDict, represent_attridict) | ||
yaml.representer.Representer.add_representer(AttriDict, represent_attridict) | ||
|
||
|
||
def represent_attridict_safe(dumper, data): | ||
return dumper.represent_dict(data) | ||
def represent_attridict_safe(dumper, data): | ||
return dumper.represent_dict(data) | ||
|
||
yaml.representer.SafeRepresenter.add_representer(AttriDict, represent_attridict_safe) | ||
yaml.representer.SafeRepresenter.add_representer(AttriDict, represent_attridict_safe) | ||
|
||
|
||
def construct_attridict(loader, node): | ||
value = loader.construct_mapping(node) | ||
return AttriDict(value) | ||
def construct_attridict(loader, node): | ||
value = loader.construct_mapping(node) | ||
return AttriDict(value) | ||
|
||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.BaseLoader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.FullLoader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.SafeLoader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.Loader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.UnsafeLoader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.BaseLoader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.FullLoader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.SafeLoader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.Loader) | ||
yaml.add_constructor("!attridict", construct_attridict, Loader=yaml.UnsafeLoader) | ||
except Exception as e: | ||
... | ||
... | ||
|
||
|
||
|
||
if __name__ == "attridict": | ||
import sys | ||
AttriDict.AttriDict = AttriDict | ||
sys.modules[__name__] = AttriDict | ||
import sys | ||
AttriDict.AttriDict = AttriDict | ||
sys.modules[__name__] = AttriDict |
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 |
---|---|---|
@@ -1,86 +1,86 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
def assign_self(func): | ||
""" | ||
Set the received object the value of the key in the mapping container | ||
""" | ||
def wrapper(container, key): | ||
new_object = func(container, key) | ||
container[key] = new_object | ||
return container[key] | ||
return wrapper | ||
""" | ||
Set the received object the value of the key in the mapping container | ||
""" | ||
def wrapper(container, key): | ||
new_object = func(container, key) | ||
container[key] = new_object | ||
return container[key] | ||
return wrapper | ||
|
||
|
||
__all__ = ["MapMixin"] | ||
|
||
class MapMixin(ABC): | ||
|
||
@abstractmethod | ||
def _valid_key(self, key): | ||
pass | ||
|
||
@abstractmethod | ||
def _attrify(self, key): | ||
pass | ||
|
||
|
||
@assign_self | ||
def __call__(self, key): | ||
""" | ||
Access value through object call | ||
eg. att(key) | ||
""" | ||
obj = self[key] | ||
return self._attrify(obj) | ||
|
||
@assign_self | ||
def __getattr__(self, key): | ||
""" | ||
Accesss value via attribute | ||
""" | ||
if not self._valid_key(key): | ||
raise AttributeError( | ||
"invalid key, '{key}'".format(key=key) | ||
) | ||
|
||
if key not in self: | ||
raise AttributeError( | ||
"key does not exist, '{key}'".format(key=key) | ||
) | ||
|
||
obj = self[key] | ||
return self._attrify(obj) | ||
|
||
def __setattr__(self, key, value): | ||
if not self._valid_key(key): | ||
raise AttributeError( | ||
"invalid key, '{key}'".format(key=key) | ||
) | ||
|
||
self[key] = value | ||
|
||
def __delattr__(self, key): | ||
del self[key] | ||
|
||
def __add__(self, other): | ||
""" | ||
Add a dict, or its child to an attridict object | ||
""" | ||
if not isinstance(other, dict): | ||
raise TypeError( | ||
"'{other}' is not a mapping object".format(other=other) | ||
) | ||
|
||
return type(self)({**self, **other}) | ||
|
||
def __radd__(self, other): | ||
""" | ||
Add an attridict object to a dict, or its child | ||
""" | ||
if not isinstance(other, dict): | ||
raise TypeError( | ||
"'{other}' is not a mapping object".format(other=other) | ||
) | ||
|
||
return type(self)({**other, **self}) | ||
@abstractmethod | ||
def _valid_key(self, key): | ||
pass | ||
|
||
@abstractmethod | ||
def _attrify(self, key): | ||
pass | ||
|
||
|
||
@assign_self | ||
def __call__(self, key): | ||
""" | ||
Access value through object call | ||
eg. att(key) | ||
""" | ||
obj = self[key] | ||
return self._attrify(obj) | ||
|
||
@assign_self | ||
def __getattr__(self, key): | ||
""" | ||
Accesss value via attribute | ||
""" | ||
if not self._valid_key(key): | ||
raise AttributeError( | ||
"invalid key, '{key}'".format(key=key) | ||
) | ||
|
||
if key not in self: | ||
raise AttributeError( | ||
"key does not exist, '{key}'".format(key=key) | ||
) | ||
|
||
obj = self[key] | ||
return self._attrify(obj) | ||
|
||
def __setattr__(self, key, value): | ||
if not self._valid_key(key): | ||
raise AttributeError( | ||
"invalid key, '{key}'".format(key=key) | ||
) | ||
|
||
self[key] = value | ||
|
||
def __delattr__(self, key): | ||
del self[key] | ||
|
||
def __add__(self, other): | ||
""" | ||
Add a dict, or its child to an attridict object | ||
""" | ||
if not isinstance(other, dict): | ||
raise TypeError( | ||
"'{other}' is not a mapping object".format(other=other) | ||
) | ||
|
||
return type(self)({**self, **other}) | ||
|
||
def __radd__(self, other): | ||
""" | ||
Add an attridict object to a dict, or its child | ||
""" | ||
if not isinstance(other, dict): | ||
raise TypeError( | ||
"'{other}' is not a mapping object".format(other=other) | ||
) | ||
|
||
return type(self)({**other, **self}) |
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,23 +10,23 @@ | |
long_description = readme.read() | ||
|
||
setup( | ||
name="attridict", | ||
version=attridict.__version__, | ||
author="Alvin Shaita", | ||
author_email="[email protected]", | ||
url="https://github.com/alvinshaita/attridict", | ||
license="MIT License", | ||
packages=["."], | ||
install_requires=["PyYAML==6.0.1"], | ||
keywords="attridict, attrdict, struct, dict, dot, attribute, attributes, dictionary, attr, object", | ||
description="A dict implementation with support for easy and clean access of its values through attributes", | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
classifiers=[ | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: Implementation :: CPython", | ||
"Programming Language :: Python :: Implementation :: PyPy", | ||
] | ||
name="attridict", | ||
version=attridict.__version__, | ||
author="Alvin Shaita", | ||
author_email="[email protected]", | ||
url="https://github.com/alvinshaita/attridict", | ||
license="MIT License", | ||
packages=["."], | ||
install_requires=["PyYAML==6.0.1"], | ||
keywords="attridict, attrdict, struct, dict, dot, attribute, attributes, dictionary, attr, object", | ||
description="A dict implementation with support for easy and clean access of its values through attributes", | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
classifiers=[ | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: Implementation :: CPython", | ||
"Programming Language :: Python :: Implementation :: PyPy", | ||
] | ||
) |
Oops, something went wrong.