
    e!h)                         d Z ddlZddlZddlZddlZddlZddlZddlZej                  Z	 G d de
      Z G d de
      Z G d de	      Z G d	 d
e      ZddZy)zb
Fast cryptographic hash of Python objects, with a special case for fast
hashing of numpy arrays.
    Nc                       e Zd ZdZd Zy)_ConsistentSetz\Class used to ensure the hash of Sets is preserved
    whatever the order of its items.
    c                     	 t        |      | _        y # t        t        j                  f$ r t        d |D              | _        Y y w xY w)Nc              3   2   K   | ]  }t        |        y wNhash).0es     V/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/joblib/hashing.py	<genexpr>z*_ConsistentSet.__init__.<locals>.<genexpr>%   s     $CT!W$Cs   )sorted	_sequence	TypeErrordecimalInvalidOperation)selfset_sequences     r   __init__z_ConsistentSet.__init__   sF    		E
 $L1DN7334 	E $$Cl$CDDN	Es    0AAN__name__
__module____qualname____doc__r        r   r   r      s    Er   r   c                       e Zd ZdZd Zy)_MyHashz5Class used to hash objects that won't normally picklec                     || _         y r   )args)r   r    s     r   r   z_MyHash.__init__+   s	    	r   Nr   r   r   r   r   r   (   s
    ?r   r   c                      e Zd ZdZd
dZddZd Zd Zdej                  fdZ
ej                  j                         Ze
e ee      <   e
e ee      <   e
e ee      <   e
e eej$                        <   d Zd	 Zee e e             <   y)HasherzA subclass of pickler, to do cryptographic hashing, rather than
    pickling. This is used to produce a unique hash of the given
    Python object that is not necessarily cryptographically secure.
    c                     t        j                         | _        d}t        j	                  | | j                  |       t        j                  |d      | _        y )N   )protocolF)usedforsecurity)ioBytesIOstreamPicklerr   hashlibnew_hash)r   	hash_namer%   s      r   r   zHasher.__init__5   sA    jjl t{{X>[[EB
r   c                 :   	 | j                  |       | j                  j                         }| j                  j                  |       |r| j                  j                         S y # t        j                  $ r"}|xj                  d|d|fz  c_         d }~ww xY w)NzPicklingError while hashing z: )	dumppicklePicklingErrorr    r)   getvaluer-   update	hexdigest)r   objreturn_digestr   dumpss        r   r	   zHasher.hash>   s    	IIcN $$&

% ::''))  ## 	FFsAFHHF	s   A% %B8BBc                    t        |t        j                  t        i j                        f      rt        |d      r|j                  j                  }n|j                  }|j                  }t        |      t        t              u rt        ||j                        }n2|t        ||      }n#|j                  j                  }t        |||      }t        j                  | |       y )N__func__)
isinstancetypes
MethodTypetypepophasattrr:   r   __self__r1   r   	__class__r*   save)r   r6   	func_nameinstclss        r   rC   zHasher.saveI   s    cE,,d266l;< sJ'LL11	LL	<<DDzT&\)i7i.ll,,is3T3r   c                 ^    t        |t        t        f      ry t        j	                  | |       y r   )r;   bytesstrr*   memoize)r   r6   s     r   rJ   zHasher.memoize\   s#    
 cE3<(c"r   Nc                 X   t        ||      }|d= 	 t        j                  | |fi | y # t        j                  $ rl t        j                  | |fi | t        |dd       }|dk(  r@|}||j                  }t        j                  |   }t        ||      st        |||       Y y Y y Y y w xY w)N)namepackrM   r   __main__)dictr*   save_globalr1   r2   getattrr   sysmodulesr@   setattr)r   r6   rL   rM   kwargsmodulemy_namemods           r   rP   zHasher.save_globalg   s     4d+6N	/c4V4## 	/c4V4S,5F#?!llGkk&)sG, C#. - $	/s   * A8B)(B)c           
          	 t        j                  | t        t        |            g|  y # t        $ r3 t        j                  | t        t        d |D                    g|  Y y w xY w)Nc              3   <   K   | ]  \  }}t        |      |f  y wr   r   )r
   kvs      r   r   z)Hasher._batch_setitems.<locals>.<genexpr>   s     !A41a47A,!As   )r*   _batch_setitemsiterr   r   )r   itemsr    s      r   r]   zHasher._batch_setitems   sc    	
 ##D$ve}*=EE 	 ##d6!A5!AABEI	s   ), 9A('A(c                 B    t         j                  | t        |             y r   )r*   rC   r   )r   	set_itemss     r   save_setzHasher.save_set   s    T>)45r   )md5)T)r   r   r   r   r   r	   rC   rJ   structrM   rP   r*   dispatchcopyr>   lenobjectr1   r0   r]   rb   setr   r   r   r"   r"   /   s    
