From 67375bd6e32813cecc6b5d1ff7e7f9a8ebeaa53a Mon Sep 17 00:00:00 2001 From: Andreas Date: Thu, 22 Oct 2009 19:51:09 +0000 Subject: [PATCH] attrdict and namedtuple moved to lib --HG-- branch : sandbox --- .pydevproject | 1 + lib/attrdict.py | 20 ++++++++++++++++++++ {mpl => lib}/namedtuple.py | 0 src/attrdict.py | 33 --------------------------------- 4 files changed, 21 insertions(+), 33 deletions(-) create mode 100644 lib/attrdict.py rename {mpl => lib}/namedtuple.py (100%) delete mode 100644 src/attrdict.py diff --git a/.pydevproject b/.pydevproject index e9c3ab1..e2800d2 100644 --- a/.pydevproject +++ b/.pydevproject @@ -7,6 +7,7 @@ /sandbox/gui /sandbox/mpl /sandbox/megui +/sandbox/lib python 2.5 Default diff --git a/lib/attrdict.py b/lib/attrdict.py new file mode 100644 index 0000000..7f96f32 --- /dev/null +++ b/lib/attrdict.py @@ -0,0 +1,20 @@ +# Copyright (c) 2009 Andreas Balogh +# See LICENSE for details. + +class AttrDict(dict): + """A dict whose items can also be accessed as member variables.""" + def __init__(self, *args, **kwargs): + dict.__init__(self, *args, **kwargs) + self.__dict__ = self + + def copy(self): + ch = AttrDict(self) + return ch + + def __repr__(self): + return 'AttrDict(' + dict.__repr__(self) + ')' + + @classmethod + def fromkeys(cls, seq, value = None): + return AttrDict(dict.fromkeys(seq, value)) + diff --git a/mpl/namedtuple.py b/lib/namedtuple.py similarity index 100% rename from mpl/namedtuple.py rename to lib/namedtuple.py diff --git a/src/attrdict.py b/src/attrdict.py deleted file mode 100644 index f3f4178..0000000 --- a/src/attrdict.py +++ /dev/null @@ -1,33 +0,0 @@ -# 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