Skip to content

Commit 7b22265

Browse files
committed
adding more docs
1 parent 3229109 commit 7b22265

File tree

7 files changed

+143
-81
lines changed

7 files changed

+143
-81
lines changed

conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ def compare_func_t2():
7777

7878

7979
@pytest.fixture(scope='class')
80-
def compare_func_result():
81-
with open(os.path.join(FIXTURES_DIR, 'compare_func_result.json')) as the_file:
80+
def compare_func_result1():
81+
with open(os.path.join(FIXTURES_DIR, 'compare_func_result1.json')) as the_file:
8282
return json.load(the_file)

deepdiff/model.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ def mutual_add_removes_to_become_value_changes(self):
8585
if 'iterable_item_added' in self and not self['iterable_item_added']:
8686
del self['iterable_item_added']
8787

88+
def _from_tree_iterable_item_moved(self, tree):
89+
if 'iterable_item_moved' in tree:
90+
for change in tree['iterable_item_moved']:
91+
the_changed = {'new_path': change.path(use_t2=True), 'value': change.t2}
92+
self['iterable_item_moved'][change.path(
93+
force=FORCE_DEFAULT)] = the_changed
94+
8895

8996
class TextResult(ResultDict):
9097

@@ -368,6 +375,13 @@ def _from_tree_repetition_change(self, tree):
368375
for index in repetition['new_indexes']:
369376
iterable_items_added_at_indexes[index] = value
370377

378+
def _from_tree_iterable_item_moved(self, tree):
379+
if 'iterable_item_moved' in tree:
380+
for change in tree['iterable_item_moved']:
381+
the_changed = {'new_path': change.path(use_t2=True), 'value': change.t2}
382+
self['iterable_item_moved'][change.path(
383+
force=FORCE_DEFAULT)] = the_changed
384+
371385

