softDict make dictionary into another object to access key-value using .propertyname convention
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
#!/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