11
11
strings , short_repr , numbers ,
12
12
np_ndarray , np_array_factory , numpy_dtypes , get_doc ,
13
13
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 ,
15
17
)
16
18
from deepdiff .path import (
17
19
_path_to_elements , _get_nested_obj , _get_nested_obj_and_force ,
@@ -877,6 +879,31 @@ def dumps(self):
877
879
def to_dict (self ):
878
880
return dict (self .diff )
879
881
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
+
880
907
@staticmethod
881
908
def _get_flat_row (action , info , _parse_path , keys_and_funcs , report_type_changes = True ):
882
909
for path , details in info .items ():
@@ -923,28 +950,44 @@ def _from_flat_dicts(flat_dict_list):
923
950
if action in FLATTENING_NEW_ACTION_MAP :
924
951
action = FLATTENING_NEW_ACTION_MAP [action ]
925
952
index = path .pop ()
926
- if action in {'attribute_added' , 'attribute_removed' }:
953
+ if action in {
954
+ FlatDataAction .attribute_added ,
955
+ FlatDataAction .attribute_removed ,
956
+ }:
927
957
root_element = ('root' , GETATTR )
928
958
else :
929
959
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
931
964
if new_path and new_path != path :
932
965
new_path = stringify_path (new_path , root_element = root_element )
933
966
else :
934
967
new_path = None
935
968
if action not in result :
936
969
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
+ }:
938
974
if path_str not in result [action ]:
939
975
result [action ][path_str ] = {}
940
976
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
+ }:
942
981
if path_str not in result [action ]:
943
982
result [action ][path_str ] = set ()
944
983
result [action ][path_str ].add (value )
945
984
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 ,
948
991
}:
949
992
result [action ][path_str ] = value
950
993
elif action == 'values_changed' :
@@ -964,8 +1007,29 @@ def _from_flat_dicts(flat_dict_list):
964
1007
]:
965
1008
if elem_value != UnkownValueCode :
966
1009
result [action ][path_str ][elem ] = elem_value
967
- elif action == ' iterable_item_moved' :
1010
+ elif action == FlatDataAction . iterable_item_moved :
968
1011
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
+ )
969
1033
if new_path :
970
1034
result [action ][path_str ]['new_path' ] = new_path
971
1035
@@ -1066,7 +1130,7 @@ def to_flat_rows(self, include_action_in_path=False, report_type_changes=True) -
1066
1130
}
1067
1131
for action , info in self .diff .items ():
1068
1132
if action == '_iterable_opcodes' :
1069
- result .extend (self ._flatten_iterable_opcodes ())
1133
+ result .extend (self ._flatten_iterable_opcodes (_parse_path = _parse_path ))
1070
1134
continue
1071
1135
if action .startswith ('_' ):
1072
1136
continue
0 commit comments