Skip to main content

Deep Difference of dictionaries, iterables, strings and other objects. It will recursively look for all the changes.

Project description

DeepDiff v 0.6.1

Deep Difference of dictionaries, iterables, strings and almost any other object. It will recursively look for all the changes.

Parameters

t1A dictionary, list, string or any python object that has __dict__

This is the first item to be compared to the second item

t2dictionary, list, string or almost any python object that has __dict__

The second item is to be compared to the first one

Returns

A DeepDiff object that has already calculated the difference of the 2 items.

Supported data types

int, string, unicode, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple and custom objects!

Examples

Importing
>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
Same object returns empty
>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = t1
>>> print(DeepDiff(t1, t2))
{}
Type of an item has changed
>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = {1:1, 2:"2", 3:3}
>>> print(DeepDiff(t1, t2))
{'type_changes': ["root[2]: 2=<type 'int'> ===> 2=<type 'str'>"]}
Value of an item has changed
>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = {1:1, 2:4, 3:3}
>>> print(DeepDiff(t1, t2))
{'values_changed': ['root[2]: 2 ===> 4']}
Item added and/or removed
>>> 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']}
String difference
>>> 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)
>>> 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!'
String difference 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!
+world
 1
 2
 End
Type change
>>> 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'>"]}
List difference
>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3, 4]}}
>>> 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, 4]"]}
List difference 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"]}
List that contains dictionary:
>>> 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"]}
Sets:
>>> 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]']}
Named Tuples:
>>> from collections import namedtuple
>>> 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']}
Custom objects:
>>> class ClassA(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']}
Object attribute added:
>>> t2.c = "new attribute"
>>> print(DeepDiff(t1, t2))
{'attribute_added': ['root.c'], 'values_changed': ['root.b: 1 ===> 2']}
Ignoring order:
>>> 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))
{}

Changelog

v0-6-1: Fixiing iterables with unhashable when order is ignored v0-6-0: Adding unicode support v0-5-9: Adding decimal support v0-5-8: Adding ignore order of unhashables support v0-5-7: Adding ignore order support v0-5-6: Adding slots support v0-5-5: Adding loop detection

Author Seperman

Github: <https://github.com/seperman> Linkedin: <http://www.linkedin.com/in/sepehr> ZepWorks: <http://www.zepworks.com>

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deepdiff-0.6.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distributions

deepdiff-0.6.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

deepdiff-0.6.1-py2-none-any.whl (9.7 kB view details)

Uploaded Python 2

File details

Details for the file deepdiff-0.6.1.tar.gz.

File metadata

  • Download URL: deepdiff-0.6.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for deepdiff-0.6.1.tar.gz
Algorithm Hash digest
SHA256 303347f99a3a9f1d309b7cc89e85888b6591cb4e1d388bcf23c40183b89fc8aa
MD5 7d9caffedf571af69db442458a705c1d
BLAKE2b-256 836922b33051c86b1cbadce6756938a93f13b87b4c0c60220b9cf9f56998e94f

See more details on using hashes here.

Provenance

File details

Details for the file deepdiff-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for deepdiff-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0fe5a220924541903d1b2e9ecea35e870bc639526de2a30a4be380c6a102014e
MD5 9c3ae982180b6c8b2210f6c9a43d724d
BLAKE2b-256 951f0932ad198d34a9019a1074e31d7e555133ec6c761042a614a8ec97e518c9

See more details on using hashes here.

Provenance

File details

Details for the file deepdiff-0.6.1-py2-none-any.whl.

File metadata

File hashes

Hashes for deepdiff-0.6.1-py2-none-any.whl
Algorithm Hash digest
SHA256 52615dc34184bd4451ab3d96138ff5bbc85ba9658abc0fa3d6f20442fc6d868b
MD5 8e476b591d8653c6a08d04c906e61580
BLAKE2b-256 3f4d688e7dd0140f6969c77fa0a07348607cbad52902ff680b6430a3faf8db53

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page