
    :Qgb                        d dl Z d dlZd dlmZmZmZmZmZmZm	Z	 d dl
mZmZ d dlmZ d dl mZ d dlmZ d dlmZmZ d dlmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d d	l'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. d d
l/m0Z0  ejb                  e2      Z3dZ4dZ5dZ6dZ7dZ8dZ9dZ:dZ;dZ<dZ=dZ>dZ?dZ@dZAdZBdZC ed      ZD G d deE      ZF G d deE      ZG G d d       ZHe2d!k(  rd dlIZI eIj                          yy)"    N)ListDictIOCallableSetUnionOptional)partial
cmp_to_key)Mapping)deepcopy)DeepDiff)pickle_loadpickle_dump)strings
short_reprnumbers
np_ndarraynp_array_factorynumpy_dtypesget_doc	not_foundnumpy_dtype_string_to_typedict_OpcodeFlatDeltaRowUnkownValueCodeFlatDataActionOPCODE_TAG_TO_FLAT_DATA_ACTIONFLAT_DATA_ACTION_TO_OPCODE_TAG
SetOrdered)_path_to_elements_get_nested_obj_get_nested_obj_and_forceGETGETATTR
parse_pathstringify_path)AnySetzExpected the old value for {} to be {} but it is {}. Error found on: {}. You may want to set force=True, especially if this delta is created by passing flat_rows_list or flat_dict_listz=Key or index of {} is not found for {} for setting operation.z=Unable to do the type change for {} from to type {} due to {}zsYou have applied the delta to an object that has different values than the original object the delta was made from.zEFailed to remove index[{}] on {}. It was expected to be {} but got {}zA numpy ndarray is most likely being added to a delta. Due to Numpy override the + operator, you can only do: delta + ndarray and NOT ndarray + deltaz^Please open the file in the binary mode and pass to Delta by passing 'b' in open(..., 'b'): {}zOAt least one of the diff, delta_path or delta_file arguments need to be passed.zDinvalid action of {} when calling _get_elem_and_compare_to_old_valuez8invalid action of {} when calling _simple_set_elem_valuez Unable to get the item at {}: {}zUnable to get the item at {}zdDelta added to an incompatible object. Unable to add the following items at the specific indexes. {}NUMPY_TO_LISTz{} is not a valid numpy type.z	delta.rstc                       e Zd ZdZy)
DeltaErrorz
    Delta specific errors
    N__name__
__module____qualname____doc__     K/var/www/html/answerous/venv/lib/python3.12/site-packages/deepdiff/delta.pyr,   r,   4        	r3   r,   c                       e Zd ZdZy)DeltaNumpyOperatorOverrideErrorz-
    Delta Numpy Operator Override Error
    Nr-   r2   r3   r4   r7   r7   ;   r5   r3   r7   c            #       T   e Zd ZeZddddddeddddedddddfdeee	e
edf   dee
   dee   dee   deee      d	eee      d
ededededeee
      dedee   dededee   def"dZd Zd Zd ZeZd ZdDdZd Z	 	 	 	 dEdZdFdZd Zd Z dGd Z!d! Z"d" Z#d# Z$d$ Z%e&d%        Z'e&d&        Z(dHd'Z)d( Z*d) Z+d* Z,d+ Z-d, Z.dId-Z/d. Z0d/ Z1d0 Z2d1 Z3d2 Z4d3 Z5d4 Z6d5 Z7d6 Z8d7 Z9d8 Z:d9 Z;d: Z<d; Z=d< Z>d= Z?e&dJd>       Z@e&d	ee   fd?       ZAe&d@        ZBdIdAee   fdBZCdIdAee   fdCZDy)KDeltaNTFdiff
delta_path
delta_file
delta_diffflat_dict_listflat_rows_listdeserializer
log_errorsmutateraise_errorssafe_to_import
serializerverify_symmetrybidirectionalalways_include_valuesiterable_compare_func_was_usedforcec                    t        d      r$dt        j                  j                        v r}ndfd	}d | _        |t
        j                  d       |}|| _        |rd| _        n|| _        |mt        |t              r%|j                  | | j                        | _        nt        |t              r|| _        nt        |t              r |||      | _        n|r5t        |d      5 }|j!                         }d d d         ||      | _        n|r|| _        n|r!	 |j!                         } |||      | _        ng|r*| j+                  t-        j.                  |            | _        n;|r*| j1                  t-        j.                  |            | _        nt%        t2              |	| _        |
| _        || _        | j                  j;                  d	d
      | _        | j                  j;                  d|      | _        || _         | _!        || _"        |rtF        | _$        ntJ        | _$        | jM                          y # 1 sw Y   DxY w# t"        $ r$}t%        t&        j)                  |            d d }~ww xY w)N__code__rD   c           	           |       }|j                  d      rKi }|d   j                         D ].  \  }}g ||<   |D ]  }||   j                  t        di |       ! 0 ||d<   |S )N_iterable_opcodesr2   )getitemsappendr   )objrD   resultrN   pathop_codesop_coder@   s          r4   _deserializerz%Delta.__init__.<locals>._deserializer^   s    %c*::12(*%*01D*E*K*K*M h24)$/'/ G-d3:: & !"&-!" 3DF./r3   zMDeepDiff Deprecation: use bidirectional instead of verify_symmetry parameter.T)directedrH   )rD   rb_numpy_pathsF_iterable_compare_func_was_usedN)'hasattrsetrL   co_varnames_reversed_diffloggerwarningrG   rH   
