Skip to content

Instantly share code, notes, and snippets.

@jtallieu
Last active August 13, 2017 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtallieu/47d2b6eb9073748e3e50a0819488fc82 to your computer and use it in GitHub Desktop.
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()
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