added attribute-like access to tuple and dict

--HG--
branch : aspn
This commit is contained in:
Andreas
2009-10-12 20:13:41 +00:00
parent ca23b54ac7
commit 39a64efbdd
2 changed files with 211 additions and 0 deletions

33
src/attrdict.py Normal file
View File

@@ -0,0 +1,33 @@
# http://code.activestate.com/recipes/473786/
class AttrDict(dict):
"""A dictionary with attribute-style access. It maps attribute access to
the real dictionary. """
def __init__(self, init={}):
dict.__init__(self, init)
def __getstate__(self):
return self.__dict__.items()
def __setstate__(self, items):
for key, val in items:
self.__dict__[key] = val
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, dict.__repr__(self))
def __setitem__(self, key, value):
return super(AttrDict, self).__setitem__(key, value)
def __getitem__(self, name):
return super(AttrDict, self).__getitem__(name)
def __delitem__(self, name):
return super(AttrDict, self).__delitem__(name)
__getattr__ = __getitem__
__setattr__ = __setitem__
def copy(self):
ch = AttrDict(self)
return ch