isinstancer   _to_delta_dictr:   r   r   openreadUnicodeDecodeError
ValueErrorBINIARY_MODE_NEEDED_MSGformat_from_flat_dictscopyr   _from_flat_rowsDELTA_AT_LEAST_ONE_ARG_NEEDEDrB   rC   rA   rO   rZ   r[   rE   r@   rJ   r$   get_nested_objr#   reset)selfr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rW   the_filecontentes          `              r4   __init__zDelta.__init__F   s(   * <,1ASI^I^IjIjEk1k(M  #&NN_ ,M*)-D&)>D&$) //]9Jbfb|b|/}	D'* 	D'*)$~N	j$' *8"--/*%gnMDI"DIN$//+ &gnMDI--dmmN.KLDI,,T]]>-JKDI:;;($ IIMM.%@ 04yy}}=^`~/,$(
";D"1D

C* * & N !8!?!?!BCMNs$   -I #I  I
	I:I55I:c                 N    dj                  t        | j                  d            S )Nz<Delta: {}>d   )
max_length)rj   r   r:   rq   s    r4   __repr__zDelta.__repr__   s    ##JtyyS$IJJr3   c                 "    t               | _        y r\   )r   post_process_paths_to_convertry   s    r4   rp   zDelta.reset   s    -2W*r3   c                    t        |t              r| j                  rt        t              | j
                  r|| _        nt        |      | _        | j                          | j                          | j                          | j                          | j                          | j                          | j                          | j                          | j!                          | j#                          | j%                          | j'                          | j)                          | j+                          | j                  }| `| j-                          |S r\   )rc   r   rZ   r7   !DELTA_NUMPY_OPERATOR_OVERRIDE_MSGrB   rootr   _do_pre_process_do_values_changed_do_set_item_added_do_set_item_removed_do_type_changes_do_iterable_opcodes_do_iterable_item_removed_do_iterable_item_added_do_ignore_order_do_dictionary_item_added_do_dictionary_item_removed_do_attribute_added_do_attribute_removed_do_post_processrp   )rq   others     r4   __add__zDelta.__add__   s    eW%$*;*;12STT;;DI DI!!!!# 	!!#&&($$&&&(((*  """$		I

