Skip to content

Instantly share code, notes, and snippets.

@sooop

sooop/softDict.py

Created Feb 20, 2013
Embed
What would you like to do?
softDict make dictionary into another object to access key-value using .propertyname convention
#!/usr/local/bin/python
#-*-coding:utf-8
#filename:softDict.py
'''
softDict
copyright 2013. All right reserved to sooop.
'''
class SoftDict:
'''
Make data in dict to access object's property convention.
'''
def __init__(self, user_dict):
self._user_dict = user_dict
self._parse()
def _parse(self):
for key in self._user_dict.iterkeys():
value = self._user_dict[key]
if type(value) == dict:
value = SoftDict(value)
setattr(self, key, value)
def main():
a = { "meta":{"name":"a", "msg":"OK"},"res":"google"}
b = SoftDict(a)
print b.meta.msg
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment