Skip to content

Instantly share code, notes, and snippets.

@sooop
Created February 20, 2013 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sooop/4993733 to your computer and use it in GitHub Desktop.
Save sooop/4993733 to your computer and use it in GitHub Desktop.
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