From 0acda95080bc0b0b92cb8f8733edd587dd2ff59a Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 12 Oct 2009 20:13:41 +0000 Subject: [PATCH] added attribute-like access to tuple and dict --HG-- branch : sandbox --- src/attrdict.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/attrdict.py diff --git a/src/attrdict.py b/src/attrdict.py new file mode 100644 index 0000000..f3f4178 --- /dev/null +++ b/src/attrdict.py @@ -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