C	* &# %)v{{ /* $$&H%HT#Y(HT&\)HT']"-HT&++6 %HT#%[r   r"   c                       e Zd ZdZddZd Zy)NumpyHasherz1Special case the hasher for when numpy is loaded.c                     || _         t        j                  | |       ddl}|| _        t        |d      r|j                  | _        yt        | _        y)z
        Parameters
        ----------
        hash_name: string
            The hash algorithm to be used
        coerce_mmap: boolean
            Make no difference between np.memmap and np.ndarray
            objects.
        r.   r   N	getbuffer)	coerce_mmapr"   r   numpynpr@   rn   
_getbuffer
memoryview)r   r.   ro   rq   s       r   r   zNumpyHasher.__init__   sB     '	22{# llDO(DOr   c                    t        || j                  j                        rF|j                  j                  s/|j
                  dk(  r|j                         }nL|j                  j                  r|}n3|j                  j                  r|j                  }n|j                         }| j                  j                  | j                  |j                  | j                  j                                     | j                   r7t        || j                  j"                        r| j                  j                  }n|j$                  }|d|j                  |j
                  |j&                  ff}nyt        || j                  j                        rY| j                  j                  dj)                  d             | j                  j                  t+        j,                  |             yt.        j1                  | |       y)zSubclass the save method, to hash ndarray subclass, rather
        than pickling them. Off course, this is a total abuse of
        the Pickler class.
        r   HASHED_HASHED_DTYPEzutf-8N)r;   rq   ndarraydtype	hasobjectshapeflattenflagsc_contiguousf_contiguousTr-   r4   rr   viewuint8ro   memmaprB   stridesencoder1   r8   r"   rC   )r   r6   obj_c_contiguousklasss       r   rC   zNumpyHasher.save   sX   
 c477??+CII4G4G yyB $';;= ''#& ''#&55 
 $';;=  JJdoo.>.C.CDGGMM.RST
 JsDGGNN$C 
 8SYY		3;;GHCTWW]]+  JJo44W=>JJfll3/0D#r   Nrc   F)r   r   r   r   r   rC   r   r   r   rk   rk      s    ;)*>r   rk   c                     d}||vrt        dj                  ||            dt        j                  v rt	        ||      }nt        |      }|j                  |       S )aI  Quick calculation of a hash to identify uniquely Python objects
    containing numpy arrays.

    Parameters
    ----------
    hash_name: 'md5' or 'sha1'
        Hashing algorithm used. sha1 is supposedly safer, but md5 is
        faster.
    coerce_mmap: boolean
        Make no difference between np.memmap and np.ndarray
    )rc   sha1zAValid options for 'hash_name' are {}. Got hash_name={!r} instead.rp   )r.   ro   rm   )
ValueErrorformatrR   rS   rk   r"   r	   )r6   r.   ro   valid_hash_nameshashers        r   r	   r	      sf     '((OVV )
 	

 #++ykJ),;;sr   r   )r   r   r+   r'   r1   rd   rR   r<   _Picklerr*   rh   r   r   r"   rk   r	   r   r   r   <module>r      sg      	   
 
//EV E&f l%W l%^V& Vrr   