1111 strings , short_repr , numbers ,
1212 np_ndarray , np_array_factory , numpy_dtypes , get_doc ,
1313 not_found , numpy_dtype_string_to_type , dict_ ,
14- Opcode , FlatDeltaRow , UnkownValueCode ,
14+ Opcode , FlatDeltaRow , UnkownValueCode , FlatDataAction ,
15+ OPCODE_TAG_TO_FLAT_DATA_ACTION ,
16+ FLAT_DATA_ACTION_TO_OPCODE_TAG ,
1517)
1618from deepdiff .path import (
1719 _path_to_elements , _get_nested_obj , _get_nested_obj_and_force ,
@@ -877,6 +879,31 @@ def dumps(self):
877879 def to_dict (self ):
878880 return dict (self .diff )
879881
882+ def _flatten_iterable_opcodes (self , _parse_path ):
883+ """
884+ Converts op_codes to FlatDeltaRows
885+ """
886+ result = []
887+ for path , op_codes in self .diff ['_iterable_opcodes' ].items ():
888+ for op_code in op_codes :
889+ result .append (
890+ FlatDeltaRow (
891+ path = _parse_path (path ),
892+ action = OPCODE_TAG_TO_FLAT_DATA_ACTION [op_code .tag ],
893+ value = op_code .new_values ,
894+ old_value = op_code .old_values ,
895+ type = type (op_code .new_values ),
896+ old_type = type (op_code .old_values ),
897+ new_path = None ,
898+ t1_from_index = op_code .t1_from_index ,
899+ t1_to_index = op_code .t1_to_index ,
900+ t2_from_index = op_code .t2_from_index ,
901+ t2_to_index = op_code .t2_to_index ,
902+
903+ )
904+ )
905+ return result
906+
880907 @staticmethod
881908 def _get_flat_row (action , info , _parse_path , keys_and_funcs , report_type_changes = True ):
882909 for path , details in info .items ():
@@ -923,28 +950,44 @@ def _from_flat_dicts(flat_dict_list):
923950 if action in FLATTENING_NEW_ACTION_MAP :
924951 action = FLATTENING_NEW_ACTION_MAP [action ]
925952 index = path .pop ()
926- if action in {'attribute_added' , 'attribute_removed' }:
953+ if action in {
954+ FlatDataAction .attribute_added ,
955+ FlatDataAction .attribute_removed ,
956+ }:
927957 root_element = ('root' , GETATTR )
928958 else :
929959 root_element = ('root' , GET )
930- path_str = stringify_path (path , root_element = root_element ) # We need the string path
960+ if isinstance (path , str ):
961+ path_str = path
962+ else :
963+ path_str = stringify_path (path , root_element = root_element ) # We need the string path
931964 if new_path and new_path != path :
932965 new_path = stringify_path (new_path , root_element = root_element )
933966 else :
934967 new_path = None
935968 if action not in result :
936969 result [action ] = {}
937- if action in {'iterable_items_added_at_indexes' , 'iterable_items_removed_at_indexes' }:
970+ if action in {
971+ 'iterable_items_added_at_indexes' ,
972+ 'iterable_items_removed_at_indexes' ,
973+ }:
938974 if path_str not in result [action ]:
939975 result [action ][path_str ] = {}
940976 result [action ][path_str ][index ] = value
941- elif action in {'set_item_added' , 'set_item_removed' }:
977+ elif action in {
978+ FlatDataAction .set_item_added ,
979+ FlatDataAction .set_item_removed
980+ }:
942981 if path_str not in result [action ]:
943982 result [action ][path_str ] = set ()
944983 result [action ][path_str ].add (value )
945984 elif action in {
946- 'dictionary_item_added' , 'dictionary_item_removed' ,
947- 'attribute_removed' , 'attribute_added' , 'iterable_item_added' , 'iterable_item_removed' ,
985+ FlatDataAction .dictionary_item_added ,
986+ FlatDataAction .dictionary_item_removed ,
987+ FlatDataAction .attribute_removed ,
988+ FlatDataAction .attribute_added ,
989+ FlatDataAction .iterable_item_added ,
990+ FlatDataAction .iterable_item_removed ,
948991 }:
949992 result [action ][path_str ] = value
950993 elif action == 'values_changed' :
@@ -964,8 +1007,29 @@ def _from_flat_dicts(flat_dict_list):
9641007 ]:
9651008 if elem_value != UnkownValueCode :
9661009 result [action ][path_str ][elem ] = elem_value
967- elif action == ' iterable_item_moved' :
1010+ elif action == FlatDataAction . iterable_item_moved :
9681011 result [action ][path_str ] = {'value' : value }
1012+ elif action in {
1013+ FlatDataAction .iterable_items_inserted ,
1014+ FlatDataAction .iterable_items_deleted ,
1015+ FlatDataAction .iterable_items_replaced ,
1016+ FlatDataAction .iterable_items_equal ,
1017+ }:
1018+ if '_iterable_opcodes' not in result :
1019+ result ['_iterable_opcodes' ] = {}
1020+ if path_str not in result ['_iterable_opcodes' ]:
1021+ result ['_iterable_opcodes' ][path_str ] = []
1022+ result ['_iterable_opcodes' ][path_str ].append (
1023+ Opcode (
1024+ tag = FLAT_DATA_ACTION_TO_OPCODE_TAG [action ],
1025+ t1_from_index = flat_dict .get ('t1_from_index' ),
1026+ t1_to_index = flat_dict .get ('t1_to_index' ),
1027+ t2_from_index = flat_dict .get ('t2_from_index' ),
1028+ t2_to_index = flat_dict .get ('t2_to_index' ),
1029+ new_values = flat_dict .get ('value' ),
1030+ old_values = flat_dict .get ('old_value' ),
1031+ )
1032+ )
9691033 if new_path :
9701034 result [action ][path_str ]['new_path' ] = new_path
9711035
@@ -1066,7 +1130,7 @@ def to_flat_rows(self, include_action_in_path=False, report_type_changes=True) -
10661130 }
10671131 for action , info in self .diff .items ():
10681132 if action == '_iterable_opcodes' :
1069- result .extend (self ._flatten_iterable_opcodes ())
1133+ result .extend (self ._flatten_iterable_opcodes (_parse_path = _parse_path ))
10701134 continue
10711135 if action .startswith ('_' ):
10721136 continue
0 commit comments