apppublic/appPublic/dictObject.py

111 lines
2.2 KiB
Python
Raw Normal View History

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-06-03 11:10:05 +08:00
class DictObject:
def __init__(self,**kw):
2020-06-05 09:55:33 +08:00
self.__dict__['_kwargs'] = {}
2020-06-07 21:23:50 +08:00
print('__dict__=',self.__dict__)
2020-06-03 11:10:05 +08:00
for k,v in kw.items():
2020-06-05 09:55:33 +08:00
self._kwargs.update({k:self.__DOitem(v)})
2020-06-03 11:10:05 +08:00
def __getattr__(self, name):
x = self.__dict__.get(name,None)
if x:
return x
2020-06-05 09:55:33 +08:00
b = self.__dict__.get('_kwargs',None)
2020-06-03 11:10:05 +08:00
if not b:
2020-06-05 09:55:33 +08:00
print('Error:_kwargs not in __dict__')
raise Exception('_kwargs not in __dict__')
2020-06-03 11:10:05 +08:00
return b.get(name,None)
2020-06-07 20:34:31 +08:00
def __setattr__(self,name,value):
self.__setitem__(name,value)
2020-06-03 11:10:05 +08:00
def __getitem__(self,name):
x = self.__dict__.get(name,None)
if x is not None:
return x
2020-06-05 09:55:33 +08:00
x = self._kwargs.get(name,None)
2020-06-03 11:10:05 +08:00
return x
def __setitem__(self,name,value):
2020-06-05 09:55:33 +08:00
self._kwargs[name] = value
2020-06-03 11:10:05 +08:00
2020-06-05 09:54:55 +08:00
def __delitem__(self,name):
2020-06-05 09:55:33 +08:00
self._kwargs.pop(name)
2020-06-03 11:10:05 +08:00
2020-06-05 09:54:55 +08:00
def get(self,name,dv=None):
2020-06-05 09:55:33 +08:00
return self._kwargs.get(name,dv)
2020-06-05 09:54:55 +08:00
2020-06-03 11:10:05 +08:00
def copy(self):
2020-06-05 09:55:33 +08:00
return self._kwargs.copy()
2020-06-03 11:10:05 +08:00
def update(self,d):
2020-06-05 09:55:33 +08:00
self._kwargs.update(d)
2020-06-03 11:10:05 +08:00
def keys(self):
2020-06-05 09:55:33 +08:00
return self._kwargs.keys()
2020-06-03 11:10:05 +08:00
def items(self):
2020-06-05 09:55:33 +08:00
return self._kwargs.items()
2020-06-03 11:10:05 +08:00
2020-06-05 16:51:29 +08:00
def __expr__(self):
return self._kwargs.__expr__()
def __str__(self):
return self._kwargs.__str__()
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):
try:
d = DictObject(**i)
return d
except Exception as e:
print("****************",i,"*******dictObject.py")
raise e
2019-07-16 16:33:07 +08:00
if type(i) is type([]):
2020-06-03 11:10:05 +08:00
return self.__DOArray(i)
2019-07-16 16:33:07 +08:00
return i
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