GeezerVison is showing github.com — adding more docs · adjust/deepdiff@7b22265
This repository was archived by the owner on Dec 11, 2023. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: conftest.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,6 +77,6 @@ def compare_func_t2(): | 77 | 77 |
| 78 | 78 |
| 79 | 79 | @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: | 82 | 82 | return json.load(the_file) |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/model.py +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -85,6 +85,13 @@ def mutual_add_removes_to_become_value_changes(self): | 85 | 85 | if 'iterable_item_added' in self and not self['iterable_item_added']: | 86 | 86 | del self['iterable_item_added'] | 87 | 87 |
| | 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 | + | 88 | 95 |
| 89 | 96 | class TextResult(ResultDict): | 90 | 97 |
| @@ -368,6 +375,13 @@ def _from_tree_repetition_change(self, tree): | 368 | 375 | for index in repetition['new_indexes']: | 369 | 376 | iterable_items_added_at_indexes[index] = value | 370 | 377 |
| | 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 | + | 371 | 385 |
| 372 | 386 | class DiffLevel: | 373 | 387 | """ | |
Collapse file Copy file name to clipboardExpand all lines: docs/other.rst +50-3Lines changed: 50 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -16,7 +16,6 @@ There are times that we want to guide DeepDiff as to what items to compare with | 16 | 16 | For example take the following objects: | 17 | 17 |
| 18 | 18 |
| 19 | | - | 20 | 19 | Now let's define a compare_func that takes 3 parameters: x, y and level. | 21 | 20 |
| 22 | 21 | >>> from deepdiff import DeepDiff | @@ -72,11 +71,59 @@ If we set the verbose_level=2, we can see more details. | 72 | 71 | {'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]}}}} | 73 | 72 |
| 74 | 73 |
| 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 | + | 76 | 120 |
| 77 | 121 | .. note:: | 78 | 122 |
| 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 | + | 80 | 127 |
| 81 | 128 |
| 82 | 129 | Back to :doc:`/index` |
Collapse file Copy file name to clipboardExpand all lines: docs/view.rst +4-4Lines changed: 4 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -52,7 +52,7 @@ You can traverse through the tree elements! | 52 | 52 |
| 53 | 53 | +---------------------------------------------------------------+ | 54 | 54 | | | | 55 | | - | parent(t1) parent node parent(t2) | | | 55 | + | parent(t1) parent node parent(t2) |----level | 56 | 56 | | + ^ + | | 57 | 57 | +------|--------------------------|---------------------|-------+ | 58 | 58 | | | | up | | @@ -61,13 +61,13 @@ You can traverse through the tree elements! | 61 | 61 | | down | | | | 62 | 62 | +------|----------------------|-------------------------|-------+ | 63 | 63 | | v v v | | 64 | | - | child(t1) child node child(t2) | | | 64 | + | child(t1) child node child(t2) |----level | 65 | 65 | | | | 66 | 66 | +---------------------------------------------------------------+ | 67 | 67 |
| 68 | 68 |
| 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 | 71 | 71 | :path(): Get the path to the current node in string representation, path(output_format='list') gives you the path in list representation. | 72 | 72 | :t1: The first item in the current node that is being diffed | 73 | 73 | :t2: The second item in the current node that is being diffed | |
Collapse file Copy file name to clipboardExpand all lines: tests/fixtures/compare_func_result1.json -44Lines changed: 0 additions & 44 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 55 | 55 | } | 56 | 56 | ] | 57 | 57 | } | 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 | | - } | 102 | 58 | } | 103 | 59 | } |
Collapse file Copy file name to clipboardExpand all lines: tests/test_delta.py +9-7Lines changed: 9 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1350,18 +1350,20 @@ def compare_func(x, y, level): | 1350 | 1350 |
| 1351 | 1351 | raise CannotCompare | 1352 | 1352 |
| 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): | 1354 | 1354 |
| 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 | 1357 | 1359 | delta = Delta(ddiff) | 1358 | 1360 | recreated_t2 = compare_func_t1 + delta | 1359 | 1361 | assert compare_func_t2 == recreated_t2 | 1360 | 1362 |
| 1361 | 1363 | def test_compare_func_with_duplicates_removed(self): | 1362 | 1364 | t1 = [{'id': 1, 'val': 1}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}, {'id': 3, 'val': 3}] | 1363 | 1365 | 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) | 1365 | 1367 | expected = { | 1366 | 1368 | 'values_changed': {"root[0]['val']": {'new_value': 3, 'old_value': 1}}, | 1367 | 1369 | 'iterable_item_removed': {'root[2]': {'id': 1, 'val': 3}}, | @@ -1378,7 +1380,7 @@ def test_compare_func_with_duplicates_removed(self): | 1378 | 1380 | def test_compare_func_with_duplicates_added(self): | 1379 | 1381 | t1 = [{'id': 3, 'val': 3}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}] | 1380 | 1382 | 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) | 1382 | 1384 | expected = { | 1383 | 1385 | 'values_changed': {"root[2]['val']": {'new_value': 1, 'old_value': 3}}, | 1384 | 1386 | 'iterable_item_added': {'root[2]': {'id': 1, 'val': 3}}, | @@ -1395,7 +1397,7 @@ def test_compare_func_with_duplicates_added(self): | 1395 | 1397 | def test_compare_func_swap(self): | 1396 | 1398 | t1 = [{'id': 1, 'val': 1}, {'id': 1, 'val': 3}] | 1397 | 1399 | 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) | 1399 | 1401 | expected = {'values_changed': {"root[0]['val']": {'new_value': 3, 'old_value': 1}, "root[1]['val']": {'new_value': 1, 'old_value': 3}}} | 1400 | 1402 | assert expected == ddiff | 1401 | 1403 | delta = Delta(ddiff) | @@ -1405,7 +1407,7 @@ def test_compare_func_swap(self): | 1405 | 1407 | def test_compare_func_path_specific(self): | 1406 | 1408 | 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}]} | 1407 | 1409 | 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) | 1409 | 1411 | 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}}}} | 1410 | 1412 | assert expected == ddiff | 1411 | 1413 | delta = Delta(ddiff) | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_ignore_order.py +64-21Lines changed: 64 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -780,6 +780,31 @@ def test_ignore_order_and_group_by(self): | 780 | 780 | expected2 = {'iterable_item_added': {"root['BB']['ate'][1]": 'Brownies'}} | 781 | 781 | assert expected2 == diff2 | 782 | 782 |
| | 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 | + | 783 | 808 | def test_ignore_order_with_compare_func_to_guide_comparison(self): | 784 | 809 | t1 = [ | 785 | 810 | { | @@ -812,35 +837,53 @@ def test_ignore_order_with_compare_func_to_guide_comparison(self): | 812 | 837 | ] | 813 | 838 |
| 814 | 839 | 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 | 828 | 842 |
| 829 | 843 | def compare_func(x, y, level=None): | 830 | 844 | try: | 831 | 845 | return x['id'] == y['id'] | 832 | 846 | except Exception: | 833 | 847 | raise CannotCompare() from None | 834 | 848 |
| 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] | 838 | 859 | }, | 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 | 843 | 886 |
| 844 | 887 | ddiff2 = DeepDiff(t1, t2, ignore_order=True, iterable_compare_func=compare_func) | 845 | | - assert expected2 == ddiff2 | | 888 | + assert self.EXPECTED_WITH_COMPARE_FUNC == ddiff2 | 846 | 889 | assert ddiff != ddiff2 |
|