r3   c                     | j                   | j                         | _         | j                   | j                  c| _        | _         | j                  |      }| j                   | j                  c| _        | _         |S r\   )r`   _get_reverse_diffr:   r   )rq   r   rS   s      r4   __rsub__zDelta.__rsub__   sh    &"&"8"8":D)-)<)<dii&	4&e$)-)<)<dii&	4&r3   c                 v    | j                   r t        t        |      |       | j                  rt	        |      y r\   )rA   getattrra   rC   r,   )rq   msglevels      r4   _raise_or_logzDelta._raise_or_log   s2    ??"GFE"3'S/! r3   c                     | j                   rX||k7  rRt        |t              r|}nt        |dt        f      }| j                  t        j                  |||t                     y y y )N root_element)	rG   rc   strr(   r&   r   VERIFICATION_MSGrj   VERIFY_BIDIRECTIONAL_MSG)rq   rT   expected_old_valuecurrent_old_valuepath_strs        r4   _do_verify_changeszDelta._do_verify_changes   sf    "48I"I$$)$b']K/66,.?AY [ \ #Jr3   c           	      J   	 |t         k(  r||   }|S |t        k(  rt        ||      }|S t        t        j                  |            # t        t        t        t        f$ rA}	| j                  r||t        |t              ri }
ng }
n|}
|t         k(  rLt        |t              r6t        |t              r|t        |      k  r|
||<   n.|j!                  |
       n|
||<   n|t        k(  rt#        |||
       |
cY d }	~	S t$        }t        |t        t&        f      r(dj)                  |D cg c]  }|d   	 nc c}w c}      }| j*                  r(| j-                  t.        j                  ||||	             n$| j-                  t0        j                  |             Y d }	~	|S d }	~	ww xY w)N.r   )r%   r&   r   r,   $INVALID_ACTION_WHEN_CALLING_GET_ELEMrj   KeyError
IndexErrorAttributeError	TypeErrorrJ   rc   r   listintlenrQ   setattrr   tuplejoinrG   r   r   UNABLE_TO_GET_PATH_MSG)rq   rR   path_for_err_reportingr   elemactionforced_old_valuenext_elementr   rt   _forced_old_valueis               r4   "_get_elem_and_compare_to_old_valuez(Delta._get_elem_and_compare_to_old_value   s   $	-}$'I!F ! E 7"$+C$6!B ! ? !!E!L!LV!TUU*ni@ 	-zz#+#+z,/L,.),.)(8%S=!#t,%dC0TCH_(9CIJJ'89$5D	w&C'89(( )04-@),AW2XA1Q42X2X)Y&!!""#3#:#:*&(91$> ? ""#9#@#@*$, -  =	-s;   A A A F"!BF5F";+F&D3
2A%FF"c                    	 |t         k(  r	 |||<   y|t        k(  rt        |||       yt        t        j                  |            # t        $ rJ |t        |      k(  r|j                  |       n'| j	                  t
        j                  ||             Y yY yw xY w# t        t        t        t        f$ r+}| j	                  dj                  ||             Y d}~yd}~ww xY w)z=
        Set the element value directly on an object
        Failed to set {} due to {}N)r%   r   r   rQ   r   ELEM_NOT_FOUND_TO_ADD_MSGrj   r&   r   r,   +INVALID_ACTION_WHEN_CALLING_SIMPLE_SET_ELEMr   r   r   )rq   rR   r   r   valuer   rt   s          r4   _simple_set_elem_valuezDelta._simple_set_elem_value  s    	_}k %CI 7"T5) !L!S!STZ![\\ " ks3x'

5)**+D+K+KDRh+ij *k *ni@ 	_;BBCY[\]^^	_sE   	B A B B ABB B BB C!6!CC!c	                 p    ||d| j                   |dd <    ||      }|r| j                  |||||       |S )z
        Coerce obj and mark it in post_process_paths_to_convert for later to be converted back.
        Also reassign it to its parent to replace the old object.
        old_typenew_typeNrR   r   r   r   r   )r|   r   )	rq   parentrR   rT   parent_to_obj_elemparent_to_obj_actionelementsto_type	from_types	            r4   _coerce_objzDelta._coerce_obj*  sX     JQ^g<h**8CR=9
 cl ''F4Vh.1:N ( P
r3   c
           
          t        |t              r!| j                  ||||||t        t              }|dk7  rB| j                  r6t        |t              r&t        |      dk(  ri }| j                  |||||       | j                  ||||	|       y)zs
        Set the element value on an object and if necessary convert the object to the proper mutable type
        )r   r   r   r   N)rc   r   r   r   rJ   r   r   )
rq   r   r   r   rR   r   rT   r   r   	new_values
             r4   _set_new_valuezDelta._set_new_value>  s    
 c5!""T#5$h # /C 19
3(=#c(a-C''F4Vh.1:N ( P##Dt*3F 	$ 	Dr3   c                    	 |t         k(  r||= y|t        k(  r|j                  |= yt        t        j                  |            # t        t        t        f$ r+}| j                  dj                  ||             Y d}~yd}~ww xY w)z:
        Delete the element directly on an object
        r   N)
r%   r&   __dict__r,   .INVALID_ACTION_WHEN_CALLING_SIMPLE_DELETE_ELEMrj   r   r   r   r   )rq   rR   r   r   r   rt   s         r4   _simple_delete_elemzDelta._simple_delete_elemQ  s|    	_}I7"LL& !O!V!VW]!^__*n5 	_;BBCY[\]^^	_s!   A A A B!A>>Bc	                     d}	t        |t              r*t        t        d| j                  |dd <   t        |      }d}	| j	                  ||||       |	r|r| j                  |||||       yyy)zv
        Delete the element value on an object and if necessary convert the object to the proper mutable type
        Fr   Nr   T)rR   r   r   r   r   )rc   r   r   r|   r   r   )
rq   r   r   r   rR   r   rT   r   r   
obj_is_news
             r4   	_del_elemzDelta._del_elem_  s    
 
c5!MQ_d@eD..x}=s)CJ  SDY_ `& ''F4Vh.1:N ( P !:r3   c                    | j                   j                  di       }| j                   j                  d      }|r7|j                         D ci c]  \  }}|d   d  }}}|j                  |       |r| j	                  |d       |r=|j                         D ci c]  \  }}|d   |d    }}}| j	                  |d       y y c c}}w c c}}w )Niterable_item_addediterable_item_movednew_pathT)insertr   F)r:   rO   rP   update_do_item_added)rq   r   r   kv
added_dicts         r4   r   zDelta._do_iterable_item_addedq  s    "iimm,A2F"iimm,AB
 :M:S:S:UV$!Q!J--VJV&&z2 3DA@S@Y@Y@[\1!J-73\J\
59  W ]s   CCc                 f    | j                   j                  d      }|r| j                  |d       y y )Ndictionary_item_addedF)sortr:   rO   r   )rq   r   s     r4   r   zDelta._do_dictionary_item_added  s2     $		.E F  5EB !r3   c                 b    | j                   j                  d      }|r| j                  |       y y )Nattribute_addedr   )rq   r   s     r4   r   zDelta._do_attribute_added  s+    ))--(9:0 r3   c                 N    t        | d         }|D cg c]  }|d   	 c}S c c}w )Nr   )r"   )path_and_valuer   r   s      r4   _sort_key_for_item_addedzDelta._sort_key_for_item_added  s+    $^A%67 ''!'''s   "c                 B   t        | d   d      D cg c]  }|d   	 }}t        |d   d      D cg c]  }|d   	 }}	 ||k  ry||kD  ryyc c}w c c}w # t        $ r t        |      t        |      kD  r|dt        |       }n%t        |      t        |      kD  r|dt        |       }t        ||      D ]_  \  }}t	        |      t	        |      k7  st	        |      dv rt        |      }t        |      }	 ||k  r Y y||kD  r Y yR# t        $ r Y ]w xY w Y yw xY w)z
        We use sort comparison instead of _sort_key_for_item_added when we run into comparing element types that can not
        be compared with each other, such as None to None. Or integer to string.
        r   Nr   r      )r"   r   r   ziptyper   )leftrightr   	left_path
