GeezerVison is showing github.com — adding to_flat_dicts. · mirror-dump/deepdiff@82b27c1
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: deepdiff/delta.py +86-1Lines changed: 86 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import logging | | 2 | +from functools import partial | 2 | 3 | from collections.abc import Mapping | 3 | 4 | from copy import deepcopy | 4 | 5 | from ordered_set import OrderedSet | | 9 | 10 | np_ndarray, np_array_factory, numpy_dtypes, get_doc, | 10 | 11 | not_found, numpy_dtype_string_to_type, dict_, | 11 | 12 | ) | 12 | | -from deepdiff.path import _path_to_elements, _get_nested_obj, _get_nested_obj_and_force, GET, GETATTR | | 13 | +from deepdiff.path import _path_to_elements, _get_nested_obj, _get_nested_obj_and_force, GET, GETATTR, parse_path | 13 | 14 | from deepdiff.anyset import AnySet | 14 | 15 |
| 15 | 16 |
| @@ -591,6 +592,90 @@ def dumps(self): | 591 | 592 | def to_dict(self): | 592 | 593 | return dict(self.diff) | 593 | 594 |
| | 595 | + @staticmethod | | 596 | + def _get_flat_row(action, info, _parse_path, keys_and_funcs): | | 597 | + for path, details in info.items(): | | 598 | + row = {'path': _parse_path(path), 'action': action} | | 599 | + for key, new_key, func in keys_and_funcs: | | 600 | + if key in details: | | 601 | + if func: | | 602 | + row[new_key] = func(details[key]) | | 603 | + else: | | 604 | + row[new_key] = details[key] | | 605 | + yield row | | 606 | + | | 607 | + def to_flat_dicts(self, include_action_in_path=False, report_type_changes=True): | | 608 | + """ | | 609 | + Returns a flat list of actions | | 610 | + """ | | 611 | + result = [] | | 612 | + if include_action_in_path: | | 613 | + _parse_path = partial(parse_path, include_actions=True) | | 614 | + else: | | 615 | + _parse_path = parse_path | | 616 | + if report_type_changes: | | 617 | + keys_and_funcs = [ | | 618 | + ('value', 'value', None), | | 619 | + ('new_value', 'value', None), | | 620 | + ('old_value', 'old_value', None), | | 621 | + ('new_type', 'new_type', None), | | 622 | + ('old_type', 'old_type', None), | | 623 | + ('new_path', 'new_path', _parse_path), | | 624 | + ] | | 625 | + action_mapping = {} | | 626 | + else: | | 627 | + keys_and_funcs = [ | | 628 | + ('value', 'value', None), | | 629 | + ('new_value', 'value', None), | | 630 | + ('old_value', 'old_value', None), | | 631 | + ('new_path', 'new_path', _parse_path), | | 632 | + ] | | 633 | + action_mapping = {'type_changes': 'values_changed'} | | 634 | + | | 635 | + FLATTENING_NEW_ACTION_MAP = { | | 636 | + 'iterable_items_added_at_indexes': 'iterable_item_added', | | 637 | + 'iterable_items_removed_at_indexes': 'iterable_item_removed', | | 638 | + } | | 639 | + for action, info in self.diff.items(): | | 640 | + if action in FLATTENING_NEW_ACTION_MAP: | | 641 | + new_action = FLATTENING_NEW_ACTION_MAP[action] | | 642 | + for path, index_to_value in info.items(): | | 643 | + path = _parse_path(path) | | 644 | + for index, value in index_to_value.items(): | | 645 | + path2 = path.copy() | | 646 | + if include_action_in_path: | | 647 | + path2.append((index, 'GET')) | | 648 | + else: | | 649 | + path2.append(index) | | 650 | + result.append( | | 651 | + {'path': path2, 'value': value, 'action': new_action} | | 652 | + ) | | 653 | + elif action in {'set_item_added', 'set_item_removed'}: | | 654 | + for path, values in info.items(): | | 655 | + path = _parse_path(path) | | 656 | + for value in values: | | 657 | + result.append( | | 658 | + {'path': path, 'value': value, 'action': action} | | 659 | + ) | | 660 | + elif action in { | | 661 | + 'dictionary_item_added', 'dictionary_item_removed', 'iterable_item_added', | | 662 | + 'iterable_item_removed', 'attribute_removed', 'attribute_added' | | 663 | + }: | | 664 | + for path, value in info.items(): | | 665 | + path = _parse_path(path) | | 666 | + result.append( | | 667 | + {'path': path, 'value': value, 'action': action} | | 668 | + ) | | 669 | + else: | | 670 | + for row in self._get_flat_row( | | 671 | + action=action_mapping.get(action, action), | | 672 | + info=info, | | 673 | + _parse_path=_parse_path, | | 674 | + keys_and_funcs=keys_and_funcs, | | 675 | + ): | | 676 | + result.append(row) | | 677 | + return result | | 678 | + | 594 | 679 |
| 595 | 680 | if __name__ == "__main__": # pragma: no cover | 596 | 681 | import doctest | |
Collapse file Copy file name to clipboardExpand all lines: docs/delta.rst +16Lines changed: 16 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,22 @@ verify_symmetry : Boolean, default=False | 59 | 59 |
| 60 | 60 | A delta object that can be added to t1 to recreate t2. | 61 | 61 |
| | 62 | + Delta objects can contain the following vocabulary: | | 63 | + | | 64 | + iterable_item_added | | 65 | + iterable_item_moved | | 66 | + iterable_item_removed | | 67 | + set_item_added | | 68 | + set_item_removed | | 69 | + dictionary_item_added | | 70 | + dictionary_item_removed | | 71 | + attribute_added | | 72 | + attribute_removed | | 73 | + type_changes | | 74 | + values_changed | | 75 | + iterable_items_added_at_indexes | | 76 | + iterable_items_removed_at_indexes | | 77 | + | 62 | 78 |
| 63 | 79 | .. _delta_diff_label: | 64 | 80 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/test_delta.py +130Lines changed: 130 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | import sys | 6 | 6 | from decimal import Decimal | 7 | 7 | from unittest import mock | | 8 | +from ordered_set import OrderedSet | 8 | 9 | from deepdiff import Delta, DeepDiff | 9 | 10 | from deepdiff.helper import np, number_to_string, TEXT_VIEW, DELTA_VIEW, CannotCompare | 10 | 11 | from deepdiff.path import GETATTR, GET | @@ -63,6 +64,14 @@ def test_list_difference_add_delta(self): | 63 | 64 | assert delta + t1 == t2 | 64 | 65 | assert t1 + delta == t2 | 65 | 66 |
| | 67 | + flat_result1 = delta.to_flat_dicts() | | 68 | + flat_expected1 = [ | | 69 | + {'path': [3], 'value': 5, 'action': 'iterable_item_added'}, | | 70 | + {'path': [2], 'value': 3, 'action': 'iterable_item_added'}, | | 71 | + ] | | 72 | + | | 73 | + assert flat_expected1 == flat_result1 | | 74 | + | 66 | 75 | def test_list_difference_dump_delta(self): | 67 | 76 | t1 = [1, 2] | 68 | 77 | t2 = [1, 2, 3, 5] | @@ -213,6 +222,11 @@ def test_identical_delta(self): | 213 | 222 | t1 = [1, 3] | 214 | 223 | assert t1 + delta == t1 | 215 | 224 |
| | 225 | + flat_result1 = delta.to_flat_dicts() | | 226 | + flat_expected1 = [] | | 227 | + | | 228 | + assert flat_expected1 == flat_result1 | | 229 | + | 216 | 230 | def test_delta_mutate(self): | 217 | 231 | t1 = [1, 2] | 218 | 232 | t2 = [1, 2, 3, 5] | @@ -266,6 +280,15 @@ def test_list_difference3_delta(self): | 266 | 280 | assert delta + t1 == t2 | 267 | 281 | assert t1 + delta == t2 | 268 | 282 |
| | 283 | + flat_result1 = delta.to_flat_dicts() | | 284 | + flat_expected1 = [ | | 285 | + {'path': [4, 'b', 2], 'action': 'values_changed', 'value': 2, 'old_value': 5}, | | 286 | + {'path': [4, 'b', 1], 'action': 'values_changed', 'value': 3, 'old_value': 2}, | | 287 | + {'path': [4, 'b', 3], 'value': 5, 'action': 'iterable_item_added'}, | | 288 | + ] | | 289 | + | | 290 | + assert flat_expected1 == flat_result1 | | 291 | + | 269 | 292 | def test_list_difference_delta_raises_error_if_prev_value_does_not_match(self): | 270 | 293 | t1 = [1, 2, 6] | 271 | 294 | t2 = [1, 3, 2, 5] | @@ -295,6 +318,15 @@ def test_list_difference_delta_raises_error_if_prev_value_does_not_match(self): | 295 | 318 | delta2 = Delta(diff, verify_symmetry=False) | 296 | 319 | assert delta2 + t1 == t2 | 297 | 320 |
| | 321 | + flat_result2 = delta2.to_flat_dicts() | | 322 | + flat_expected2 = [ | | 323 | + {'path': [2], 'action': 'values_changed', 'value': 2, 'old_value': 5}, | | 324 | + {'path': [1], 'action': 'values_changed', 'value': 3, 'old_value': 2}, | | 325 | + {'path': [3], 'value': 5, 'action': 'iterable_item_added'}, | | 326 | + ] | | 327 | + | | 328 | + assert flat_expected2 == flat_result2 | | 329 | + | 298 | 330 | def test_list_difference_delta1(self): | 299 | 331 | t1 = { | 300 | 332 | 1: 1, | @@ -317,6 +349,14 @@ def test_list_difference_delta1(self): | 317 | 349 |
| 318 | 350 | assert delta + t1 == t2 | 319 | 351 |
| | 352 | + flat_result = delta.to_flat_dicts() | | 353 | + flat_expected = [ | | 354 | + {'path': [4, 'b', 2], 'value': 'to_be_removed', 'action': 'iterable_item_removed'}, | | 355 | + {'path': [4, 'b', 3], 'value': 'to_be_removed2', 'action': 'iterable_item_removed'}, | | 356 | + ] | | 357 | + | | 358 | + assert flat_expected == flat_result | | 359 | + | 320 | 360 | @mock.patch('deepdiff.delta.logger.error') | 321 | 361 | def test_list_difference_delta_if_item_is_already_removed(self, mock_logger): | 322 | 362 | t1 = [1, 2, 'to_be_removed'] | @@ -1167,6 +1207,28 @@ def test_list_ignore_order_various_deltas2(self): | 1167 | 1207 | t1_plus_delta2 = t1 + delta2 | 1168 | 1208 | assert t1_plus_delta2 == (8, 4, 4, 1, 3, 4, 1, 7) | 1169 | 1209 |
| | 1210 | + flat_result1 = delta1.to_flat_dicts() | | 1211 | + flat_expected1 = [ | | 1212 | + {'path': [0], 'value': 7, 'action': 'iterable_item_added'}, | | 1213 | + {'path': [6], 'value': 8, 'action': 'iterable_item_added'}, | | 1214 | + {'path': [1], 'value': 4, 'action': 'iterable_item_added'}, | | 1215 | + {'path': [2], 'value': 4, 'action': 'iterable_item_added'}, | | 1216 | + {'path': [5], 'value': 4, 'action': 'iterable_item_added'}, | | 1217 | + {'path': [6], 'value': 6, 'action': 'iterable_item_removed'}, | | 1218 | + {'path': [0], 'value': 5, 'action': 'iterable_item_removed'}, | | 1219 | + ] | | 1220 | + assert flat_expected1 == flat_result1 | | 1221 | + | | 1222 | + flat_result2 = delta2.to_flat_dicts() | | 1223 | + flat_expected2 = [ | | 1224 | + {'path': [1], 'value': 4, 'action': 'iterable_item_added'}, | | 1225 | + {'path': [2], 'value': 4, 'action': 'iterable_item_added'}, | | 1226 | + {'path': [5], 'value': 4, 'action': 'iterable_item_added'}, | | 1227 | + {'path': [6], 'action': 'values_changed', 'value': 7}, | | 1228 | + {'path': [0], 'action': 'values_changed', 'value': 8}, | | 1229 | + ] | | 1230 | + assert flat_expected2 == flat_result2 | | 1231 | + | 1170 | 1232 | def test_delta_view_and_to_delta_dict_are_equal_when_parameteres_passed(self): | 1171 | 1233 | """ | 1172 | 1234 | This is a test that passes parameters in a dictionary instead of kwargs. | @@ -1296,6 +1358,15 @@ def test_apply_delta_to_incompatible_object6_value_change(self): | 1296 | 1358 | t4 = delta2 + t3 | 1297 | 1359 | assert [] == t4 | 1298 | 1360 |
| | 1361 | + flat_result2 = delta2.to_flat_dicts() | | 1362 | + flat_expected2 = [{'path': [1, 2, 0], 'action': 'values_changed', 'value': 5}] | | 1363 | + assert flat_expected2 == flat_result2 | | 1364 | + | | 1365 | + delta3 = Delta(diff, raise_errors=False, verify_symmetry=True) | | 1366 | + flat_result3 = delta3.to_flat_dicts() | | 1367 | + flat_expected3 = [{'path': [1, 2, 0], 'action': 'values_changed', 'value': 5, 'old_value': 4}] | | 1368 | + assert flat_expected3 == flat_result3 | | 1369 | + | 1299 | 1370 | def test_apply_delta_to_incompatible_object7_type_change(self): | 1300 | 1371 | t1 = ['1'] | 1301 | 1372 | t2 = [1] | @@ -1397,6 +1468,10 @@ def test_delta_to_dict(self): | 1397 | 1468 | expected = {'iterable_items_removed_at_indexes': {'root': {2: 'B'}}} | 1398 | 1469 | assert expected == result | 1399 | 1470 |
| | 1471 | + flat_result = delta.to_flat_dicts() | | 1472 | + flat_expected = [{'action': 'iterable_item_removed', 'path': [2], 'value': 'B'}] | | 1473 | + assert flat_expected == flat_result | | 1474 | + | 1400 | 1475 | def test_class_type_change(self): | 1401 | 1476 | t1 = CustomClass | 1402 | 1477 | t2 = CustomClass2 | @@ -1445,6 +1520,30 @@ def test_none_in_delta_object(self): | 1445 | 1520 | delta = Delta(dump) | 1446 | 1521 | assert t2 == delta + t1 | 1447 | 1522 |
| | 1523 | + flat_result = delta.to_flat_dicts() | | 1524 | + flat_expected = [{'path': ['a'], 'action': 'type_changes', 'value': 1, 'new_type': int, 'old_type': type(None)}] | | 1525 | + assert flat_expected == flat_result | | 1526 | + | | 1527 | + flat_result2 = delta.to_flat_dicts(report_type_changes=False) | | 1528 | + flat_expected2 = [{'path': ['a'], 'action': 'values_changed', 'value': 1}] | | 1529 | + assert flat_expected2 == flat_result2 | | 1530 | + | | 1531 | + def test_delta_set_in_objects(self): | | 1532 | + t1 = [[1, OrderedSet(['A', 'B'])], {1}] | | 1533 | + t2 = [[2, OrderedSet([10, 'C', 'B'])], {1}] | | 1534 | + delta = Delta(DeepDiff(t1, t2)) | | 1535 | + flat_result = delta.to_flat_dicts() | | 1536 | + flat_expected = [ | | 1537 | + {'path': [0, 1], 'value': 10, 'action': 'set_item_added'}, | | 1538 | + {'path': [0, 0], 'action': 'values_changed', 'value': 2}, | | 1539 | + {'path': [0, 1], 'value': 'A', 'action': 'set_item_removed'}, | | 1540 | + {'path': [0, 1], 'value': 'C', 'action': 'set_item_added'}, | | 1541 | + ] | | 1542 | + # Sorting because otherwise the order is not deterministic for sets, | | 1543 | + # even though we are using OrderedSet here. It still is converted to set at some point and loses its order. | | 1544 | + flat_result.sort(key=lambda x: str(x['value'])) | | 1545 | + assert flat_expected == flat_result | | 1546 | + | 1448 | 1547 | def test_delta_with_json_serializer(self): | 1449 | 1548 | t1 = {"a": 1} | 1450 | 1549 | t2 = {"a": 2} | @@ -1547,6 +1646,16 @@ def test_compare_func_with_duplicates_removed(self): | 1547 | 1646 | recreated_t2 = t1 + delta | 1548 | 1647 | assert t2 == recreated_t2 | 1549 | 1648 |
| | 1649 | + flat_result = delta.to_flat_dicts() | | 1650 | + flat_expected = [ | | 1651 | + {'path': [2], 'value': {'id': 1, 'val': 3}, 'action': 'iterable_item_removed'}, | | 1652 | + {'path': [0], 'value': {'id': 1, 'val': 3}, 'action': 'iterable_item_removed'}, | | 1653 | + {'path': [3], 'value': {'id': 3, 'val': 3}, 'action': 'iterable_item_removed'}, | | 1654 | + {'path': [0], 'action': 'iterable_item_moved', 'value': {'id': 1, 'val': 3}, 'new_path': [2]}, | | 1655 | + {'path': [3], 'action': 'iterable_item_moved', 'value': {'id': 3, 'val': 3}, 'new_path': [0]}, | | 1656 | + ] | | 1657 | + assert flat_expected == flat_result | | 1658 | + | 1550 | 1659 | def test_compare_func_with_duplicates_added(self): | 1551 | 1660 | t1 = [{'id': 3, 'val': 3}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}] | 1552 | 1661 | t2 = [{'id': 1, 'val': 1}, {'id': 2, 'val': 2}, {'id': 1, 'val': 3}, {'id': 3, 'val': 3}] | @@ -1695,3 +1804,24 @@ def test_delta_force1(self): | 1695 | 1804 | result = {} + delta | 1696 | 1805 | expected = {'x': {'y': {3: 4}}, 'q': {'t': 0.5}} | 1697 | 1806 | assert expected == result | | 1807 | + | | 1808 | + def test_dict_added(self): | | 1809 | + t1 = {"field1": {"joe": "Joe"}} | | 1810 | + t2 = {"field1": {"joe": "Joe Nobody"}, "field2": {"jimmy": "Jimmy"}} | | 1811 | + diff = DeepDiff(t1, t2) | | 1812 | + delta = Delta(diff=diff) | | 1813 | + flat_result = delta.to_flat_dicts(report_type_changes=False) | | 1814 | + expected_result = [ | | 1815 | + {'path': ['field2'], 'value': {'jimmy': 'Jimmy'}, 'action': 'dictionary_item_added'}, | | 1816 | + {'path': ['field1', 'joe'], 'action': 'values_changed', 'value': 'Joe Nobody'}, | | 1817 | + ] | | 1818 | + assert expected_result == flat_result | | 1819 | + | | 1820 | + def test_flatten_attribute_added(self): | | 1821 | + t1 = picklalbe_obj_without_item | | 1822 | + t2 = PicklableClass(10) | | 1823 | + diff = DeepDiff(t1, t2) | | 1824 | + delta = Delta(diff=diff) | | 1825 | + flat_result = delta.to_flat_dicts(report_type_changes=False) | | 1826 | + expected_result = [{'path': ['item'], 'value': 10, 'action': 'attribute_added'}] | | 1827 | + assert expected_result == flat_result |
|