
    :QgP2                        d dl Z d dlZd dl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 d dlmZmZ dZ G d d      Z eg       fdZdd	Zdd
ZdZefdZddededefdZ dedefdZ!ddZ"d Z#d Z$d Z%d Z&e
efej                  e#fejN                  e$fejP                  e%fejR                  e&fgZ*ddZ+y)    N)DeepHash)
DELTA_VIEWnumbersstringsadd_to_frozen_set	not_foundonly_numbersnp
np_float64time_to_secondscartesian_product_numpy
np_ndarraynp_array_factory,get_homogeneous_numpy_compatible_type_of_seqdict_CannotCompare)MappingIterablezTDistance calculation can not happen once the cache is purged. Try with _cache='keep'c                   ,    e Zd Zd ZddZd Zd Zd Zy)DistanceMixinc                    t        | j                  | j                  | j                  | j                  | j
                        }|t        ur|S | j                  t        k(  r| n| j                  d      }t        |      }|dk(  ry| j                  | j                        }| j                  | j                        }|||z   z  S )aJ  
        Gives a numeric value for the distance of t1 and t2 based on how many operations are needed to convert
        one to the other.

        This is a similar concept to the Levenshtein Edit Distance but for the structured data and is it is designed
        to be between 0 and 1.

        A distance of zero means the objects are equal and a distance of 1 is very far.

        Note: The distance calculation formula is subject to change in future. Use the distance results only as a
        way of comparing the distances of pairs of items with other pairs rather than an absolute distance
        such as the one provided by Levenshtein edit distance.

        Info: The current algorithm is based on the number of operations that are needed to convert t1 to t2 divided
        by the number of items that make up t1 and t2.
        max_use_log_scalelog_scale_similarity_thresholdF)report_repetition_requiredr   )get_numeric_types_distancet1t2cutoff_distance_for_pairsr   r   r   viewr   _to_delta_dict_get_item_length%_DistanceMixin__get_item_rough_length)self	_distanceitemdiff_lengtht1_lent2_lens         N/var/www/html/answerous/venv/lib/python3.12/site-packages/deepdiff/distance.py_get_rough_distancez!DistanceMixin._get_rough_distance   s    $ /GGTWW4#A#AQUQcQc  EI  Eh  Ehi	 I%yyJ.tD4G4Gch4G4i&t,!--dgg6--dgg6fvo..    c                     t        | d      st        t              t        j                  | j
                  |dd      }|4| j                  |       t        j                  | j
                  |dd      }|S )a-  
        Get the rough length of an item.
        It is used as a part of calculating the rough distance between objects.

        **parameters**

        item: The item to calculate the rough length for
        parent: It is only used for DeepHash reporting purposes. Not really useful here.
        hashesN   )keydefaultextract_index)hasattrRuntimeErrorDISTANCE_CALCS_NEEDS_CACHEr   get_keyr/   '_DistanceMixin__calculate_item_deephash)r%   r'   parentlengths       r+   __get_item_rough_lengthz%DistanceMixin.__get_item_rough_length3   sh     tX&9::!!$++4UVW>**40%%dkktTYZ[Fr-   c                 L    t        |f| j                  ddd| j                   y )NrootT)r/   r9   
apply_hash)r   r/   deephash_parameters)r%   r'   s     r+   __calculate_item_deephashz'DistanceMixin.__calculate_item_deephashE   s/    	
;;		

 &&	
r-   c                    t               }|D ]`  }|D ]Y  }	 | j                  ||   j                  ||   j                        }	|	r| j                  xs d}
nd}
|
|dj	                  ||      <   [ b |S # t
        $ r Y kw xY w)Ngư>r0   {}--{})r   iterable_compare_funcr'   math_epsilonformatr   )r%   hashes_addedhashes_removedt1_hashtablet2_hashtable_original_typepre_calced_distances
added_hashremoved_hashis_close_distancedistances              r+   -_precalculate_distance_by_custom_compare_funcz;DistanceMixin._precalculate_distance_by_custom_compare_funcN   s      %w& 	_J . _
_(,(B(B<PZC[C`C`bno{b|  cB  cB  )C% )#'#4#4#@#$V^(\)RS_	_ $# % s   ,A22	A>=A>c                 x   t        |t        t        |               j                  t        t
        f      ry t               }|D cg c]  }||   j                   }}|D cg c]  }||   j                   }	}|t        |      }
t        |	      }|
r|
|k(  r|
}|y t        ||      }t        |	|      }	t        ||	      }|j                  }t        |d   |d   | j                  | j                  | j                        }d}|D ]&  }|D ]  }||   |dj                  ||      <   |dz  }! ( |S c c}w c c}w )N)dtyper   r0   r   rB   )
isinstancenextiterr'   r   listr   r   r   r   T_get_numpy_array_distancer    r   r   rE   )r%   rF   rG   rH   rI   rJ   rK   kaddedremovedadded_numpy_compatible_typeremoved_numpy_compatible_typepairspairs_transposed	distancesirL   rM   s                     r+   #_precalculate_numpy_arrays_distancez1DistanceMixin._precalculate_numpy_arrays_distanceb   sc    l4\(:#;<AAJPTCUV$w/;<!a%%<<1?@A<?''@@!*VW\*]',XY`,a)*/JNk/k!<! n="7.A'w7 77-Q!1!!4//,,+/+N+N	
	 & 	J . R[\]R^$X__Z%NOQ	 $#= =@s   D2!D7N)r=   )__name__