right_pathl_elemr_elems          r4   _sort_comparisonzDelta._sort_comparison  s=    $5T!W4#PQaQqTQ	Q$5eAhT$RSqadS
S	:%Z' RS  	9~J/%&6s:7	Z3y>1'Y8
"%i"< 
<4</4<43G [F [F!&  )  
 !	sY   AAA A B D:D?DDD
D	DDDDDc                    |r'	 t        |j                         | j                        }n|j                         }|D ]_  \  }}| j                  |      }|r|\  }}}	}
}}}n%|r |t        |      k  r|j                  |d        | j                  ||	|
||||||	       a y # t        $ r1 t        |j                         t	        | j
                              }Y w xY w)N)key)
sortedrP   r   r   r   r   _get_elements_and_detailsr   r   r   )rq   rP   r   r   rT   r   elem_and_detailsr   r   r   r   rR   r   r   s                 r4   r   zDelta._do_item_added  s    Uu{{}$2O2OP KKME$ 	NOD)#==dC`p]&"46JCQUW] $S/

4&(:<P #XtT69N	N  Uu{{}*T=R=R2STUs   %B 7CCc                 b    | j                   j                  d      }|r| j                  |       y y )Nvalues_changedr:   rO   _do_values_or_type_changed)rq   r   s     r4   r   zDelta._do_values_changed  s+    '78++N; r3   c                 f    | j                   j                  d      }|r| j                  |d       y y )Ntype_changesTis_type_changer   )rq   r   s     r4   r   zDelta._do_type_changes  s/    yy}}^4++L+N r3   c                 Z    | j                   r| j                  | j                   dd       y y )NTF)r   verify_changes)r|   r   ry   s    r4   r   zDelta._do_post_process  s/    -- ++D,N,N_cty+z .r3   c                    | j                   rd| j                  v sd| j                  v rut               }| j                   j                         D ]7  \  }}t        t
        d||<   	 t        |      }t
        |d| j                  |<   9 |r| j                  |d       y y y y # t        $ r.}| j                  t        j                  |             Y d }~d }~ww xY w)Nr   iterable_item_removedr   Tr   )rZ   r:   r   rP   r   r   r   	Exceptionr   NOT_VALID_NUMPY_TYPErj   r|   r   )rq   preprocess_pathsrT   type_rt   s        r4   r   zDelta._do_pre_process  s    "7499"DH_cgclclHl$w#00668 ae6@d)S &6u=E IMZ_;`2248a  //0@QU/V   Im ! &&';'B'B1'EFs   $B  	C)$CCc                    	 t        |      }t        |      dkD  rc|d d }t        |      t        |      k7  r|d   d   }|d   d   }nd }| j                  | ||      }|d   \  }}| j                  ||d ||      }	nd x}x}}| }	|d   \  }
}|	t        u ry |||||	|
|fS # t
        $ r/}| j                  t        j                  ||             Y d }~y d }~ww xY w)Nr   r   r   )rR   r   r   )rR   r   r   r   r   r   )	r"   r   ro   r   r   r  r   UNABLE_TO_GET_ITEM_MSGrj   )rq   rT   r   elements_subsetr   next2_elementr   r   r   rR   r   r   rt   s                r4   r   zDelta._get_elements_and_details  s&   	a(.H8}q "*3B-'3x=8#+B<?L$,RLOM#'L,,^j,k;CB<8"$8==tPT+4HWd > f FJII+.B#B<LD&
 iV%79MsTXZ```  	5<<T1EF	s   BB   	C)%CCc                 8   |j                         D ]  \  }}| j                  |      }|r|\  }}}	}
}}}n%|j                  dt              }| j	                  |||||      }|t        u rZ|r(d|vr$	 |d   }|t
        v rt        ||      }n ||      }n|d   }| j                  ||	|
||||||	       |s| j                  |||        y # t        $ rD}| j                  t        j                  ||   |j                  dd      |             Y d }~d }~ww xY w)N	old_value)rR   r   r   r   r   r   r   unknown)rP   r   rO   r   r   r   r   r  r   TYPE_CHANGE_FAIL_MSGrj   r   r   )rq   changesr   r  rT   r   r   r   r   r   r   rR   r   r   r   r   r   r   rt   s                      r4   r   z Delta._do_values_or_type_changed  sY   "==? !	UKD%#==dC`p]&"46JCQUW]!&;	!B $ G GI[bfou !H !w I- +U":	$Z0H</$45F$Q	$,->$?	
 "+.	(:<P #XtT69N ''.@BSTC!	U. ! &&';'B'B3t9eiiXbdmNnpq'rss   2"C	D9DDc                    	 t        |j                         | j                  d      }|D ]  \  }}| j                  |      }|r|\  }}}}	}
}}n%d}t        }	 |t        k(  r|
|   }n|t        k(  rt        |
|      }||k7  }|r3t        |
t              r#| j                   s| j#                  |
||      }||}|t        u s|| j%                  |||	|
||||       | j'                  |||        y# t        $ r3 t        |j                         t	        | j
                        d      }Y w xY w# t        t        t        t        f$ r d}Y w xY w)z(
        Handle removing items.
        T)r   reverseFN)r   rP   r   r   r   r   r   r   r%   r&   r   r   r   r   rc   r   r[   (_find_closest_iterable_element_for_indexr   r   )rq   rP   sorted_itemrT   r   r   r   r   r   r   rR   r   r   look_for_expected_old_valuer   s                  r4   _do_item_removedzDelta._do_item_removed4  sy   	e D4Q4Q[_`K )4 	Q$D$#==dC`p]&"46JCQUW]*/' )3S=(+D	%w&(/T(:%.?CU.U+ +z#t/DTMqMqDDS$Pbc#(:% I-NN6#57K$f>##D*<>OP9	Q  	e Jt?T?T4U_cdK	e" j.)D 3.2+3s#   &C/ )D./8D+*D+.EEc                     d }t        d      }t        |      D ]+  \  }}t        ||z
        }||kD  r |S ||k(  s"||k  s(|}|}- |S )Ninf)float	enumerateabs)	rq   rR   r   r   closest_elemclosest_distanceindexr   dists	            r4   r  z.Delta._find_closest_iterable_element_for_index\  sp     <%cN 	(LE5ut|$D&&  **t6F/F$#' 	( r3   c                    | j                   j                  di       }|rF|j                         D ]1  \  }}g }| j                  |      }|r#|\  }}}}	}
}}|| }| j                  }
d}t
        }	n@| j                  | |      }
