Skip to content

Instantly share code, notes, and snippets.

@capttwinky
Created September 6, 2011 18:43
Show Gist options
  • Save capttwinky/1198571 to your computer and use it in GitHub Desktop.
Save capttwinky/1198571 to your computer and use it in GitHub Desktop.
class dictWrap(object):
def __init__(self, dictIn = False, fnXform = False):
self.noneVal = None
if dictIn:
if fnXform:
dictIn = dict((k,fnXform(v)) for k,v in dictIn.items())
self.__dict__.update(dictIn)
def __getattribute__(self, attr):
try:
return object.__getattribute__(self, attr)
except:
return self.noneVal
myDict = {'a': 'apple', 'b': 'banana'}
myObj1 = dictWrap(myDict)
myObj2 = dictWrap(myDict, lambda x: str(x).upper())
if True:
print "%r"%myDict['a']
print "%r"%myObj1.a
print "%r"%myObj2.a
try:
print "%r"%myDict['balloon']
except Exception as e:
print "error: %r"%e
try:
print "%r"%myObj2.balloon
except Exception as e:
print "error: %r"%e
print "%r"%myDict.get('balloon')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment