Kwissle

My real-time quiz battle game Kwissle.com

Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Photos

Photoalbum, both old and new.

Twitter

Follow me on Twitter

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 
Python

SmartDict - a smart 'dict' wrapper

http://www.peterbe.com/plog/SmartDict/SmartDict.py

14th of July 2005

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?



Comment

John P. Speno - 14th July 2005  [«« Reply to this]
Why use type() instead of isintance()?
Peter Bengtsson - 14th July 2005   [«« Reply to this]
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 - 14th July 2005   [«« Reply to this]
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 - 14th July 2005   [«« Reply to this]
That is very useful to know! Thank you.
John P. Speno - 14th July 2005   [«« Reply to this]
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 - 14th July 2005   [«« Reply to this]
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 - 14th July 2005  [«« Reply to this]
Of course since Python 2.2, you could just write: type(obj) in (list, tuple) ;)
Peter Bengtsson - 14th July 2005   [«« Reply to this]
indeed.
Anonymous - 14th July 2005  [«« Reply to this]
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
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.