t        |
t              }|D ]  }|j                  dk(  r|j                  |j                         .|j                  dk(  r>|j                  dk(  r|j                  |j                         i|j                  dk(  sy|j                  |
|j                  |j                           |r#t        |
      }
| j                  ||||
|	       -||
d d  4 y y )	NrN   r   rR   r   replacedeleter   equalr   )r:   rO   rP   r   r   r&   ro   rc   r   tagextend
new_valuest1_from_indext1_to_indexr   )rq   rN   rT   opcodestransformedr   r   r   r   r   rR   r   r   is_obj_tupleopcodes                  r4   r   zDelta._do_iterable_opcodesh  s|    IIMM*=rB!2!8!8!: $)g #'#A#A$#G #dtaHf&8:NPSUY[a~!%"ii-3*/6,))dX)F)#u5% YFzzY.#**6+<+<=x/ x/#**6+<+<=w.#**3v/C/CFDVDV+WXY  *C //FSW^p69BV 0 X )CFI$) r3   c                    | j                   j                  di       }| j                   j                  d      }|r7|j                         D ci c]  \  }}||d    }}}|j                  |       |r| j	                  |       y y c c}}w )Nr  r   r   )r:   rO   rP   r   r  )rq   r  r   r   r   removed_dicts         r4   r   zDelta._do_iterable_item_removed  s     $		.Er J"iimm,AB6I6O6O6QRdaAqzMRLR!((6 !!"78 ! Ss   Bc                 b    | j                   j                  d      }|r| j                  |       y y )Ndictionary_item_removedr:   rO   r  )rq   r3  s     r4   r   z!Delta._do_dictionary_item_removed  s-    "&))--0I"J"!!"9: #r3   c                 b    | j                   j                  d      }|r| j                  |       y y )Nattribute_removedr4  )rq   r6  s     r4   r   zDelta._do_attribute_removed  s-     IIMM*=>!!"34 r3   c                 f    | j                   j                  d      }|r| j                  |d       y y )Nset_item_addedunionfuncr:   rO   _do_set_or_frozenset_itemrq   rP   s     r4   r   zDelta._do_set_item_added  s0    		./**5w*? r3   c                 f    | j                   j                  d      }|r| j                  |d       y y )Nset_item_removed
differencer:  r<  r>  s     r4   r   zDelta._do_set_item_removed  s0    		01**5|*D r3   c           
         |j                         D ]u  \  }}t        |      }| j                  | |d d       }|d   \  }}| j                  ||d ||t	                     }	 t        |	|      |      }
| j                  ||||
|       w y )Nr   r#  )r   r   r   r   r   )r   r   r   r   )rP   r"   ro   r   r^   r   r   )rq   rP   r;  rT   r   r   r   r   r   rR   r   s              r4   r=  zDelta._do_set_or_frozenset_item  s     ;;= 	xKD%(.H((THSbM(JF#B<LD&99tSW`fy|y~ : @C*T*51I''tRV^gpv'w	xr3   c              #      K   d}t        |      dz
  }||k  r\|dz  }||   }||v r||v r>|j                  |      }||k(  r/| j                  t        j	                  ||||             | ||k  r[yyw)zp
        A generator that gets the old values in an iterable when the order was supposed to be ignored.
        r   r   N)r   popr   $FAIL_TO_REMOVE_ITEM_IGNORE_ORDER_MSGrj   )	rq   rR   remove_indexes_per_pathfixed_indexes_valuesr   old_obj_indexmax_lencurrent_old_objexpected_obj_to_deletes	            r4   _do_ignore_order_get_oldzDelta._do_ignore_order_get_old  s      c(Q,g%QM!-0O"66 77)@)D)D])S&"&<<&&'K'R'R%'=?UWf(h i!! g%s   A0A53A5c                    | j                   j                  dt                     }| j                   j                  dt                     }t        |j	                               t        |j	                               z  }|D ]  }| j                  dj                  |            }|r|\  }}}}	}
}}n2|j                  |t                     j                         }|j                  |t                     j                         }t        |j                               }g }t        |
t              r|
j                  dkD  }nt        |
      }| j                  |
