Last active
August 13, 2017 15:01
-
-
Save jtallieu/47d2b6eb9073748e3e50a0819488fc82 to your computer and use it in GitHub Desktop.
Yet another iteration at providing 'dot' access to dict items. DOES NOT COPY with copy.copy()
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
class Mapping(dict): | |
""" | |
Example: Map({'make': 'Toyota'}, model='Tacoma', year=1998, color='white') | |
""" | |
def __init__(self, *args, **kwargs): | |
super(Mapping, self).__init__(*args, **kwargs) | |
for arg in args: | |
if isinstance(arg, dict): | |
for k, v in arg.iteritems(): | |
self[k] = v | |
if kwargs: | |
for k, v in kwargs.iteritems(): | |
self[k] = v | |
def __getattr__(self, attr): | |
return self.get(attr) | |
def __setattr__(self, key, value): | |
self[key] = value | |
def __delattr__(self, item): | |
self.__delitem__(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment