
    	e4                     <   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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ddlmZ ddlZddlmZ dZdZi Z ej                    Z  edefi                       Zeeeee e! ed          fZ"d	Z#d	Z$d	Z%d	Z&e
j'        d
k    rej(        Z)nej*        Z) G d de          Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5i ee-ed ed e d  ed          d e6e1ee!fd e7e8e9fe0ej:        d ej;        e2ej<        j=        d ej>        ej?        fe5ej@        d de4d e3d!e3d"e3d#e3d$e1d%d& d'd( d)d* d+d, ejA        d- ejB        d. d/ d0 i	ZCeD ]ZDd1eCeD<   d2 ZEd3 ZFd4 ZGd5 ZHd6 ZId7 ZJed8             ZKd9 ZL	 	 d>d=ZMdS )?z?
Implements memoization for functions with arbitrary arguments
    N)contextmanager   )states.   panel-93KZ39Q-floatingdangeroushomechose-CYCLE)z_cffi_backend.FFIzbuiltins.CompiledFFIINDETERMINATEi win32c                   &    e Zd Zd Zd Zd Zd ZdS )_Stackc                     i | _         d S N)_stackselfs    .lib/python3.11/site-packages/panel/io/cache.py__init__z_Stack.__init__:   s        c                 4    || j         t          |          <   d S r   )r   idr   vals     r   pushz_Stack.push=   s    "BsGGr   c                 8    | j                                          d S r   )r   popitemr   s    r   popz
_Stack.pop@   s    r   c                 .    t          |          | j        v S r   )r   r   r   s     r   __contains__z_Stack.__contains__C   s    #ww$+%%r   N)__name__
__module____qualname__r   r   r   r    r   r   r	   r	   8   sP          # # #  & & & & &r   r	   c                 J    t          |           }|j        }|j        }|d|S )z&Get module.type_name for a given type..)typer   r   )objthe_typemodulenames       r   _get_fqnr'   F   s.    CyyH F Dffdd##r   c                 f    |                                  dz   dz  }|                     |dd          S )N   littleT)signed)
bit_lengthto_bytes)i	num_bytess     r   _int_to_bytesr0   M   s1    !#)I::i$:777r   c                 ,    t          | t                    S r   )
isinstance_NATIVE_TYPESr#   s    r   
_is_nativer5   Q   s    c=)))r   c                 ^    t          | t                    ot          d | D                       S )Nc              3   4   K   | ]}t          |          V  d S r   )_is_native_tuple).0vs     r   	<genexpr>z#_is_native_tuple.<locals>.<genexpr>U   s+      )K)K!*:1*=*=)K)K)K)K)K)Kr   )r2   tupleallr4   s    r   r8   r8   T   s.    c5!!Kc)K)Ks)K)K)K&K&KKr   c                 `   t          j        d          }|                    t          dt	          |           j                              t          | t                    r|                                 n| D ]$}|                    t          |                     %|	                                S )Nmd5__)
hashlibnewupdate_generate_hashr"   r   r2   dictitemsdigest)r#   hitems      r   _container_hashrJ   W   s    EAHH^5c!35566777 *3 5 5>3 ' '	%%&&&&88::r   c                 D    t          | j        | j        | j        g          S r   )rJ   startstepstopxs    r   _slice_hashrQ   ^   s    AGQVQV4555r   c                 <   t          j        d          }|                    t          | j                             |                    t          | j                             |                    t          | j                             |                                S Nr?   )rA   rB   rC   rD   argsfunckeywordsrG   r#   rH   s     r   _partial_hashrX   a   ss    EAHH^CH%%&&&HH^CH%%&&&HH^CL))***88::r   c                    dd l }t          | |j        |j        f          s|                    |           } t	          |           t
          k    r|                     t          d          } 	 d|j        	                    |           
                                z  S # t          $ r% dt          j        | t          j                  z  cY S w xY w)Nr   )nrandom_states   %s)pandasr2   Series	DataFramelen_PANDAS_ROWS_LARGEsample_PANDAS_SAMPLE_SIZEutilhash_pandas_objectsum	TypeErrorpickledumpsHIGHEST_PROTOCOL)r#   pds     r   _pandas_hashrk   h   s    cBIr|455 iinn
3xx%%%jj.Qj??Brw11#66::<<<< B B B v|C)@AAAAAABs   +.B ,C	C	c                    t          j        d          }|                    t          | j                             | j        t          k    r:dd l}|j        	                    d          } |j
        | j        t                    } |                    |                                            |                                S )Nr?   r   )size)rA   rB   rC   rD   shaperm   _NP_SIZE_LARGEnumpyrandomRandomStatechoiceflat_NP_SAMPLE_SIZEtobytesrG   )r#   rH   npr   s       r   _numpy_hashrx   w   s    EAHH^CI&&'''
x>!!	%%a((el38/:::HHS[[]]88::r   c                 "   t          j        d          }|                    t          |                                                      |                    t          |                                                      |                                S rS   )rA   rB   rC   rD   tellgetvaluerG   rW   s     r   _io_hashr|      sc    EAHH^CHHJJ''(((HH^CLLNN++,,,88::r   c                 *    |                                  S r   )encoder4   s    r   <lambda>r      s    szz|| r   c                 :    t          t          |                     S r   )r0   hashr4   s    r   r   r      s    }T#YY77 r   c                     | du rdndS )NT   1   0r   r4   s    r   r   r      s    sd{{tt r   c                     dS )Nr   r   r4   s    r   r   r      s    t r   c                     | S r   r   r4   s    r   r   r      s    S r   c                 D    t          |                                           S r   )strr~   r4   s    r   r   r      s    SXX__%6%6 r   c                 :    t          t          |                     S r   )r0   r   r4   s    r   r   r      s    ]2c77%;%; r   c                 X    t          |           j         |                      d          S )Nzutf-8)r"   r   r~   r4   s    r   r   r      s(    $s))"4;c;;BB7KK r   numpy.ndarrayzpandas.core.series.Seriespandas.core.frame.DataFramezpandas.core.indexes.base.Indexz&pandas.core.indexes.numeric.Int64Indexz$pandas.core.indexes.range.RangeIndexzbuiltins.mappingproxyc                 :    t          t          |                     S r   rJ   rE   r4   s    r   r   r          tCyy/I/I r   zbuiltins.dict_itemsc                 :    t          t          |                     S r   r   r4   s    r   r   r      r   r   zbuiltins.getset_descriptorc                 4    | j                                         S r   )r   r~   r4   s    r   r   r      s    s/?/F/F/H/H r   znumpy.ufuncc                 4    | j                                         S r   r   r~   r4   s    r   r   r      s    s|/B/B/D/D r   c                 4    | j                                         S r   r   r4   s    r   r   r      s    S\-@-@-B-B r   c                     | j         S r   )r   r4   s    r   r   r      s    S\ r   c                 8    t          | d          o
| j        dk    S )Nrv   r   )hasattrrn   rO   s    r   r   r      s    ga##52 r   c                 *    |                                  S r   )rv   rO   s    r   r   r      s     r   r   c                 >   t          |           }|t          v rt          |         S t                                          D ]]\  }}t          |t                    r||k    r|c S %t          j        |          r ||           r|c S It          | |          r|c S ^d S r   )r'   _hash_funcsrF   r2   r   inspect
isfunction)r#   fqn_typeotype	hash_funcs       r   _find_hash_funcr      s    }}H;8$$'--//  yeS!! 	       !&& 	uSzz !    !U## 		 r   c                 H   t          |           }|O	  ||           }n@# t          $ r3}t          d|d| dt          |          j         d| d	          d }~ww xY w|S t          | d          rt          j        d          }	 |                                 }n2# t          $ r% t          dt          |           j                   w xY w|D ]$}|	                    t          |                     %|                                S t          t          |                     S )	NzUser hash function z failed for input z with following error: z("z").
__reduce__r?   zCould not hash object of type )r   BaseException
ValueErrorr"   r   r   rA   rB   r   rC   rD   rG   r0   r   )r#   r   outputerH   reduce_datarI   s          r   _generate_hash_innerr      sv   $$I	Ys^^FF 	 	 	Li L LL L15a1AL LEFL L L  	
 sL!! K	T..**KK 	T 	T 	TRd3ii>PRRSSS	T 	+ 	+DHH^D))****xxzzC!!!s     
A.AAB /C
c                     t           j        }| |v rt          S |                    |            	 t	          |           }|                                 n# |                                 w xY w|S r   )r   _current_stack_CYCLE_PLACEHOLDERr   r   r   )r#   
hash_stack
hash_values      r   rD   rD      sn    %J
j!!OOC)#..

s   A A(c                    | d S t          |           st          |           r| S t          | t                    r t	          d | D                       rdg| R S nqt          |           dk    sOt          |           dk    s<t          j        |           s(t          j        |           st          j	        |           rt          |           S t          S )Nc              3   4   K   | ]}t          |          V  d S r   )r5   )r9   rI   s     r   r;   z_key.<locals>.<genexpr>   s*      00Dz$000000r   __listr   r   )r5   r8   r2   listr=   r'   r   	isbuiltin	isroutineiscoder   _INDETERMINATEr4   s    r   _keyr      s    
{t	C ,S11 
	C		 
00C00000 	$#s###	$ 	666C==O++S!! ,S!! , ># ,
 #wwr   c                    t          |           |k    r|                                dk    r(t          |                                           d         }n|                                dk    r=t	          fd|                                 D             d           d         d         }nH|                                dk    r0t	          |                                 d	           d         d         }| |= t          |           |k    d
S d
S )zs
    Deletes items in the cache if the exceed the number of items or
    their TTL (time-to-live) has expired.
    fifor   lruc              3   6   K   | ]\  }\  }}}}||z
  fV  d S r   r   )r9   k_ttimes       r   r;   z!_cleanup_cache.<locals>.<genexpr>   s7      KK/!\aAq1d1f+KKKKKKr   c                     | d         S )Nr   r   os    r   r   z _cleanup_cache.<locals>.<lambda>   s
    qt r   )keylfuc                     | d         d         S )Nr      r   r   s    r   r   z _cleanup_cache.<locals>.<lambda>   s    ad1g r   N)r_   lowerr   keyssortedrF   )cachepolicy	max_itemsr   r   s      ` r   _cleanup_cacher      s   
 e**	
!
!<<>>V##uzz||$$Q'CC\\^^u$$KKKKU[[]]KKK+^- - --/1124CC\\^^u$$,=,=>>>qA!DC#J e**	
!
!
!
!
!
!r   c                 v    t          |                                           D ]\  }\  }}}}||z
  |k    r| |= dS )zM
    Deletes items in the cache if their TTL (time-to-live) has expired.
    N)r   rF   )r   ttlr   r   r   tss         r   _cleanup_ttlr      sO     #5;;==11  ]aQGs??c
 r   c              #   F  K   t          t                    }t                              |            	 d V  t                                           t                              |           d S # t                                           t                              |           w xY wr   )rE   r   rC   clear)
hash_funcsbackups     r   _override_hash_funcsr     s      +Fz"""#6""""" 	6""""s   A+ +5B c                    | t          |          t          |          f}t          |vr|t          v rt          |         S t          j        d          }t          |          5  |r"|                    t          |                     |r"|                    t          |                     ddd           n# 1 swxY w Y   |                                }t          |vr
|t          |<   |S )a<  
    Computes a hash given a function and its arguments.

    Arguments
    ---------
    func: callable
        The function to cache.
    hash_funcs: dict
        A dictionary of custom hash functions indexed by type
    args: tuple
        Arguments to hash
    kwargs: dict
        Keyword arguments to hash
    r?   N)	r   r   	_HASH_MAPrA   rB   r   rC   rD   	hexdigest)rU   r   rT   kwargsr   hasherr   s          r   compute_hashr     s    dT&\\
*CS  SI%5%5~[F	j	)	) 2 2 	0MM.../// 	2MM.00111	2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 !!##JS  #	#s   "A	B77B;>B;LRUF./cachec                    
                                  dvrt          d d          pi  	fdS d}t          j                    
t	          j                    
f
d            dfd	}	|	_        r<t          j        r0t          j        j	        rt          j        
                    |	           	 j                             j                   n# t          $ r Y nw xY wS )	a
  
    Memoizes functions for a user session. Can be used as function annotation or just directly.

    For global caching across user sessions use `pn.state.as_cached`.

    Arguments
    ---------
    func: callable
        The function to cache.
    hash_funcs: dict or None
        A dictionary mapping from a type to a function which returns
        a hash for an object of that type. If provided this will
        override the default hashing function provided by Panel.
    policy: str
        A caching policy when max_items is set, must be one of:
          - FIFO: First in - First out
          - LRU: Least recently used
          - LFU: Least frequently used
    ttl: float or None
        The number of seconds to keep an item in the cache, or None if
        the cache should not expire. The default is None.
    to_disk: bool
        Whether to cache to disk using diskcache.
    cache_dir: str
        Directory to cache to on disk.
    per_session: bool
        Whether to cache data only for the current session.
    )r   r   r   z8Cache policy must be one of 'FIFO', 'LRU' or 'LFU', not r!   Nc                 .    t          |           S )N)rU   r   r   r   to_disk
cache_path)r   )fr   r   r   r   r   s    r   r   zcache.<locals>.<lambda>V  s(    !!
 
 
 r   c                  l  
  j         } o@t           d         t                    o%t          t	           d                   |d           u } |}}|rt           d         t
          j                  rlt          di           }t           fd|                    dd          D                        dd          z   }t          |                    di           fi |}t          ||          }t                      }t          j        j                 }	j        dk    rdn|	j        }
|r#|
t	           d                   j         j         fan	|
j         far"t           t#          t$          j                  fz  at)          j        t-          t                                                               at$          j                            t                     }|Or5dd	lm}  |t6          j                            t                               }ni }|xt$          j        t           <   }t=          ||           ||v r55  ||         \  }}}}|||dz   |f||<   |cd d d            S # 1 swxY w Y   t?          ||             i |}5  ||d|f||<   d d d            n# 1 swxY w Y   |S )
Nr   _dinfoc              3   D   K   | ]}t          d          |          V  dS )r   N)getattr)r9   drT   s     r   r;   z.cache.<locals>.wrapped_func.<locals>.<genexpr>n  s1      YYagd1gq11YYYYYYr   dependenciesr   r   kw__main__Index) r   r2   objectr   r"   paramParameterizedr<   getrE   r   _TIME_FNsysmodulesr   __file__	func_hashr   r   curdocrA   sha256rD   r   _memoize_cache	diskcacher   ospathjoinr   r   )rT   r   	func_name	is_method	hash_argshash_kwargsdinfor   r   r%   fname
func_cacher   r   retr   countr   r   rU   r   lockr   per_sessionr   r   r   wrapped_funcs   `                 r   r	  zcache.<locals>.wrapped_funcb  sg    M	 DZQ00 DDaMM9d33|C 	 "&v;	 	>*T!We.ABB 	>L(B77EYYYY599^UW;X;XYYYYY\`abacac\ddIuyyr22==f==K!$
I{KK
zz
 T_-"o;;

 	/T!W 6FII.I 	-"U\**,,IN>)#<#<==GGII	)--i88
 ++++++bgll:yAABB;@@E +j?S$///##  $.z$:!R*-r57D)A
:&               
  :vy$???dD#F## 	: 	:&)4D%9Jz"	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:
s$   I**I.1I.
J))J-0J-c                 "   t           d S rIddlm}  |t          j                            t                               }|                                 n%t          j        	                    t           i           }|                                 d S )Nr   r   )
r   r   r   r   r   r   r   r   r   r   )session_contextr   r   r   r   s      r   r   zcache.<locals>.clear  s    F 	<''''''E"',,z9==>>EKKMMMM(,,Y;;Er   r   )r   r   	threadingRLock	functoolswrapsr   r   r   r  on_session_destroyed__dict__rC   AttributeError)rU   r   r   r   r   r   r   r  r   r   r  r	  s   ````````  @@r   r   r   /  s   @ ||~~333PvPPP
 
 	
 !rJ|
 
 
 
 
 
 
 
 	
 I?D_T7 7 7 7 7 7 7 7 7 7 7 7 7 7r
 
 
 
 
 
 
 L 1u| 1(D 1))%000$$T]3333    s   C- -
C:9C:)NNNr   NFr   F)N__doc__datetimedtr  rA   r   ior   pathlibrg   r   r  r   unittestunittest.mockweakref
contextlibr   r   r   r   _FFI_TYPE_NAMESr   WeakKeyDictionary_HASH_STACKSr"   r   r   bytesr   floatintbool	bytearrayr3   ro   ru   r`   rb   platformperf_counterr   	monotonicr	   r'   r0   r5   r8   rJ   rQ   rX   rk   rx   r|   slicer   r<   rE   PathpartialmockMockStringIOBytesIOdater   ismoduler   r&   r   r   rD   r   r   r   r   r   r   r   r   r   <module>r0     si              				 				   



            % % % % % %        G @	(w(**5oy"5577 
3sD)TT$ZZ   <7 HH~H& & & & &V & & &$ $ $8 8 8* * *L L L  6 6 6  B B B    = ++ 
77	
 	<< 	DJJ## 
; I 
5$ L66  M;; ["*x GKK  ;!"  <#$ "<%& %l'( -l*K#I#I#I#I #H#H#D#D!B!B!9!9557L7L; @   DK  " " ",
 
 
  $     # # #  > 8=?DB B B B B Br   