|||      }|s|rt        |      }||v r#|j!                  |      }|j#                  |       nx|r	 t%        |      }|j#                  |       nX| j)                  t*        j                  |             |j!                  t%        t-        |                  }|j#                  |       |r|rt        |
t.              rt/        |      }| j1                  |||||	        y# t&        $ r d}Y Hw xY w)	a  

            't1': [5, 1, 1, 1, 6],
            't2': [7, 1, 1, 1, 8],

            'iterable_items_added_at_indexes': {
                'root': {
                    0: 7,
                    4: 8
                }
            },
            'iterable_items_removed_at_indexes': {
                'root': {
                    4: 6,
                    0: 5
                }
            }

        iterable_items_added_at_indexes!iterable_items_removed_at_indexesz{}[0]r   )r   Fr   N)r:   rO   r   r!   keysr   rj   rl   r)   valuesrc   r   sizeboolrL  r   rD  rQ   nextStopIterationr   #INDEXES_NOT_FOUND_WHEN_IGNORE_ORDERiterr   r   )rq   fixed_indexesremove_indexespathsrT   r   _r   r   r   rR   fixed_indexes_per_pathrF  rG  new_objthere_are_old_itemsold_item_gennew_obj_indexnew_items                      r4   r   zDelta._do_ignore_order  s+   ( 		&GQ'JEGT=--/0:n>Q>Q>S3TT ,	TD  $==gnnT>RSQaN6-/CS!Q%2%6%6tUW%E%J%J%L"&4&8&8uw&G&L&L&N##)*@*G*G*I#J G#z*&)hhl#&*3i#88,.B[_ 9 aL%)? #G $::599-HHNN8,(1#'#5  x0 &&'J'Q'QRh'ij599$tDZ?[:\]HNN8,! &)?$ #u%. ''F4Vh.5>R ( TW,	T: ) 4.3+4s   IIIc                 h   | j                   st        d      dddddd}t        |j                               D ]
  }||||   <    i }| j                  j                         D ]  \  }}|j                  |      }|r|||<    |dk(  rGi ||<   |j                         D ].  \  }}|j                  d	      r|d	   n|}	|d
   |d   d||   |	<   0 l|dk(  rli ||<   |j                         D ]S  \  }}|j                  d	      r|d	   n|}	|d   |d   d||   |	<   d|v r|d   ||   |	   d
<   d
|v sF|d
   ||   |	   d<   U |dk(  r2i ||<   |j                         D ]  \  }}|d	   }
||d   d||   |
<    |dk(  si ||<   |j                         D ]  \  }}g ||   |<   |D ]  }|j                  }dddj                  ||      }t        ||j                  |j                  |j                  |j                  |j                  |j                        }||   |   j                  |          |S )Nz1Please recreate the delta with bidirectional=Truer  rO  r6  r@  r3  )r   rN  r   r8  r   r   r   r  r   r   r  r   r   r   r   r   r   )r   r   rN   r   r%  )r%  r   r'  r*  r+  t2_from_indext2_to_indexr)  
old_values)rG   rh   r   rP  r:   rP   rO   r'  r   re  rf  r*  r+  rg  r)  rQ   )rq   SIMPLE_ACTION_TO_REVERSEr   r_diffr   inforeverse_actionrT   	path_inforeverse_pathold_pathrU   rV   r'  new_op_codes                  r4   r   zDelta._get_reverse_diff  s   !!PQQ $;/R20%>$
  05578 	JCFI$%=c%BC	J  IIOO- -	ALFD599&AN)-~&++!#v'+zz| OD)<EMM*<U9Z#8[_L%.{%;)T_J`4F6N<0
 >)!#v'+zz| [OD)<EMM*<U9Z#8[_L$-j$9yQ[G\4F6N<0 #i/DMkDZv|4[A"i/DMkDZv|4[A[ 00!#v'+zz| OD)(4H$(9W3E0F6N8,
 ..!#v&*jjl AND(+-F6N4(#+ A%kk)1XFJJ3PST&, #*1*?*?(/(;(;*1*?*?(/(;(;'.'9'9'.'9'9' vt,33K@AA?-	A\ r3   c                     t        | j                  j                  j                        }d|v r| j                  | j                  |       y|j                  | j                                y)z'
        Dump into file object
        file_obj)rq  N)r^   rE   rL   r_   r:   writedumps)rq   fileparam_names_of_serializers      r4   dumpz
Delta.dumpZ  sN     %((@(@(L(L$M!22OODIIO5JJtzz|$r3   c                 8    | j                  | j                        S )zx
        Return the serialized representation of the object as a bytes object, instead of writing it to a file.
        )rE   r:   ry   s    r4   rs  zDelta.dumpsi  s     tyy))r3   c                 ,    t        | j                        S r\   )dictr:   ry   s    r4   to_dictzDelta.to_dicto  s    DIIr3   c                    g }| j                   d   j                         D ]  \  }}|D ]  }|j                  t         ||      t        |j
                     |j                  |j                  t        |j                        t        |j                        d|j                  |j                  |j                  |j                                 |S )z4
        Converts op_codes to FlatDeltaRows
        rN   N)rT   r   r   r  r   r   r   r*  r+  re  rf  )r:   rP   rQ   r   r   r'  r)  rg  r   r*  r+  re  rf  )rq   _parse_pathrS   rT   rU   rV   s         r4   _flatten_iterable_opcodeszDelta._flatten_iterable_opcodesr  s     "ii(;<BBD 	ND(#  (.=gkkJ%00")"4"4!'"4"45!%g&8&8!9!%&-&;&;$+$7$7&-&;&;$+$7$7	$ r3   c              #   &  K   |j                         D ]z  \  }} ||      | d}|D ]$  \  }}	}
