A fix for diffing using iterable_compare_func with nested objects by dtorres-sf who originally contributed this feature.
Temporarily we are publishing DeepDiff under DeepDiff6 on pypi until further notice.
Note: There are no breaking changes in DeepDiff 6 compared to the latest DeepDiff 5 releases.
Installation
Install from PyPi:
pip install deepdiff6
If you want to use DeepDiff from commandline:
pip install "deepdiff6[cli]"
Note: Prior to DeepDiff 6, it was published under DeepDiff name on pypi.
DeepDiff 6 is being published under DeepDiff6 package name on Pypi temporarily until further notice.
Importing
>>> from deepdiff import DeepDiff # For Deep Difference of 2 objects
>>> from deepdiff import grep, DeepSearch # For finding if item exists in an object
>>> from deepdiff import DeepHash # For hashing objects based on their contents
Note: if you want to use DeepDiff via commandline, make sure to run pip install "deepdiff6[cli]". Then you can access the commands via:
You can also exclude using regular expressions by using exclude_regex_paths and pass a set or list of path regexes to exclude. The items in the list could be raw regex strings or compiled regex objects.
So for example ddiff['dictionary_item_removed'] is a set if strings thus this is called the text view.
The following examples are using the *default text view.*
The Tree View is introduced in DeepDiff v3
and provides traversing capabilities through your diffed data and more!
Read more about the Tree View at the [tree view section](#tree-view) of this page.
Tree View
Starting the version v3 You can choose the view into the deepdiff results.
The tree view provides you with tree objects that you can traverse through to find the parents of the objects that are diffed and the actual objects that are being diffed.
Value of an item has changed (Tree View)
>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = {1:1, 2:4, 3:3}
>>> ddiff_verbose0 = DeepDiff(t1, t2, verbose_level=0, view='tree')
>>> ddiff_verbose0
{'values_changed': {<root[2]>}}
>>>
>>> ddiff_verbose1 = DeepDiff(t1, t2, verbose_level=1, view='tree')
>>> ddiff_verbose1
{'values_changed': {<root[2] t1:2, t2:4>}}
>>> set_of_values_changed = ddiff_verbose1['values_changed']
>>> # since set_of_values_changed includes only one item in a set
>>> # in order to get that one item we can:
>>> (changed,) = set_of_values_changed
>>> changed # Another way to get this is to do: changed=list(set_of_values_changed)[0]
<root[2] t1:2, t2:4>
>>> changed.t1
2
>>> changed.t2
4
>>> # You can traverse through the tree, get to the parents!
>>> changed.up
<root t1:{1: 1, 2: 2,...}, t2:{1: 1, 2: 4,...}>
Serialization
In order to convert the DeepDiff object into a normal Python dictionary, use the to_dict() method.
Note that to_dict will use the text view even if you did the diff in tree view.
DeepDiff comes with a utility to find the path to the item you are looking for.
It is called DeepSearch and it has a similar interface to DeepDiff.
Let's say you have a huge nested object and want to see if any item with the word somewhere exists in it.
Just grep through your objects as you would in shell!
DeepHash is designed to give you hash of ANY python object based on its contents even if the object is not considered hashable!
DeepHash is supposed to be deterministic in order to make sure 2 objects that contain the same data, produce the same hash.
So what is exactly the hash of obj in this case?
DeepHash is calculating the hash of the obj and any other object that obj contains.
The output of DeepHash is a dictionary of object IDs to their hashes.
In order to get the hash of obj itself, you need to use the object (or the id of object) to get its hash:
At first it might seem weird why DeepHash(obj)[obj] but remember that DeepHash(obj) is a dictionary of hashes of all other objects that obj contains too.
result is the output of the function that is being tests.
expected is the expected output of the function.
self.assertEqual(DeepDiff(expected, result), {})
or if you are using Pytest:
assert not DeepDiff(expected, result)
In other words, assert that there is no diff between the expected and the result.
Difference with Json Patch
Unlike Json Patch which is designed only for Json objects, DeepDiff is designed specifically for almost all Python types. In addition to that, DeepDiff checks for type changes and attribute value changes that Json Patch does not cover since there are no such things in Json. Last but not least, DeepDiff gives you the exact path of the item(s) that were changed in Python syntax.
I was honored to give a talk about the basics of how DeepDiff does what it does at Pycon 2016. Please check out the video and let me know what you think:
Please make sure that your PR has tests. Since DeepDiff is used in many sensitive data driven projects, we strive to maintain around 100% test coverage on the code.
Please run pytest --cov=deepdiff --runslow to see the coverage report. Note that the --runslow flag will run some slow tests too. In most cases you only want to run the fast tests which so you wont add the --runslow flag.
Or to see a more user friendly version, please run: pytest --cov=deepdiff --cov-report term-missing --runslow.
Thank you!
Citing
How to cite this library (APA style):
Dehpour, S. (2022). DeepDiff (Version 6.2.0) [Software]. Available from https://github.com/seperman/deepdiff.