common_dict_compare

上一篇 / 下一篇  2017-07-21 02:39:42 / 个人分类:utile

def dict_compare(value1, value2, level=0, skip='Not Care', result='Pass'):
resultF = result
flag = True
if type(value1) == type(value2):
if isinstance(value1,int) or isinstance(value1,str) or isinstance(value1,tuple):
if value1 == value2:
print("--"*level+"{}: {} --- pass".format(level, value1))
else:
flag = False
resultF = 'Fail'
print("--"*level+"{}: value is different, one is {} another is {} --- fail".format(level, value1, value2))
elif isinstance(value1,list):
if len(value1) != len(value2):
flag = False
resultF = 'Fail'
print("--"*level+"{}: list length is different,one is {} another is {} --- fail".format(level, len(value1), len(value2)))
else:
for list_ele1,list_ele2 in zip(value1,value2):
flag, resultF = parse_compare(list_ele1,list_ele2,level+1,result = resultF)
elif isinstance(value1,dict):
if value1 == skip or value2 == skip:
print("--"*level+"{}: this value can be skip, no need to check".format(level))
else:
if len(value1) != len(value2):
flag = False
resultF = 'Fail'
print("--"*level+"{}: dict items number is different,one is {} another is {} --- fail".format(level, len(value1), len(value2)))
elif set(value1.keys())-set(value2.keys()):
flag = False
resultF = 'Fail'
print("--"*level+"{}: dict1 keys is more than dict2 keys, keys are {} --- fail".format(level, set(value1.keys())-set(value2.keys())))
elif set(value2.keys())-set(value1.keys()):
flag = False
resultF = 'Fail'
print("--"*level+"{}: dict2 keys is more than dict1 keys, keys are {} --- fail".format(level, set(value2.keys())-set(value1.keys())))
elif set.intersection(set(value1.keys()),set(value2.keys())):
for key in list(set.intersection(set(value1.keys()),set(value2.keys()))):
flag, resultF = parse_compare(value1[key],value2[key],level+1,result = resultF)
else:
print("--"*level+"{}: not sure what's the condition, {} vs {} --- fail".format(level, value1, value2))
else:
flag = False
resultF = 'Fail'
print("--"*level+"{}: Type is different, one is {} another is {} --- fail".format(level, type(value1), type(value2)))
return flag, resultF

a = {'a':1,'b':2,'c':(1,2,3,'5'),'d':['a','c',{'d':[1,2,3],'e':('s',1,'ty')}],'f':'fdsa'}
b = {'a':1,'d':['a','c',{'d':[1,2,3],'e':('s',1,'ty')}],'b':2,'c':(1,2,3,'5'),'f':'fdsa'}
c = {'a':1,'b':2,'c':(1,2,3,'5'),'d':['a','c',{'d':[1,2,3,'4'],'e':('s',1,{'s':'fds'},'ty')}],'f':'Not Care'}

print(parse_compare(a,b))
print(parse_compare(b,c))

TAG: util

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar