--HG--
branch : sandbox
This commit is contained in:
@@ -1,20 +0,0 @@
|
|||||||
# 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))
|
|
||||||
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
# http://code.activestate.com/recipes/500261/
|
|
||||||
|
|
||||||
from operator import itemgetter
|
|
||||||
import sys
|
|
||||||
|
|
||||||
def __from_iterable__(cls,arg):
|
|
||||||
return cls.__new__(cls,*arg)
|
|
||||||
|
|
||||||
|
|
||||||
def NamedTuple(typename, field_names):
|
|
||||||
if isinstance(field_names, str):
|
|
||||||
field_names = field_names.split()
|
|
||||||
nargs = len(field_names)
|
|
||||||
|
|
||||||
def __new__(cls, *args, **kwds):
|
|
||||||
if (len(args) == 1) and (getattr(args[0], '__iter__', False)):
|
|
||||||
args = tuple(name for name in args[0])
|
|
||||||
if kwds:
|
|
||||||
try:
|
|
||||||
args += tuple(kwds[name] for name in field_names[len(args):])
|
|
||||||
except KeyError, name:
|
|
||||||
raise TypeError('%s missing required argument: %s' % (typename, name))
|
|
||||||
if len(args) != nargs:
|
|
||||||
raise TypeError('%s takes exactly %d arguments (%d given)' % (typename, nargs, len(args)))
|
|
||||||
return tuple.__new__(cls, args)
|
|
||||||
|
|
||||||
repr_template = '%s(%s)' % (typename, ', '.join('%s=%%r' % name for name in field_names))
|
|
||||||
|
|
||||||
m = dict(vars(tuple)) # pre-lookup superclass methods (for faster lookup)
|
|
||||||
m.update(__doc__= '%s(%s)' % (typename, ', '.join(field_names)),
|
|
||||||
__slots__ = (), # no per-instance dict (so instances are same size as tuples)
|
|
||||||
__new__ = __new__,
|
|
||||||
__repr__ = lambda self, _format=repr_template.__mod__: _format(self),
|
|
||||||
__module__ = sys._getframe(1).f_globals['__name__'],
|
|
||||||
__field_names__ = tuple(field_names),
|
|
||||||
__from_iterable__=classmethod(__from_iterable__),
|
|
||||||
)
|
|
||||||
m.update((name, property(itemgetter(index))) for index, name in enumerate(field_names))
|
|
||||||
|
|
||||||
return type(typename, (tuple,), m)
|
|
||||||
Reference in New Issue
Block a user