||v s|
r |
||         ||	<   ||   ||	<   & |r2d|v rd|vrt        |d         |d<   d|v rd|vrt        |d         |d<   t        di | | y w)N)rT   r   r   r   r  r   r2   )rP   r   r   )r   rj  r|  keys_and_funcsreport_type_changesrT   detailsrowr   new_keyr;  s              r4   _get_flat_rowzDelta._get_flat_row  s     !ZZ\ 	&MD'&t,?C&4 4"Wd'>'+GCL'9G'.s|G4 #c>fC&7"&s7|"4CK#%*C*?&*3{+;&<C
O%%%	&s   /BABc                 >    d | D        }t         j                  |      S )Nc              3   <   K   | ]  }|j                           y wr\   )_asdict).0r   s     r4   	<genexpr>z(Delta._from_flat_rows.<locals>.<genexpr>  s     >!!))+>s   )r9   rk   )r?   r>   s     r4   rm   zDelta._from_flat_rows  s    >~>%%n55r3   c                    i }ddd}| D ]w  }d}|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d	t              }	|st        d
      |t        d      ||v r||   }|j                         }|t        j
                  t        j                  hv r	dt        f}
ndt        f}
t        |t              r|}nt        ||
      }|r||k7  rt        ||
      }nd}||vri ||<   |dv r|||   vri ||   |<   |||   |   |<   nC|t        j                  t        j                  hv r0|||   vrt               ||   |<   ||   |   j                  |       n|t        j                   t        j"                  t        j                  t        j
                  t        j$                  t        j&                  hv r
|||   |<   n|dk(  r"|	t        k(  rd|i||   |<   no||	d||   |<   nb|dk(  r_|j                  dt              }|j                  dt              }d|i||   |<   d|fd|fd	|	ffD ]  \  }}|t        k7  s|||   |   |<    n|t        j(                  k(  rd|i||   |<   n|t        j*                  t        j,                  t        j.                  t        j0                  hv rd|vri |d<   ||d   vrg |d   |<   |d   |   j3                  t5        t6        |   |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d	                   |sm|||   |   d<   z |S )zH
        Create the delta's diff object from the flat_dict_list
        rN  rO  )unordered_iterable_item_addedunordered_iterable_item_removedNr   rT   r   r   r  z'Flat dict need to include the 'action'.z%Flat dict need to include the 'path'.r   r   >   rN  rO  r   r   rc  r   r   r   r   rN   r*  r+  re  rf  rd  )rO   r   rh   rD  r   r   r6  r&   r%   rc   r   r(   r8  r@  r^   addr   r3  r   r  r   iterable_items_insertediterable_items_deletediterable_items_replacediterable_items_equalrQ   r   r    )r>   rS   FLATTENING_NEW_ACTION_MAP	flat_dictr   r   rT   r   r   r  r   r   r  r   r   
elem_values                   r4   rk   zDelta._from_flat_dicts  s   
 -N/R%
! ( _	@IE]]8,F==(DMM'*E }}Z0H!k?CI !JKK| !HII2226:
..00  !'0 &}$$)$\JH,)(NV#!#v   6&>1/1F6N8,27vx(/--//  6&>1/2uF6N8,vx(,,U3446600..2244  ,1vx(++/0;U/CF6N8,=BQZ/[F6N8,>)!fo>$==_E,7+?vx('* ),) D$D*
 "_49Cvx06D >===,3U+;vx(66556633	  'f424F./6*=#>><>F./9*+H5<<:6B&/mmO&D$-MM-$@&/mmO&D$-MM-$@#,==#9#,==#=
 7?vx(4_	@B r3   returnc                 j    | j                  dd      D cg c]  }|j                          c}S c c}w )a  
        Returns a flat list of actions that is easily machine readable.

        For example:
            {'iterable_item_added': {'root[3]': 5, 'root[2]': 3}}

        Becomes:
            [
                {'path': [3], 'value': 5, 'action': 'iterable_item_added'},
                {'path': [2], 'value': 3, 'action': 'iterable_item_added'},
            ]

        
        **Parameters**

        include_action_in_path : Boolean, default=False
            When False, we translate DeepDiff's paths like root[3].attribute1 into a [3, 'attribute1'].
            When True, we include the action to retrieve the item in the path: [(3, 'GET'), ('attribute1', 'GETATTR')]
            Note that the "action" here is the different than the action reported by to_flat_dicts. The action here is just about the "path" output.

        report_type_changes : Boolean, default=True
            If False, we don't report the type change. Instead we report the value change.

        Example:
            t1 = {"a": None}
            t2 = {"a": 1}

            dump = Delta(DeepDiff(t1, t2)).dumps()
            delta = Delta(dump)
            assert t2 == delta + t1

            flat_result = delta.to_flat_dicts()
            flat_expected = [{'path': ['a'], 'action': 'type_changes', 'value': 1, 'new_type': int, 'old_type': type(None)}]
            assert flat_expected == flat_result

            flat_result2 = delta.to_flat_dicts(report_type_changes=False)
            flat_expected2 = [{'path': ['a'], 'action': 'values_changed', 'value': 1}]

        **List of actions**

        Here are the list of actions that the flat dictionary can return.
            iterable_item_added
            iterable_item_removed
            iterable_item_moved
            values_changed
            type_changes
            set_item_added
            set_item_removed
            dictionary_item_added
            dictionary_item_removed
            attribute_added
            attribute_removed
        FT)include_action_in_pathr  )to_flat_rowsr  )rq   r  r  r   s       r4   to_flat_dictszDelta.to_flat_dicts  s9    n "&!2!2%ei!2!j
