URL: https://www.peterbe.com/plog/SmartDict/SmartDict.py

For a work project we needed a convenient way to wrap our SQL recordset, instance objects and dictionary variables to share the same interface. The result is SmartDict which makes it possible to assert that access can be made in any which way you want; something that is very useful when you write templates and don't want to have to know if what you're working with is a dict or a recordset.

This doesn't work:


>>> d= {'name':"Peter"}
>>> print d.get('name') # fine
>>> print d.name # Error!!!

Likewise with some instance objects or record sets, this doesn't work:


>>> d = getRecordsetObject()
>>> print d.name # fine
>>> print d.get('name') # Error!!!

The solution is to process them all through our 'SmartDict':


>>> from SmartDict import SmartDict
>>>
>>> d= SmartDict({'name':"Peter"})
>>> print d.get('name') # fine
>>> print d.name # fine
>>>
>>> d = SmartDict(getRecordsetObject())
>>> print d.name # fine
>>> print d.get('name') # fine

The example above doesn't give much justice to SmartDict because if you know exactly which object you have to deal with you will make sure not to access it incorrectly. SmartDict is useful in cases of integration and development where any two systems are quite far apart and you might not just know what to expect. Feedback?

Comments

Post your own comment
John P. Speno

Why use type() instead of isintance()?

Peter Bengtsson

what benefit does isinstance give over type()? I'm not arguing, I'm asking.

Personally I think:
if isinstance(obj, (tuple,list)):
is more confusing (especially for newcomers) than:
if type(obj) in (types.ListType, types.TupleType):

Chuck

isinstance will also return True if obj is a sublcass of tuble or list. It is usually considered safer to use isinstance unless you know for sure that the type will be a tuple or list and not a sublcass of those.

Peter Bengtsson

That is very useful to know! Thank you.

John P. Speno

In general, isinstance(foo, klass) returns True if
foo is an instance of klass or a subclass of it. It can also accept types.* instead of classes. Martelli calls it the lesser of two evils. :-)

Peter Bengtsson

It's just a of an oddity in python that 'list' is a type object and not a <type 'function'> like one'd expect it to be.

Gerhard Häring

Of course since Python 2.2, you could just write: type(obj) in (list, tuple) ;)

Peter Bengtsson

indeed.

Anonymous

another implementation, but allows more than one mapping in the creator, also less restrictive with what it can be updated from:

class SmartDict(dict):

....def __init__(self, *a, **kw):

........super(SmartDict, self).__init__(**kw)
........for x in a:
............try:
................self.update(x)
............except TypeError:
................self.update(x.__dict__)
........self.__dict__ = self

Your email will never ever be published.

Related posts