| 1 |
:doc:`/index` |
| 2 |
|
| 3 |
.. _ignore_order_label: |
| 4 |
|
| 5 |
Ignore Order |
| 6 |
============ |
| 7 |
|
| 8 |
DeepDiff by default compares objects in the order that it iterates through them in iterables. |
| 9 |
In other words if you have 2 lists, then the first item of the lists are compared to each other, then the 2nd items and so on. |
| 10 |
That makes DeepDiff be able to run in linear time. |
| 11 |
|
| 12 |
However, there are often times when you don't care about the order in which the items have appeared. |
| 13 |
In such cases DeepDiff needs to do way more work in order to find the differences. |
| 14 |
|
| 15 |
There are a couple of parameters provided to you to have full control over. |
| 16 |
|
| 17 |
|
| 18 |
List difference with ignore_order=False which is the default: |
| 19 |
>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}} |
| 20 |
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}} |
| 21 |
>>> ddiff = DeepDiff(t1, t2) |
| 22 |
>>> pprint (ddiff, indent = 2) |
| 23 |
{ 'iterable_item_added': {"root[4]['b'][3]": 3}, |
| 24 |
'values_changed': { "root[4]['b'][1]": {'new_value': 3, 'old_value': 2}, |
| 25 |
"root[4]['b'][2]": {'new_value': 2, 'old_value': 3}}} |
| 26 |
|
| 27 |
Ignore Order |
| 28 |
------------ |
| 29 |
|
| 30 |
List difference ignoring order or duplicates: (with the same dictionaries as above) |
| 31 |
>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}} |
| 32 |
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}} |
| 33 |
>>> ddiff = DeepDiff(t1, t2, ignore_order=True) |
| 34 |
>>> print (ddiff) |
| 35 |
{} |
| 36 |
|
| 37 |
.. _ignore_order_func_label: |
| 38 |
|
| 39 |
Dynamic Ignore Order |
| 40 |
-------------------- |
| 41 |
|
| 42 |
Sometimes single *ignore_order* parameter is not enough to do a diff job, |
| 43 |
you can use *ignore_order_func* to determine whether the order of certain paths should be ignored |
| 44 |
|
| 45 |
List difference ignoring order with *ignore_order_func* |
| 46 |
>>> t1 = {"set": [1,2,3], "list": [1,2,3]} |
| 47 |
>>> t2 = {"set": [3,2,1], "list": [3,2,1]} |
| 48 |
>>> ddiff = DeepDiff(t1, t2, ignore_order_func=lambda level: "set" in level.path()) |
| 49 |
>>> print (ddiff) |
| 50 |
{ 'values_changed': { "root['list'][0]": {'new_value': 3, 'old_value': 1}, |
| 51 |
"root['list'][2]": {'new_value': 1, 'old_value': 3}}} |
| 52 |
|
| 53 |
|
| 54 |
Ignoring order when certain word in the path |
| 55 |
>>> from deepdiff import DeepDiff |
| 56 |
>>> t1 = {'a': [1, 2], 'b': [3, 4]} |
| 57 |
>>> t2 = {'a': [2, 1], 'b': [4, 3]} |
| 58 |
>>> DeepDiff(t1, t2, ignore_order=True) |
| 59 |
{} |
| 60 |
>>> def ignore_order_func(level): |
| 61 |
... return 'a' in level.path() |
| 62 |
... |
| 63 |
>>> DeepDiff(t1, t2, ignore_order=True, ignore_order_func=ignore_order_func) |
| 64 |
{'values_changed': {"root['b'][0]": {'new_value': 4, 'old_value': 3}, "root['b'][1]": {'new_value': 3, 'old_value': 4}}} |
| 65 |
|
| 66 |
|
| 67 |
.. _report_repetition_label: |
| 68 |
|
| 69 |
Reporting Repetitions |
| 70 |
--------------------- |
| 71 |
|
| 72 |
List difference ignoring order and reporting repetitions: |
| 73 |
>>> from deepdiff import DeepDiff |
| 74 |
>>> from pprint import pprint |
| 75 |
>>> t1 = [1, 3, 1, 4] |
| 76 |
>>> t2 = [4, 4, 1] |
| 77 |
>>> ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True) |
| 78 |
>>> pprint(ddiff, indent=2) |
| 79 |
{ 'iterable_item_removed': {'root[1]': 3}, |
| 80 |
'repetition_change': { 'root[0]': { 'new_indexes': [2], |
| 81 |
'new_repeat': 1, |
| 82 |
'old_indexes': [0, 2], |
| 83 |
'old_repeat': 2, |
| 84 |
'value': 1}, |
| 85 |
'root[3]': { 'new_indexes': [0, 1], |
| 86 |
'new_repeat': 2, |
| 87 |
'old_indexes': [3], |
| 88 |
'old_repeat': 1, |
| 89 |
'value': 4}}} |
| 90 |
|
| 91 |
.. _max_passes_label: |
| 92 |
|
| 93 |
Max Passes |
| 94 |
---------- |
| 95 |
|
| 96 |
max_passes: Integer, default = 10000000 |
| 97 |
Maximum number of passes to run on objects to pin point what exactly is different. This is only used when ignore_order=True |
| 98 |
|
| 99 |
If you have deeply nested objects, DeepDiff needs to run multiple passes in order to pin point the difference. |
| 100 |
That can dramatically increase the time spent to find the difference. |
| 101 |
You can control the maximum number of passes that can be run via the max_passes parameter. |
| 102 |
|
| 103 |
.. note:: |
| 104 |
The definition of pass is whenever 2 iterable objects are being compared with each other and deepdiff decides to compare every single element of one iterable with every single element of the other iterable. |
| 105 |
Refer to :ref:`cutoff_distance_for_pairs_label` and :ref:`cutoff_intersection_for_pairs_label` for more info on how DeepDiff decides to start a new pass. |
| 106 |
|
| 107 |
Max Passes Example |
| 108 |
>>> from pprint import pprint |
| 109 |
>>> from deepdiff import DeepDiff |
| 110 |
>>> |
| 111 |
>>> t1 = [ |
| 112 |
... { |
| 113 |
... 'key3': [[[[[1, 2, 4, 5]]]]], |
| 114 |
... 'key4': [7, 8], |
| 115 |
... }, |
| 116 |
... { |
| 117 |
... 'key5': 'val5', |
| 118 |
... 'key6': 'val6', |
| 119 |
... }, |
| 120 |
... ] |
| 121 |
>>> |
| 122 |
>>> t2 = [ |
| 123 |
... { |
| 124 |
... 'key5': 'CHANGE', |
| 125 |
... 'key6': 'val6', |
| 126 |
... }, |
| 127 |
... { |
| 128 |
... 'key3': [[[[[1, 3, 5, 4]]]]], |
| 129 |
... 'key4': [7, 8], |
| 130 |
... }, |
| 131 |
... ] |
| 132 |
>>> |
| 133 |
>>> for max_passes in (1, 2, 62, 65): |
| 134 |
... diff = DeepDiff(t1, t2, ignore_order=True, max_passes=max_passes, verbose_level=2) |
| 135 |
... print('-\n----- Max Passes = {} -----'.format(max_passes)) |
| 136 |
... pprint(diff) |
| 137 |
... |
| 138 |
DeepDiff has reached the max number of passes of 1. You can possibly get more accurate results by increasing the max_passes parameter. |
| 139 |
- |
| 140 |
----- Max Passes = 1 ----- |
| 141 |
{'values_changed': {'root[0]': {'new_value': {'key5': 'CHANGE', 'key6': 'val6'}, |
| 142 |
'old_value': {'key3': [[[[[1, 2, 4, 5]]]]], |
| 143 |
'key4': [7, 8]}}, |
| 144 |
'root[1]': {'new_value': {'key3': [[[[[1, 3, 5, 4]]]]], |
| 145 |
'key4': [7, 8]}, |
| 146 |
'old_value': {'key5': 'val5', 'key6': 'val6'}}}} |
| 147 |
DeepDiff has reached the max number of passes of 2. You can possibly get more accurate results by increasing the max_passes parameter. |
| 148 |
- |
| 149 |
----- Max Passes = 2 ----- |
| 150 |
{'values_changed': {"root[0]['key3'][0]": {'new_value': [[[[1, 3, 5, 4]]]], |
| 151 |
'old_value': [[[[1, 2, 4, 5]]]]}, |
| 152 |
"root[1]['key5']": {'new_value': 'CHANGE', |
| 153 |
'old_value': 'val5'}}} |
| 154 |
DeepDiff has reached the max number of passes of 62. You can possibly get more accurate results by increasing the max_passes parameter. |
| 155 |
- |
| 156 |
----- Max Passes = 62 ----- |
| 157 |
{'values_changed': {"root[0]['key3'][0][0][0][0]": {'new_value': [1, 3, 5, 4], |
| 158 |
'old_value': [1, 2, 4, 5]}, |
| 159 |
"root[1]['key5']": {'new_value': 'CHANGE', |
| 160 |
'old_value': 'val5'}}} |
| 161 |
DeepDiff has reached the max number of passes of 65. You can possibly get more accurate results by increasing the max_passes parameter. |
| 162 |
- |
| 163 |
----- Max Passes = 65 ----- |
| 164 |
{'values_changed': {"root[0]['key3'][0][0][0][0][1]": {'new_value': 3, |
| 165 |
'old_value': 2}, |
| 166 |
"root[1]['key5']": {'new_value': 'CHANGE', |
| 167 |
'old_value': 'val5'}}} |
| 168 |
|
| 169 |
|
| 170 |
.. note:: |
| 171 |
If there are potential passes left to be run and the max_passes value is reached, DeepDiff will issue a warning. |
| 172 |
However the most accurate result might have already been found when there are still potential passes left to be run. |
| 173 |
|
| 174 |
For example in the above example at max_passes=64, DeepDiff finds the optimal result however it has one more pass |
| 175 |
to go before it has run all the potential passes. Hence just for the sake of example we are using max_passes=65 |
| 176 |
as an example of a number that doesn't issue warnings. |
| 177 |
|
| 178 |
.. note:: |
| 179 |
Also take a look at :ref:`max_passes_label` |
| 180 |
|
| 181 |
.. _cutoff_distance_for_pairs_label: |
| 182 |
|
| 183 |
Cutoff Distance For Pairs |
| 184 |
------------------------- |
| 185 |
|
| 186 |
cutoff_distance_for_pairs : 1 >= float >= 0, default=0.3 |
| 187 |
What is the threshold to consider 2 items as potential pairs. |
| 188 |
Note that it is only used when ignore_order = True. |
| 189 |
|
| 190 |
cutoff_distance_for_pairs in combination with :ref:`cutoff_intersection_for_pairs_label` are the parameters that decide whether 2 objects to be paired with each other during ignore_order=True algorithm or not. Note that these parameters are mainly used for nested iterables. |
| 191 |
|
| 192 |
For example by going from the default of cutoff_distance_for_pairs=0.3 to 0.1, we have essentially disallowed the 1.0 and 20.0 to be paired with each other. As you can see, DeepDiff has decided that the :ref:`deep_distance_label` of 1.0 and 20.0 to be around 0.27. Since that is way above cutoff_distance_for_pairs of 0.1, the 2 items are not paired. As a result the lists containing the 2 numbers are directly compared with each other: |
| 193 |
|
| 194 |
>>> from deepdiff import DeepDiff |
| 195 |
>>> t1 = [[1.0]] |
| 196 |
>>> t2 = [[20.0]] |
| 197 |
>>> DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.3) |
| 198 |
{'values_changed': {'root[0][0]': {'new_value': 20.0, 'old_value': 1.0}}} |
| 199 |
>>> DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=0.1) |
| 200 |
{'values_changed': {'root[0]': {'new_value': [20.0], 'old_value': [1.0]}}} |
| 201 |
>>> DeepDiff(1.0, 20.0, get_deep_distance=True) |
| 202 |
{'values_changed': {'root': {'new_value': 20.0, 'old_value': 1.0}}, 'deep_distance': 0.2714285714285714} |
| 203 |
|
| 204 |
|
| 205 |
.. _cutoff_intersection_for_pairs_label: |
| 206 |
|
| 207 |
Cutoff Intersection For Pairs |
| 208 |
----------------------------- |
| 209 |
|
| 210 |
cutoff_intersection_for_pairs : 1 >= float >= 0, default=0.7 |
| 211 |
What is the threshold to calculate pairs of items between 2 iterables. |
| 212 |
For example 2 iterables that have nothing in common, do not need their pairs to be calculated. |
| 213 |
Note that it is only used when ignore_order = True. |
| 214 |
|
| 215 |
Behind the scene DeepDiff takes the :ref:`deep_distance_label` of objects when running ignore_order=True. |
| 216 |
The distance is between zero and 1. |
| 217 |
A distance of zero means the items are equal. A distance of 1 means they are 100% different. |
| 218 |
When comparing iterables, the cutoff_intersection_for_pairs is used to decide whether to compare every single item in each iterable |
| 219 |
with every single item in the other iterable or not. If the distance between the 2 iterables is equal or bigger than the |
| 220 |
cutoff_intersection_for_pairs, then the 2 iterables items are only compared as added or removed items and NOT modified items. |
| 221 |
However, if the distance between 2 iterables is below the cutoff, every single item from each iterable will be compared to every |
| 222 |
single item from the other iterable to find the closest "pair" of each item. |
| 223 |
|
| 224 |
.. note:: |
| 225 |
The process of comparing every item to the other is very expensive so :ref:`cutoff_intersection_for_pairs_label` in combination with :ref:`cutoff_distance_for_pairs_label` is used to give acceptable results with much higher speed. |
| 226 |
|
| 227 |
With a low cutoff_intersection_for_pairs, the 2 iterables above will be considered too |
| 228 |
far off from each other to get the individual pairs of items. |
| 229 |
So numbers that are not only related to each other via their positions in the lists |
| 230 |
and not their values are paired together in the results. |
| 231 |
|
| 232 |
>>> t1 = [1.0, 2.0, 3.0, 4.0, 5.0] |
| 233 |
>>> t2 = [5.0, 3.01, 1.2, 2.01, 4.0] |
| 234 |
>>> |
| 235 |
>>> DeepDiff(t1, t2, ignore_order=True, cutoff_intersection_for_pairs=0.1) |
| 236 |
{'values_changed': {'root[1]': {'new_value': 3.01, 'old_value': 2.0}, 'root[2]': {'new_value': 1.2, 'old_value': 3.0}}, 'iterable_item_added': {'root[3]': 2.01}, 'iterable_item_removed': {'root[0]': 1.0}} |
| 237 |
|
| 238 |
With the cutoff_intersection_for_pairs of 0.7 (which is the default value), |
| 239 |
the 2 iterables will be considered close enough to get pairs of items between the 2. |
| 240 |
So 2.0 and 2.01 are paired together for example. |
| 241 |
|
| 242 |
>>> t1 = [1.0, 2.0, 3.0, 4.0, 5.0] |
| 243 |
>>> t2 = [5.0, 3.01, 1.2, 2.01, 4.0] |
| 244 |
>>> |
| 245 |
>>> DeepDiff(t1, t2, ignore_order=True, cutoff_intersection_for_pairs=0.7) |
| 246 |
{'values_changed': {'root[2]': {'new_value': 3.01, 'old_value': 3.0}, 'root[0]': {'new_value': 1.2, 'old_value': 1.0}, 'root[1]': {'new_value': 2.01, 'old_value': 2.0}}} |
| 247 |
|
| 248 |
|
| 249 |
As an example of how much this parameter can affect the results in deeply nested objects, please take a look at :ref:`distance_and_diff_granularity_label`. |
| 250 |
|
| 251 |
|
| 252 |
.. _iterable_compare_func_label2: |
| 253 |
|
| 254 |
Iterable Compare Func |
| 255 |
--------------------- |
| 256 |
|
| 257 |
New in DeepDiff 5.5.0 |
| 258 |
|
| 259 |
There are times that we want to guide DeepDiff as to what items to compare with other items. In such cases we can pass a `iterable_compare_func` that takes a function pointer to compare two items. The function takes three parameters (x, y, level) and should return `True` if it is a match, `False` if it is not a match or raise `CannotCompare` if it is unable to compare the two. |
| 260 |
|
| 261 |
|
| 262 |
For example take the following objects: |
| 263 |
|
| 264 |
>>> from deepdiff import DeepDiff |
| 265 |
>>> from deepdiff.helper import CannotCompare |
| 266 |
>>> |
| 267 |
>>> t1 = [ |
| 268 |
... { |
| 269 |
... 'id': 1, |
| 270 |
... 'value': [1] |
| 271 |
... }, |
| 272 |
... { |
| 273 |
... 'id': 2, |
| 274 |
... 'value': [7, 8, 1] |
| 275 |
... }, |
| 276 |
... { |
| 277 |
... 'id': 3, |
| 278 |
... 'value': [7, 8], |
| 279 |
... }, |
| 280 |
... ] |
| 281 |
>>> |
| 282 |
>>> t2 = [ |
| 283 |
... { |
| 284 |
... 'id': 2, |
| 285 |
... 'value': [7, 8] |
| 286 |
... }, |
| 287 |
... { |
| 288 |
... 'id': 3, |
| 289 |
... 'value': [7, 8, 1], |
| 290 |
... }, |
| 291 |
... { |
| 292 |
... 'id': 1, |
| 293 |
... 'value': [1] |
| 294 |
... }, |
| 295 |
... ] |
| 296 |
>>> |
| 297 |
>>> DeepDiff(t1, t2, ignore_order=True) |
| 298 |
{'values_changed': {"root[2]['id']": {'new_value': 2, 'old_value': 3}, "root[1]['id']": {'new_value': 3, 'old_value': 2}}} |
| 299 |
|
| 300 |
|
| 301 |
Now let's define a compare_func that takes 3 parameters: x, y and level. |
| 302 |
|
| 303 |
>>> def compare_func(x, y, level=None): |
| 304 |
... try: |
| 305 |
... return x['id'] == y['id'] |
| 306 |
... except Exception: |
| 307 |
... raise CannotCompare() from None |
| 308 |
... |
| 309 |
>>> DeepDiff(t1, t2, ignore_order=True, iterable_compare_func=compare_func) |
| 310 |
{'iterable_item_added': {"root[2]['value'][2]": 1}, 'iterable_item_removed': {"root[1]['value'][2]": 1}} |
| 311 |
|
| 312 |
As you can see the results are different. Now items with the same ids are compared with each other. |
| 313 |
|
| 314 |
.. note:: |
| 315 |
|
| 316 |
The level parameter of the iterable_compare_func is only used when ignore_order=False. |
| 317 |
|
| 318 |
Back to :doc:`/index` |