词典、词典、字符串和其他对象的巨大差异。它将递归地查找所有更改。
pivotal-deepdiff的Python项目详细描述
深度差V 0.6.1
词典、词典、字符串和其他对象的巨大差异。它将递归地查找所有更改。 在Python2.7和3.4上测试
注意:
this fork提供了专门处理浮点相关字段中舍入的更改
安装
从pypi安装:
pip install deepdiff
导入
>>>fromdeepdiffimportDeepDiff
支持的数据类型
int,string,dictionary,list,tuple,set,frozenset,ordereddict,namedtuple和自定义对象!
示例
同一对象返回空值
>>>t1={1:1,2:2,3:3}>>>t2=t1>>>ddiff=DeepDiff(t1,t2)>>>ddiff{}
项目类型已更改
>>>t1={1:1,2:2,3:3}>>>t2={1:1,2:"2",3:3}>>>ddiff=DeepDiff(t1,t2)>>>print(ddiff){'type_changes':["root[2]: 2=<type 'int'> ===> 2=<type 'str'>"]}
项目值已更改
>>>t1={1:1,2:2,3:3}>>>t2={1:1,2:4,3:3}>>>ddiff=DeepDiff(t1,t2)>>>print(ddiff){'values_changed':['root[2]: 2 ===> 4']}
添加和/或删除的项目
>>>t1={1:1,2:2,3:3,4:4}>>>t2={1:1,2:4,3:3,5:5,6:6}>>>ddiff=DeepDiff(t1,t2)>>>pprint(ddiff){'dic_item_added':['root[5, 6]'],'dic_item_removed':['root[4]'],'values_changed':['root[2]: 2 ===> 4']}
字符串差异
>>>t1={1:1,2:2,3:3,4:{"a":"hello","b":"world"}}>>>t2={1:1,2:4,3:3,4:{"a":"hello","b":"world!"}}>>>ddiff=DeepDiff(t1,t2)>>>frompprintimportpprint>>>pprint(ddiff,indent=2){'values_changed':['root[2]: 2 ===> 4',"root[4]['b']: 'world' ===> 'world!'"]}>>>>>>print(ddiff['values_changed'][1])root[4]['b']:'world'===>'world!'
字符串差异2
>>>t1={1:1,2:2,3:3,4:{"a":"hello","b":"world!\nGoodbye!\n1\n2\nEnd"}}>>>t2={1:1,2:2,3:3,4:{"a":"hello","b":"world\n1\n2\nEnd"}}>>>ddiff=DeepDiff(t1,t2)>>>pprint(ddiff,indent=2){'values_changed':["root[4]['b']:\n"'--- \n''+++ \n''@@ -1,5 +1,4 @@\n''-world!\n''-Goodbye!\n''+world\n'' 1\n'' 2\n'' End']}>>>>>>print(ddiff['values_changed'][0])root[4]['b']:---+++@@-1,5+1,4@@-world!-Goodbye!+world12End
类型更改
>>>t1={1:1,2:2,3:3,4:{"a":"hello","b":[1,2,3]}}>>>t2={1:1,2:2,3:3,4:{"a":"hello","b":"world\n\n\nEnd"}}>>>ddiff=DeepDiff(t1,t2)>>>pprint(ddiff,indent=2){'type_changes':["root[4]['b']: [1, 2, 3]=<type 'list'> ===> world\n"'\n''\n'"End=<type 'str'>"]}
列表差异
>>>t1={1:1,2:2,3:3,4:{"a":"hello","b":[1,2,3]}}>>>t2={1:1,2:2,3:3,4:{"a":"hello","b":[1,2]}}>>>ddiff=DeepDiff(t1,t2)>>>pprint(ddiff,indent=2){'iterable_item_removed':["root[4]['b']: [3]"]}
列表差异2
>>>t1={1:1,2:2,3:3,4:{"a":"hello","b":[1,2,3]}}>>>t2={1:1,2:2,3:3,4:{"a":"hello","b":[1,3,2,3]}}>>>ddiff=DeepDiff(t1,t2)>>>pprint(ddiff,indent=2){'iterable_item_added':["root[4]['b']: [3]"],'values_changed':["root[4]['b'][1]: 2 ===> 3","root[4]['b'][2]: 3 ===> 2"]}
包含字典的列表:
>>>t1={1:1,2:2,3:3,4:{"a":"hello","b":[1,2,{1:1,2:2}]}}>>>t2={1:1,2:2,3:3,4:{"a":"hello","b":[1,2,{1:3}]}}>>>ddiff=DeepDiff(t1,t2)>>>pprint(ddiff,indent=2){'dic_item_removed':["root[4]['b'][2][2]"],'values_changed':["root[4]['b'][2][1]: 1 ===> 3"]}
设置
>>>t1={1,2,8}>>>t2={1,2,3,5}>>>ddiff=DeepDiff(t1,t2)>>>print(DeepDiff(t1,t2)){'set_item_added':['root: [3, 5]'],'set_item_removed':['root: [8]']}
命名元组:
>>>fromcollectionsimportnamedtuple>>>Point=namedtuple('Point',['x','y'])>>>t1=Point(x=11,y=22)>>>t2=Point(x=11,y=23)>>>print(DeepDiff(t1,t2)){'values_changed':['root.y: 22 ===> 23']}
自定义对象:
>>>classClassA(object):...a=1...def__init__(self,b):...self.b=b...>>>t1=ClassA(1)>>>t2=ClassA(2)>>>>>>print(DeepDiff(t1,t2)){'values_changed':['root.b: 1 ===> 2']}
添加的对象属性:
>>>t2.c="new attribute">>>print(DeepDiff(t1,t2)){'attribute_added':['root.c'],'values_changed':['root.b: 1 ===> 2']}
忽略顺序:
注意:如果您的对象包含包含任何不可更改项的iterable,忽略该顺序可能会很昂贵。
>>>t1=[{"a":2},{"b":[3,4,{1:1}]}]>>>t2=[{"b":[3,4,{1:1}]},{"a":2}]ddiff=DeepDiff(t1,t2,ignore_order=True)>>>>>>print(DeepDiff(t1,t2)){}
文档
http://deepdiff.readthedocs.org/en/latest/
构建过程:
- 更新应用程序内部的
__version_info__
。承诺和推动。 - 用版本标记发布。
git tag <version> -m "Release"; git push --tags
- 构建版本
rm -rf dist build *egg-info; python setup.py sdist bdist_wheel
- 上传数据
twine upload dist/*
更改日志
原作者
塞伯曼 Github:https://github.com/seperman LinkedIn:http://www.linkedin.com/in/sepehr 玉米加工厂:http://www.zepworks.com
感谢: 初始py3移植的brbsix 支持Unicode的Wangfenjin