2020-06-08 08:00:42 +08:00
|
|
|
import json
|
|
|
|
from json import JSONEncoder
|
2020-11-09 02:30:04 +08:00
|
|
|
from inspect import ismethod, isfunction, isbuiltin, isabstract
|
2020-06-08 08:00:42 +08:00
|
|
|
|
2019-07-16 16:33:07 +08:00
|
|
|
def multiDict2Dict(md):
|
|
|
|
ns = {}
|
|
|
|
for k,v in md.items():
|
|
|
|
ov = ns.get(k,None)
|
|
|
|
if ov is None:
|
|
|
|
ns[k] = v
|
|
|
|
elif type(ov) == type([]):
|
|
|
|
ov.append(v)
|
|
|
|
ns[k] = ov
|
|
|
|
else:
|
|
|
|
ns[k] = [ov,v]
|
|
|
|
return ns
|
|
|
|
|
2020-11-07 09:54:45 +08:00
|
|
|
class DictObject:
|
2020-06-03 11:10:05 +08:00
|
|
|
def __init__(self,**kw):
|
2020-11-07 13:30:46 +08:00
|
|
|
self.org_keys__ = []
|
|
|
|
self.org_keys__ = [ k for k in self.__dict__.keys()]
|
2020-06-03 11:10:05 +08:00
|
|
|
for k,v in kw.items():
|
2020-06-08 08:00:42 +08:00
|
|
|
self.update({k:self.__DOitem(v)})
|
2020-06-03 11:10:05 +08:00
|
|
|
|
2020-11-07 13:30:46 +08:00
|
|
|
def __getattr__(self,name):
|
|
|
|
if name in self._addon().keys():
|
|
|
|
return self.__getitem__(name)
|
|
|
|
return None
|
|
|
|
|
2020-11-07 09:54:45 +08:00
|
|
|
def update(self,kw):
|
2020-11-07 13:30:46 +08:00
|
|
|
self.__dict__.update(kw)
|
|
|
|
|
|
|
|
def _addon(self):
|
|
|
|
ks = [ k for k in self.__dict__.keys() if k not in self.org_keys__]
|
|
|
|
return {k:v for k,v in self.__dict__.items() if k in ks}
|
2020-11-07 09:54:45 +08:00
|
|
|
|
2020-11-07 03:44:59 +08:00
|
|
|
def clear(self):
|
2020-11-07 13:30:46 +08:00
|
|
|
for k in self._addon().keys():
|
|
|
|
self.__dict__.pop(k)
|
2020-11-07 03:44:59 +08:00
|
|
|
|
2020-11-07 13:30:46 +08:00
|
|
|
def get(self,name,default=None):
|
|
|
|
return self._addon().get(name,default)
|
2020-11-07 03:44:59 +08:00
|
|
|
|
2020-11-07 13:30:46 +08:00
|
|
|
def pop(self,k,default=None):
|
|
|
|
return self.__dict__.pop(k,default)
|
2020-11-07 03:44:59 +08:00
|
|
|
|
2020-11-07 13:30:46 +08:00
|
|
|
def popitem(self):
|
|
|
|
return self.__dict__.popitem()
|
2020-11-07 03:44:59 +08:00
|
|
|
|
2020-11-07 09:54:45 +08:00
|
|
|
def items(self):
|
2020-11-07 13:30:46 +08:00
|
|
|
return self._addon().items()
|
2020-11-07 09:54:45 +08:00
|
|
|
|
|
|
|
def keys(self):
|
2020-11-07 13:30:46 +08:00
|
|
|
return self._addon().keys()
|
2020-11-07 09:54:45 +08:00
|
|
|
|
|
|
|
def values(self):
|
2020-11-07 13:30:46 +08:00
|
|
|
return self._addon().values()
|
2020-11-07 09:54:45 +08:00
|
|
|
|
2020-11-09 05:04:05 +08:00
|
|
|
def __delitem__(self,key):
|
|
|
|
self.pop(key)
|
|
|
|
|
2020-11-07 09:54:45 +08:00
|
|
|
def __getitem__(self,name):
|
2020-11-07 13:30:46 +08:00
|
|
|
return self._addon().get(name)
|
2020-11-07 09:54:45 +08:00
|
|
|
|
|
|
|
def __setitem__(self,name,value):
|
2020-11-07 13:30:46 +08:00
|
|
|
self.__dict__[name] = value
|
2020-11-07 09:54:45 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2020-11-07 13:30:46 +08:00
|
|
|
return str(self._addon())
|
2020-06-03 11:10:05 +08:00
|
|
|
|
2020-11-07 09:54:45 +08:00
|
|
|
def __expr__(self):
|
2020-11-07 13:30:46 +08:00
|
|
|
return self.addon().__expr__()
|
2020-06-05 16:51:29 +08:00
|
|
|
|
2020-11-07 10:07:32 +08:00
|
|
|
def copy(self):
|
2020-11-07 13:30:46 +08:00
|
|
|
return {k:v for k,v in self._addon().items()}
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
d = self._addon()
|
2020-11-09 02:30:04 +08:00
|
|
|
newd = self.dict_to_dict(d)
|
|
|
|
return newd
|
2020-11-07 13:30:46 +08:00
|
|
|
|
|
|
|
def dict_to_dict(self,dic):
|
|
|
|
d = {}
|
2020-11-09 02:30:04 +08:00
|
|
|
for k,v in dic.items():
|
|
|
|
if isinstance(v,DictObject):
|
2020-11-07 13:30:46 +08:00
|
|
|
d[k] = v.to_dict()
|
|
|
|
elif isinstance(v,dict):
|
|
|
|
d[k] = self.dict_to_dict(v)
|
2020-11-09 02:30:04 +08:00
|
|
|
elif isinstance(v,list):
|
2020-11-07 13:30:46 +08:00
|
|
|
d[k] = self.array_to_dict(v)
|
2020-11-09 02:30:04 +08:00
|
|
|
elif k == '__builtins__':
|
|
|
|
pass
|
|
|
|
elif isbuiltin(v) or isfunction(v) or ismethod(v) or isabstract(v):
|
|
|
|
pass
|
2020-11-07 13:30:46 +08:00
|
|
|
else:
|
|
|
|
d[k] = v
|
|
|
|
return d
|
|
|
|
|
|
|
|
def array_to_dict(self,v):
|
|
|
|
r = []
|
|
|
|
for i in v:
|
|
|
|
if isinstance(i,list):
|
|
|
|
r.append(self.array_to_dict(i))
|
|
|
|
elif isinstance(i,dict):
|
|
|
|
r.append(self.dict_to_dict(i))
|
|
|
|
elif isinstance(i,DictObject):
|
|
|
|
r.append(i.to_dict())
|
2020-11-09 02:30:04 +08:00
|
|
|
elif isbuiltin(i) or isfunction(i) or ismethod(i) or isabstract(i):
|
|
|
|
pass
|
2020-11-07 13:30:46 +08:00
|
|
|
else:
|
|
|
|
r.append(i)
|
|
|
|
return r
|
2020-11-07 10:07:32 +08:00
|
|
|
|
2019-07-16 16:33:07 +08:00
|
|
|
@classmethod
|
|
|
|
def isMe(self,name):
|
|
|
|
return name == 'DictObject'
|
|
|
|
|
2020-06-03 11:10:05 +08:00
|
|
|
def __DOArray(self,a):
|
|
|
|
b = [ self.__DOitem(i) for i in a ]
|
2019-07-16 16:33:07 +08:00
|
|
|
return b
|
|
|
|
|
2020-06-03 11:10:05 +08:00
|
|
|
def __DOitem(self, i):
|
|
|
|
if isinstance(i,DictObject):
|
|
|
|
return i
|
|
|
|
if isinstance(i,dict):
|
2020-07-12 16:03:29 +08:00
|
|
|
i = {k:v for k,v in i.items() if isinstance(k,str)}
|
2020-06-03 11:10:05 +08:00
|
|
|
try:
|
|
|
|
d = DictObject(**i)
|
|
|
|
return d
|
|
|
|
except Exception as e:
|
|
|
|
print("****************",i,"*******dictObject.py")
|
|
|
|
raise e
|
2020-11-07 09:54:45 +08:00
|
|
|
if type(i) == type([]) or type(i) == type(()) :
|
2020-06-03 11:10:05 +08:00
|
|
|
return self.__DOArray(i)
|
2019-07-16 16:33:07 +08:00
|
|
|
return i
|
|
|
|
|
2020-06-08 08:00:42 +08:00
|
|
|
class DictObjectEncoder(JSONEncoder):
|
|
|
|
def default(self, o):
|
2020-11-07 13:30:46 +08:00
|
|
|
return o._addon()
|
|
|
|
|
|
|
|
|
2020-06-08 08:00:42 +08:00
|
|
|
|
2019-07-16 16:33:07 +08:00
|
|
|
def dictObjectFactory(_klassName__,**kwargs):
|
|
|
|
def findSubclass(_klassName__,klass):
|
|
|
|
for k in klass.__subclasses__():
|
|
|
|
if k.isMe(_klassName__):
|
|
|
|
return k
|
|
|
|
k1 = findSubclass(_klassName__,k)
|
|
|
|
if k1 is not None:
|
|
|
|
return k1
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
if _klassName__=='DictObject':
|
|
|
|
return DictObject(**kwargs)
|
|
|
|
k = findSubclass(_klassName__,DictObject)
|
|
|
|
if k is None:
|
|
|
|
return DictObject(**kwargs)
|
|
|
|
return k(**kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
print("dictObjectFactory()",e,_klassName__)
|
|
|
|
raise e
|
2024-04-06 13:43:28 +08:00
|
|
|
|
|
|
|
class DotDict(dict):
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
"""
|
|
|
|
实现点操作符访问字典中的键值对
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.__DOitem(self[attr])
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __setattr__(self, attr, value):
|
|
|
|
"""
|
|
|
|
实现点操作符设置字典中的键值对
|
|
|
|
"""
|
|
|
|
self[attr] = value
|
|
|
|
|
|
|
|
def __DOArray(self,a):
|
|
|
|
b = [ self.__DOitem(i) for i in a ]
|
|
|
|
return b
|
|
|
|
|
|
|
|
def __DOitem(self, i):
|
|
|
|
if isinstance(i,DotDict):
|
|
|
|
return i
|
|
|
|
if isinstance(i,dict):
|
|
|
|
i = {k:v for k,v in i.items() if isinstance(k,str)}
|
|
|
|
try:
|
|
|
|
d = DotDict(**i)
|
|
|
|
return d
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
if type(i) == type([]) or type(i) == type(()) :
|
|
|
|
return self.__DOArray(i)
|
|
|
|
return i
|