代码拉取完成,页面将自动刷新
# coding=utf-8
class Dict2Object(dict):
"""
dict to object
so you can access like a.attribute but not a['attribute']
reference:
http://stackoverflow.com/questions/1305532/convert-python-dict-to-object
"""
def __init__(self, data = None):
super(Dict2Object, self).__init__()
if data:
self.__update(data, {})
def __update(self, data, did):
dataid = id(data)
did[dataid] = self
for k in data:
dkid = id(data[k])
if did.has_key(dkid):
self[k] = did[dkid]
elif isinstance(data[k], Dict2Object):
self[k] = data[k]
elif isinstance(data[k], dict):
obj = Dict2Object()
obj.__update(data[k], did)
self[k] = obj
obj = None
else:
self[k] = data[k]
def __getattr__(self, key):
return self.get(key, None)
def __setattr__(self, key, value):
if isinstance(value,dict):
self[key] = Dict2Object(value)
else:
self[key] = value
def update(self, *args):
for obj in args:
for k in obj:
if isinstance(obj[k],dict):
self[k] = Dict2Object(obj[k])
else:
self[k] = obj[k]
return self
def merge(self, *args):
for obj in args:
for k in obj:
if self.has_key(k):
if isinstance(self[k],list) and isinstance(obj[k],list):
self[k] += obj[k]
elif isinstance(self[k],list):
self[k].append(obj[k])
elif isinstance(obj[k],list):
self[k] = [self[k]] + obj[k]
elif isinstance(self[k],Dict2Object) and isinstance(obj[k],Dict2Object):
self[k].merge(obj[k] )
elif isinstance(self[k],Dict2Object) and isinstance(obj[k],dict):
self[k].merge(obj[k])
else:
self[k] = [self[k], obj[k]]
else:
if isinstance(obj[k],dict):
self[k] = Dict2Object(obj[k])
else:
self[k] = obj[k]
return self
if __name__ == '__main__':
import doctest
doctest.testmod()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。