Created
June 11, 2011 00:31
-
-
Save brainsik/1020091 to your computer and use it in GitHub Desktop.
Override Python's dict with this for JS style dot notation access :-)
This file contains hidden or 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
# encoding: utf-8 | |
class DotDict(dict): | |
def __init__(self, *a, **kw): | |
dict.__init__(self, *a, **kw) | |
for key in self: | |
self._validate_key(key) | |
def _validate_key(self, key): | |
try: | |
class IsIdentifier(object): __slots__ = key | |
except TypeError: | |
raise TypeError("invalid identifier: '%s'" % key) | |
try: | |
dict.__getattribute__(self, key) | |
except AttributeError: | |
pass | |
else: | |
raise TypeError("builtin dict attribute: '%s'" % key) | |
def __getattribute__(self, name): | |
try: | |
return dict.__getattribute__(self, name) | |
except AttributeError: | |
if name in self: | |
return self[name] | |
raise | |
def __setitem__(self, key, value): | |
self._validate_key(key) | |
dict.__setitem__(self, key, value) | |
__setattr__ = __setitem__ | |
__delattr__ = dict.__delitem__ | |
def copy(self, *a, **kw): | |
obj = dict.copy(self, *a, **kw) | |
if obj.__class__ is DotDict.__class__: | |
return obj | |
return DotDict(obj) | |
@classmethod | |
def fromkeys(cls, *a, **kw): | |
obj = super(DotDict, cls).fromkeys(*a, **kw) | |
if obj.__class__ is DotDict.__class__: | |
return obj | |
return cls(obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment