generic object implementation
Bengt Richter
bokr at oz.net
Mon Nov 22 01:53:26 EST 2004
On Sun, 21 Nov 2004 23:55:48 GMT, Steven Bethard <steven.bethard at gmail.com> wrote:
>So I'm trying to get a prototype implementation of the 'generic object'
>type. (I'm currently calling it 'bunch', but only because I can't
>really think of anything better.) I'd like some feedback on what
>methods it needs to support. Ideally I'd keep this as minimal as
>possible...
UIAM 'bunch' already has a related meaning from c.l.py past (approx the first
three lines of your class), so how about 'gob' ? ;-)
>
>Remember that the goal of the 'generic object' type is to allow the
>programmer to make the design decision that attribute-style access is
>more appropriate than []-style access. Given that, my feeling is that
>the 'generic object' type should *not* support __(get|set|del)item__ ,
>though I'm still undecided on __len__, __iter__, __contains__, items,
>keys, values, etc.
>
>Here's what I have currently:
>
>import operator as _operator
>
>class bunch(object):
> def __init__(self, **kwds):
> self.__dict__.update(kwds)
>
> def __eq__(self, other):
> if not isinstance(other, bunch):
> return False
> attrs = set(self.__dict__)
> if attrs != set(other.__dict__):
> return False
> for attr in attrs:
> if not getattr(self, attr) == getattr(other, attr):
> return False
> return True
>
> def __repr__(self):
> return '%s(%s)' % (self.__class__.__name__,
> ', '.join('%s=%r' % (k, v)
> for k, v in self.__dict__.items()))
>
> def update(self, other):
> self.__dict__.update(other.__dict__)
>
> @classmethod
> def frommapping(cls, mapping,
> getkeys=iter, getitem=_operator.getitem):
> result = bunch()
> for key in getkeys(mapping):
> value = getitem(mapping, key)
> try:
> value = bunch.frommapping(value)
> except TypeError:
> pass
> setattr(result, key, value)
> return result
I dunno, begins to look more like a dict with getattr access substituted for __getitem__
than a primitive generic object...
>>> class dbunch(dict):
... def __metaclass__(name, bases, cdict):
... cdict.update(dict.__dict__)
... cdict['__getattr__'] = cdict['__getitem__']
... cdict['__setattr__'] = cdict['__setitem__']
... cdict['__delattr__'] = cdict['__delitem__']
... def raiseunsub(*ignore): raise TypeError, 'unsubscriptable object'
... cdict['__getitem__'] = raiseunsub
... cdict['__setitem__'] = raiseunsub
... cdict['__delitem__'] = raiseunsub
... return type(name, bases, cdict)
...
>>> db = dbunch(x=123)
>>> db
{'x': 123}
>>> db.x
123
>>> db['x']
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 7, in raiseunsub
TypeError: unsubscriptable object
>>> db.y = 456
>>> db
{'y': 456, 'x': 123}
>>> db.x = 789
>>> db
{'y': 456, 'x': 789}
>>> del db.x
>>> db
{'y': 456}
>>> db.x = 101112
>>> db.z = 131415
>>> db
{'y': 456, 'x': 101112, 'z': 131415}
>>> db.keys()
['y', 'x', 'z']
>>> db.values()
[456, 101112, 131415]
>>> db.items()
[('y', 456), ('x', 101112), ('z', 131415)]
>>> for k in db: print k,
...
y x z
... and etc. methods from dict, so should it be a restricted builtin subclass of dict
or derive from object directly? It the hope for a lean representation for small numbers
of items?
Regards,
Bengt Richter
More information about the Python-list
mailing list