__module____qualname__r,   r$   r8   rP   rb    r-   r+   r   r      s    !/F$
$(&$r-   r   c                    d}t        | t              r| j                         D ]  \  }}|dv r{t               }|j                         D ]\  \  }}t	               }t               }	|j                         D ]+  \  }
}t        |      }||vs|j                  |       ||	|
<   - |	||<   ^ |}t        |t              r|j                  d      s
|dk(  s|dk(  rt        |      }|r||v rt        ||      }|t        ||      z  } |S t        | t              rd}|S t        | t              rd}|S t        | t              r6| D ]/  }t        |      }|r||v rt        ||      }|t        ||      z  }1 |S t        | t              rd}|S t        | d      r7| j                  D ](  }t        |      }t        ||      }|t        ||      z  }* |S )z
    Get the number of operations in a diff object.
    It is designed mainly for the delta view output
    but can be used with other dictionary types of view outputs too.
    r   >   iterable_items_added_at_indexes!iterable_items_removed_at_indexes_deep_distancenew_pathr0   __dict__)rS   r   itemsr   setidaddr   
startswithr   r#   r   r   typer4   rm   )r'   parents_idsr:   r1   subitemnew_subitempath_indexes_to_itemsused_value_idsnew_indexes_to_itemsrY   vv_iditem_idparents_ids_addeds                  r+   r#   r#      s    F$  JJL 	CLC^^#g/6}} >+E+%(UN+07( 0 6 6 8 81!!u~5*..t46703	8
 *>K&> & #w'S^^C-@C?DZ^aeo^okGw+5 1+w G&w0ABBF/	CV M' 
D'	"$ M# 
D'	"  M 
D(	# 	CGkGw+5 1+w G&w0ABBF	C M 
D$	 M 4$== GW+$5k7$K!*74EFFG Mr-   c                 &   | |k(  ry|rt        | |      }|t         k  ry|S t        | t              st        |       } t        |t              st        |      }| |z   |z  }|dk(  r|S 	 t        |t	        | |z
  |z              S # t
        $ r |cY S w xY w)
    Get the distance of 2 numbers. The output is a number between 0 to the max.
    The reason is the
    When max is returned means the 2 numbers are really far, and 0 means they are equal.
    r   )logarithmic_distancerS   floatminabs	Exception)num1num2r   r   r   rO   divisors          r+   _get_numbers_distancer      s     t|'d3**dE"T{dE"T{
 d{d"G!|4dTkW4566 s   'B BBc                     t        j                  | j                  |t              }t        j                  | |||dk7  t              }d|| |k(  <   |S )N)shape
fill_valuerR   r   )outwhererR   )r
   fullr   r   divide)abreplace_inf_with	max_arrayresults        r+   
_numpy_divr      sE    agg2B*UIYYq!!q&
KFF16NMr-   g|=c                     t        j                  |       |z   }t        j                  |      }t        j                  ||       }|S N)r
   r   logcopysign)arrayoffsetabs_plus_offset
log_valuessigned_log_valuess        r+   numpy_apply_log_keep_signr      s=    ffUmf,O (J J6r-   r   r   	thresholdc                      t        | |      |k  S )z
    A threshold of 0.1 translates to about 10.5% difference.
    A threshold of 0.5 translates to about 65% difference.
    A threshold of 0.05 translates to about 5.1% difference.
    )r   )r   r   r   s      r+   logarithmic_similarityr      s      1%	11r-   c                 .   t        |       } t        |      }t        j                  t        j                  t	        |       t
        z         |       }t        j                  t        j                  t	        |      t
        z         |      }t	        ||z
        S r   )r   mathr   r   r   MATH_LOG_OFFSET)r   r   log_alog_bs       r+   r   r      sg    aAaAMM$((3q6O#;<a@EMM$((3q6O#;<a@Euu}r-   c                     |rt        |       } t        |      }| |z   |z  }t        | |z
  ||      }t        j                  t        j                  |      d|      }|rd|||k  <   |S )r   )r   r   )r   r   r
   clipabsolute)r   r   r   r   r   r   r   distance_arrays           r+   rX   rX   	  so     (.(.d{d"GwFFWWR[[0!T:NJK~(FFGr-   c                 T    t        | j                         |j                         |      S r   )r   	timestampdate1date2r   r   r   s        r+   _get_datetime_distancer           !2EOO4EtLLr-   c                 T    t        | j                         |j                         |      S r   )r   	toordinalr   s        r+   _get_date_distancer   $  r   r-   c                 T    t        | j                         |j                         |      S r   )r   total_seconds)
timedelta1
timedelta2r   r   r   s        r+   _get_timedelta_distancer   (  s$     !9!9!;Z=U=U=WY]^^r-   c                 @    t        t        |       t        |      |      S r   )r   r   )time1time2r   r   r   s        r+   _get_time_distancer   ,  s     !79OQUVVr-   c                 x    t         D ],  \  }}t        | |      st        ||      s  || ||||      c S  t        S r   )TYPES_TO_DIST_FUNCrS   r   )r   r   r   r   r   type_funcs          r+   r   r   9  sF    ) YtdE"z$'>dD-9WXXY r-   )r0   F皙?)r0   )r   )Fr   ),r   datetimedeepdiff.deephashr   deepdiff.helperr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   collections.abcr   r   r6   r   	frozensetr#   r   r   r   r   r   r   r   rX   r   r   r   r   date	timedeltatimer   r   rf   r-   r+   <module>r      s     &     . t z$ z$z (1} 3l< ,; 
2g 2' 2e 2G  .MM_W
 ()./]]&'01]]&' r-   