| 1 |
:doc:`/index` |
| 2 |
|
| 3 |
.. _exclude_paths_label: |
| 4 |
|
| 5 |
Exclude Paths |
| 6 |
============= |
| 7 |
|
| 8 |
Exclude part of your object tree from comparison. |
| 9 |
use exclude_paths and pass a set or list of paths to exclude, if only one item is being passed, then just put it there as a string. No need to pass it as a list then. |
| 10 |
|
| 11 |
Example |
| 12 |
>>> t1 = {"for life": "vegan", "ingredients": ["no meat", "no eggs", "no dairy"]} |
| 13 |
>>> t2 = {"for life": "vegan", "ingredients": ["veggies", "tofu", "soy sauce"]} |
| 14 |
>>> print (DeepDiff(t1, t2, exclude_paths="root['ingredients']")) # one item pass it as a string |
| 15 |
{} |
| 16 |
>>> print (DeepDiff(t1, t2, exclude_paths=["root['ingredients']", "root['ingredients2']"])) # multiple items pass as a list or a set. |
| 17 |
{} |
| 18 |
|
| 19 |
Also for root keys you don't have to pass as "root['key']". You can instead just pass the key: |
| 20 |
|
| 21 |
Example |
| 22 |
>>> t1 = {"for life": "vegan", "ingredients": ["no meat", "no eggs", "no dairy"]} |
| 23 |
>>> t2 = {"for life": "vegan", "ingredients": ["veggies", "tofu", "soy sauce"]} |
| 24 |
>>> print (DeepDiff(t1, t2, exclude_paths="ingredients")) # one item pass it as a string |
| 25 |
{} |
| 26 |
>>> print (DeepDiff(t1, t2, exclude_paths=["ingredients", "ingredients2"])) # multiple items pass as a list or a set. |
| 27 |
{} |
| 28 |
|
| 29 |
|
| 30 |
.. _include_paths_label: |
| 31 |
|
| 32 |
Include Paths |
| 33 |
============= |
| 34 |
|
| 35 |
Only include this part of your object tree in the comparison. |
| 36 |
Use include_paths and pass a set or list of paths to limit diffing to only those paths. If only one item is being passed, just put it there as a string—no need to pass it as a list then. |
| 37 |
|
| 38 |
Example |
| 39 |
>>> t1 = {"for life": "vegan", "ingredients": ["no meat", "no eggs", "no dairy"]} |
| 40 |
>>> t2 = {"for life": "vegan", "ingredients": ["veggies", "tofu", "soy sauce"]} |
| 41 |
>>> print (DeepDiff(t1, t2, include_paths="root['for life']")) # one item pass it as a string |
| 42 |
{} |
| 43 |
>>> print (DeepDiff(t1, t2, include_paths=["for life", "ingredients2"])) # multiple items pass as a list or a set and you don't need to pass the full path when dealing with root keys. So instead of "root['for life']" you can pass "for life" |
| 44 |
{} |
| 45 |
|
| 46 |
|
| 47 |
When passing include_paths, all the children of that path will be included too. |
| 48 |
|
| 49 |
Example |
| 50 |
>>> t1 = { |
| 51 |
... "foo": {"bar": "potato"}, |
| 52 |
... "ingredients": ["no meat", "no eggs", "no dairy"] |
| 53 |
... } |
| 54 |
>>> t2 = { |
| 55 |
... "foo": {"bar": "banana"}, |
| 56 |
... "ingredients": ["bread", "cheese"] |
| 57 |
... } |
| 58 |
>>> DeepDiff(t1, t2, include_paths="foo") |
| 59 |
{'values_changed': {"root['foo']['bar']": {'new_value': 'banana', 'old_value': 'potato'}}} |
| 60 |
|
| 61 |
|
| 62 |
.. _exclude_regex_paths_label: |
| 63 |
|
| 64 |
Exclude Regex Paths |
| 65 |
------------------- |
| 66 |
|
| 67 |
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. |
| 68 |
>>> import re |
| 69 |
>>> t1 = [{'a': 1, 'b': 2}, {'c': 4, 'b': 5}] |
| 70 |
>>> t2 = [{'a': 1, 'b': 3}, {'c': 4, 'b': 5}] |
| 71 |
>>> print(DeepDiff(t1, t2, exclude_regex_paths=r"root\[\d+\]\['b'\]")) |
| 72 |
{} |
| 73 |
>>> exclude_path = re.compile(r"root\[\d+\]\['b'\]") |
| 74 |
>>> print(DeepDiff(t1, t2, exclude_regex_paths=[exclude_path])) |
| 75 |
{} |
| 76 |
|
| 77 |
example 2: |
| 78 |
>>> t1 = {'a': [1, 2, [3, {'foo1': 'bar'}]]} |
| 79 |
>>> t2 = {'a': [1, 2, [3, {'foo2': 'bar'}]]} |
| 80 |
>>> DeepDiff(t1, t2, exclude_regex_paths="\['foo.'\]") # since it is one item in exclude_regex_paths, you don't have to put it in a list or a set. |
| 81 |
{} |
| 82 |
|
| 83 |
Tip: DeepDiff is using re.search on the path. So if you want to force it to match from the beginning of the path, add `^` to the beginning of regex. |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
Back to :doc:`/index` |