372386
class DiffLevel:
373387
"""

docs/other.rst

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ There are times that we want to guide DeepDiff as to what items to compare with
1616
For example take the following objects:
1717

1818

19-
2019
Now let's define a compare_func that takes 3 parameters: x, y and level.
2120

2221
>>> from deepdiff import DeepDiff
@@ -72,11 +71,59 @@ If we set the verbose_level=2, we can see more details.
7271
{'iterable_item_added': {"root[2]['value'][2]": 1}, 'iterable_item_removed': {"root[1]['value'][2]": 1}, 'iterable_item_moved': {'root[0]': {'new_path': 'root[2]', 'value': {'id': 1, 'value': [1]}}, 'root[1]': {'new_path': 'root[0]', 'value': {'id': 2, 'value': [7, 8]}}, 'root[2]': {'new_path': 'root[1]', 'value': {'id': 3, 'value': [7, 8, 1]}}}}
7372

7473

75-
We can also use the level parameter.
74+
We can also use the level parameter. Levels are explained in the :ref:`tree_view_label`.
75+
76+
For example you could use the level object to further determine if the 2 objects should be matches or not.
77+
78+
79+
>>> t1 = {
80+
... 'path1': [],
81+
... 'path2': [
82+
... {
83+
... 'id': 1,
84+
... 'value': [1]
85+
... },
86+
... {
87+
... 'id': 2,
88+
... 'value': [7, 8, 1]
89+
... },
90+
... ]
91+
... }
92+
>>>
93+
>>> t2 = {
94+
... 'path1': [{'pizza'}],
95+
... 'path2': [
96+
... {
97+
... 'id': 2,
98+
... 'value': [7, 8, 1]
99+
... },
100+
... {
101+
... 'id': 1,
102+
... 'value': [1, 2]
103+
... },
104+
... ]
105+
... }
106+
>>>
107+
>>>
108+
>>> def compare_func2(x, y, level):
109+
... if (not isinstance(x, dict) or not isinstance(y, dict)):
110+
... raise CannotCompare
111+
... if(level.path() == "root['path2']"):
112+
... if (x["id"] == y["id"]):
113+
... return True
114+
... return False
115+
...
116+
>>>
117+
>>> DeepDiff(t1, t2, iterable_compare_func=compare_func2)
118+
{'iterable_item_added': {"root['path1'][0]": {'pizza'}, "root['path2'][0]['value'][1]": 2}}
119+
76120

77121
.. note::
78122

79-
The level parameter of the iterable_compare_func is only used when ignore_order=False which is the default.
123+
The level parameter of the iterable_compare_func is only used when ignore_order=False which is the default value for ignore_order.
124+
125+
126+
80127

81128

82129
Back to :doc:`/index`

docs/view.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ You can traverse through the tree elements!
5252
5353
+---------------------------------------------------------------+
5454
| |
55-
| parent(t1) parent node parent(t2) |
55+
| parent(t1) parent node parent(t2) |----level
5656
| + ^ + |
5757
+------|--------------------------|---------------------|-------+
5858
| | | up |
@@ -61,13 +61,13 @@ You can traverse through the tree elements!
6161
| down | | |
6262
+------|----------------------|-------------------------|-------+
6363
| v v v |
64-
| child(t1) child node child(t2) |
64+
| child(t1) child node child(t2) |----level
6565
| |
6666
+---------------------------------------------------------------+
6767
6868
69-
:up: Move up to the parent node
70-
:down: Move down to the child node
69+
:up: Move up to the parent node aka parent level
70+
:down: Move down to the child node aka child level
7171
:path(): Get the path to the current node in string representation, path(output_format='list') gives you the path in list representation.
7272
:t1: The first item in the current node that is being diffed
7373
:t2: The second item in the current node that is being diffed

tests/fixtures/compare_func_result.json renamed to tests/fixtures/compare_func_result1.json

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,49 +55,5 @@
5555
}
5656
]
5757
}
58-
},
59-
"iterable_item_moved": {
60-
"root['Cars'][0]": {
61-
"new_path": "root['Cars'][2]",
62-
"value": {
63-
"id": "1",
64-
"make": "Toyota",
65-
"model": "Camry",
66-
"dealers": [
67-
{
68-
"id": 105,
69-
"address": "105 Fake St",
70-
"quantity": 50
71-
},
72-
{
73-
"id": 200,
74-
"address": "200 Fake St",
75-
"quantity": 10
76-
}
77-
]
78-
}
79-
},
80-
"root['Cars'][0]['dealers'][1]": {
81-
"new_path": "root['Cars'][0]['dealers'][0]",
82-
"value": {
83-
"id": 105,
84-
"address": "105 Fake St",
85-
"quantity": 50
86-
}
87-
},
88-
"root['Cars'][2]": {
89-
"new_path": "root['Cars'][1]",
90-
"value": {
91-
"id": "3",
92-
"make": "Toyota",
93-
"model": "4Runner",
94-
"model_numbers": [
95-
1,
96-
2,
97-
3,
98-
4
99-
]
100-
}
101-
}
10258
}
10359
}

tests/test_delta.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,18 +1350,20 @@ def compare_func(x, y, level):
13501350

13511351
raise CannotCompare
13521352

1353-
def test_pass(self, compare_func_t1, compare_func_t2, compare_func_result):
1353+
def test_compare_func1(self, compare_func_t1, compare_func_t2, compare_func_result1):
13541354

1355-
ddiff = DeepDiff(compare_func_t1, compare_func_t2, iterable_compare_func=self.compare_func)
1356-
assert compare_func_result == ddiff
1355+
ddiff = DeepDiff(
1356+
compare_func_t1, compare_func_t2,
1357+
iterable_compare_func=self.compare_func, verbose_level=1)
1358+
assert compare_func_result1 == ddiff
13571359
delta = Delta(ddiff)
13581360
recreated_t2 = compare_func_t1 + delta
13591361
assert compare_func_t2 == recreated_t2
13601362

13611363
def test_compare_func_with_duplicates_removed(self):
13621364
t1 = [{'id': 1, 'val': 1}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}, {'id': 3, 'val': 3}]
13631365
t2 = [{'id': 3, 'val': 3}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}]
1364-
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func)
1366+
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2)
13651367
expected = {
13661368
'values_changed': {"root[0]['val']": {'new_value': 3, 'old_value': 1}},
13671369
'iterable_item_removed': {'root[2]': {'id': 1, 'val': 3}},
@@ -1378,7 +1380,7 @@ def test_compare_func_with_duplicates_removed(self):
13781380
def test_compare_func_with_duplicates_added(self):
13791381
t1 = [{'id': 3, 'val': 3}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}]
13801382
t2 = [{'id': 1, 'val': 1}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}, {'id': 3, 'val': 3}]
1381-
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func)
1383+
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2)
13821384
expected = {
13831385
'values_changed': {"root[2]['val']": {'new_value': 1, 'old_value': 3}},
13841386
'iterable_item_added': {'root[2]': {'id': 1, 'val': 3}},
@@ -1395,7 +1397,7 @@ def test_compare_func_with_duplicates_added(self):
13951397
def test_compare_func_swap(self):
13961398
t1 = [{'id': 1, 'val': 1}, {'id': 1, 'val': 3}]
13971399
t2 = [{'id': 1, 'val': 3}, {'id': 1, 'val': 1}]
1398-
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func)
1400+
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2)
13991401
expected = {'values_changed': {"root[0]['val']": {'new_value': 3, 'old_value': 1}, "root[1]['val']": {'new_value': 1, 'old_value': 3}}}
14001402
assert expected == ddiff
14011403
delta = Delta(ddiff)
@@ -1405,7 +1407,7 @@ def test_compare_func_swap(self):
14051407
def test_compare_func_path_specific(self):
14061408
t1 = {"path1": [{'id': 1, 'val': 1}, {'id': 2, 'val': 3}], "path2": [{'ID': 4, 'val': 3}, {'ID': 3, 'val': 1}, ], "path3": [{'no_id': 5, 'val': 1}, {'no_id': 6, 'val': 3}]}
14071409
t2 = {"path1": [{'id': 1, 'val': 1}, {'id': 2, 'val': 3}], "path2": [{'ID': 3, 'val': 1}, {'ID': 4, 'val': 3}], "path3": [{'no_id': 5, 'val': 1}, {'no_id': 6, 'val': 3}]}
1408-
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func)
1410+
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func, verbose_level=2)
14091411
expected = {'iterable_item_moved': {"root['path2'][0]": {'new_path': "root['path2'][1]", 'value': {'ID': 4, 'val': 3}},"root['path2'][1]": {'new_path': "root['path2'][0]", 'value': {'ID': 3, 'val': 1}}}}
14101412
assert expected == ddiff
14111413
delta = Delta(ddiff)

tests/test_ignore_order.py

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,31 @@ def test_ignore_order_and_group_by(self):
780780
expected2 = {'iterable_item_added': {"root['BB']['ate'][1]": 'Brownies'}}
781781
assert expected2 == diff2
782782

783+
784+
class TestCompareFuncIgnoreOrder:
785+
786+
EXPECTED = {
787+
'values_changed': {
788+
"root[2]['id']": {
789+
'new_value': 2,
790+
'old_value': 3
791+
},
792+
"root[1]['id']": {
793+
'new_value': 3,
794+
'old_value': 2
795+
}
796+
}
797+
}
798+
799+
EXPECTED_WITH_COMPARE_FUNC = {
800+
'iterable_item_added': {
801+
"root[2]['value'][2]": 1
802+
},
803+
'iterable_item_removed': {
804+
"root[1]['value'][2]": 1
805+
}
806+
}
807+
783808
def test_ignore_order_with_compare_func_to_guide_comparison(self):
784809
t1 = [
785810
{
@@ -812,35 +837,53 @@ def test_ignore_order_with_compare_func_to_guide_comparison(self):
812837
]
813838

814839
ddiff = DeepDiff(t1, t2, ignore_order=True)
815-
expected = {
816-
'values_changed': {
817-
"root[2]['id']": {
818-
'new_value': 2,
819-
'old_value': 3
820-
},
821-
"root[1]['id']": {
822-
'new_value': 3,
823-
'old_value': 2
824-
}
825-
}
826-
}
827-
assert expected == ddiff
840+
841+
assert self.EXPECTED == ddiff
828842

829843
def compare_func(x, y, level=None):
830844
try:
831845
return x['id'] == y['id']
832846
except Exception:
833847
raise CannotCompare() from None
834848

835-
expected2 = {
836-
'iterable_item_added': {
837-
"root[2]['value'][2]": 1
849+
ddiff2 = DeepDiff(t1, t2, ignore_order=True, iterable_compare_func=compare_func)
850+
assert self.EXPECTED_WITH_COMPARE_FUNC == ddiff2
851+
assert ddiff != ddiff2
852+
853+
def test_ignore_order_with_compare_func_can_throw_cannot_compare(self):
854+
t1 = [
855+
{},
856+
{
857+
'id': 2,
858+
'value': [7, 8, 1]
838859
},
839-
'iterable_item_removed': {
840-
"root[1]['value'][2]": 1
841-
}
842-
}
860+
{
861+
'id': 3,
862+
'value': [7, 8],
863+
},
864+
]
865+
866+
t2 = [
867+
{
868+
'id': 2,
869+
'value': [7, 8]
870+
},
871+
{
872+
'id': 3,
873+
'value': [7, 8, 1],
874+
},
875+
{},
876+
]
877+
878+
ddiff = DeepDiff(t1, t2, ignore_order=True)
879+
assert self.EXPECTED == ddiff
880+
881+
def compare_func(x, y, level=None):
882+
try:
883+
return x['id'] == y['id']
884+
except Exception:
885+
raise CannotCompare() from None
843886

844887
ddiff2 = DeepDiff(t1, t2, ignore_order=True, iterable_compare_func=compare_func)
845-
assert expected2 == ddiff2
888+
assert self.EXPECTED_WITH_COMPARE_FUNC == ddiff2
846889
assert ddiff != ddiff2

0 commit comments

Comments
 (0)