AIIK
 	
 
s   0c                 H   g }|rt        t        d      }nt        }|rddddddd|fg}n | j                  st        d	      ddddd|fg}d
dd}| j                  j                         D ]4  \  }}|dk(  r"|j                  | j                  |             .|j                  d      r@||v r||   }	|j                         D ]  \  }
} ||
      }
|j                         D ]v  \  }}|
j                         }|r|j                  |df       n|j                  |       |rt        |||	t        |            }nt        |||	      }|j                  |       x  |dv rc|j                         D ]N  \  }
} ||
      }
|D ]<  }|rt        |
||t        |            }nt        |
||      }|j                  |       > P Z|dk(  r|j                         D ]  \  }
} ||
      }
t        |t              r9t        |      dk(  r+t!        t#        |            }|
j                  |       ||   }nmt        |t$        t&        f      r't        |      dk(  r|d   }|
j                  d       d}n0t        |t(              r t        |      dk(  r|j+                         }d}|rt        |
||t        |            }nt        |
||      }|j                  |        r|dv r\|j                         D ]G  \  }
} ||
      }
|rt        |
||t        |            }nt        |
||      }|j                  |       I |dk(  r2|sd}| j-                  |||||      D ]  }|j                  |        	| j-                  |||||      D ]  }|j                  |        7 |S )zO
        Just like to_flat_dicts but returns FlatDeltaRow Named Tuples
        T)include_actions)r   r   N)r   r   N)r  r  N)r   r   N)r   r   Nr   zWhen converting to flat dictionaries, if report_type_changes=False and there are type changes, you must set the always_include_values=True at the delta object creation. Otherwise there is nothing to include.r  r  )rN  rO  rN   )r|  r[  r%   )rT   r   r   r   )rT   r   r   >   r8  r@  r   r   r   r   r8  >   r   r6  r   r  r3  r   r   )r   rj  r|  r  r  )r
   r'   rH   rh   r:   rP   r(  r}  
startswithrl   rQ   r   r   rc   ry  r   rT  rW  r   r   r^   rD  r  )rq   r  r  rS   r|  r  r  r   rj  
new_actionrT   index_to_valuer   r   path2r  rQ  r  s                     r4   r  zDelta.to_flat_rowsH  s    !!*dCK$K(,0*.Z5N -- G 
 ),0Z5	N 0O1R%
! !IIOO- P	'LFD,,d<<<UV  %226v>
,0JJL +(D.&t,D(6(<(<(> 
+u $		1!LL%8!LL/.".Ez`dej`k"lC".Ez"ZCc*
++ AA$(JJL +LD&&t,D!' +.".Df[_`e[f"gC".Df"UCc*++ 22#'::< 'KD%&t,D!%.3u:?"&tE{"3G, %g#ED%=9c%jAo %aA!6#E3/CJ!O %		!1**E&W[\aWbc*E&QMM#&#'$    $(::< 'KD%&t,D**E&W[\aWbc*E&QMM#&' >)*-F--! +#1(; .  'C MM#&'  --! +#1(; .  'C MM#&'SP	'b r3   )error)NNNN)NNN)NN)TF)FT)T)Er.   r/   r0   docr1   r   r   r   r   r   r   bytesr	   r   ry  r   r   r   r   rS  r   ru   rz   rp   r   __radd__r   r   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r   r   r  r  r   r   r   r   r   r   r=  rL  r   r   rv  rs  rz  r}  r  rm   rk   r  r  r2   r3   r4   r9   r9   B   s   G :>"&!%#'-159* +/((,!$)7;%_HgsE478_ SM_ RL	_
 TN_ !d,_ !l!34_ _ _ _ _ !S*_ _ "$_ _   $!_" )1#_$ %_BK5< H"\ 0!d_((D&_P$:$C
1
 ( (    FN4<
O
{Wa@"UH&QP
')^
9;
5
@
E
x"(CTJ>@%*2 & &  6\(: 6 6 j jX8
W[\hWi 8
ttVZ[gVh tr3   r9   __main__)Krl   loggingtypingr   r   r   r   r   r   r	   	functoolsr
   r   collections.abcr   r   deepdiffr   deepdiff.serializationr   r   deepdiff.helperr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   deepdiff.pathr"   r#   r$   r%   r&   r'   r(   deepdiff.anysetr)   	getLoggerr.   ra   r   r   r  r   rE  r~   ri   rn   r   r   r   r  r   rV  r*   r  r  rh   r,   r7   r9   doctesttestmodr2   r3   r4   <module>r     s      A A A ) #   ;       # 
		8	$ N [ V a 'n $ " {  q 'm $.h +1k .; 7  'M #6 k	 		j 	z zz# zGOO r3   