Created
September 6, 2011 18:43
-
-
Save capttwinky/1198571 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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