
    BeN8                     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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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 Z0d Z1d Z2d Z3d Z4d Z5d Z6d Z7i e!e/ed e d e"d  ed          d e8e3ee#fd e9e:e;fe2ej<        d ej=        e4ej>        j?        d ej@        ejA        fe7ejB        d d e6d!e5d"e5d#e5d$e5d%e3d&d' d(d) d*d+ d,d- ejC        d. ejD        d/ d0 d1 i	ZEeD ]ZFd2eEeF<   d3 ZGd4 ZHd5 ZId6 ZJd7 ZKd8 ZLed9             ZMd: ZN	 	 d?d>ZOdS )@z?
Implements memoization for functions with arbitrary arguments
    N)contextmanager)iscoroutinefunction   )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.popB   s    r   c                 .    t          |          | j        v S r   )r   r   r   s     r   __contains__z_Stack.__contains__E   s    #ww$+%%r   N)__name__
__module____qualname__r   r   r   r    r   r   r
   r
   :   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(   H   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_bytesr1   O   s1    !#)I::i$:777r   c                 ,    t          | t                    S r   )
isinstance_NATIVE_TYPESr$   s    r   
_is_nativer6   S   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>W   s+      )K)K!*:1*=*=)K)K)K)K)K)Kr   )r3   tupleallr5   s    r   r9   r9   V   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   r3   dictitemsdigest)r$   hitems      r   _container_hashrK   Y   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   )rK   startstepstopxs    r   _slice_hashrR   `   s    AGQVQV4555r   c                 <   t          j        d          }|                    t          | j                             |                    t          | j                             |                    t          | j                             |                                S Nr@   )rB   rC   rD   rE   argsfunckeywordsrH   r$   rI   s     r   _partial_hashrY   c   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)pandasr3   Series	DataFramelen_PANDAS_ROWS_LARGEsample_PANDAS_SAMPLE_SIZEutilhash_pandas_objectsum	TypeErrorpickledumpsHIGHEST_PROTOCOL)r$   pds     r   _pandas_hashrl   j   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)rB   rC   rD   rE   shapern   _NP_SIZE_LARGEnumpyrandomRandomStatechoiceflat_NP_SAMPLE_SIZEtobytesrH   )r$   rI   npr   s       r   _numpy_hashry   y   s    EAHH^CI&&'''
x>!!	%%a((el38/:::HHS[[]]88::r   c                 "   t          j        d          }|                    t          |                                                      |                    t          |                                                      |                                S rT   )rB   rC   rD   rE   tellgetvaluerH   rX   s     r   _io_hashr}      sc    EAHH^CHHJJ''(((HH^CLLNN++,,,88::r   c                 *    |                                  S r   )encoder5   s    r   <lambda>r      s    szz|| r   c                 :    t          t          |                     S r   )r1   hashr5   s    r   r   r      s    }T#YY77 r   c                     | du rdndS )NT   1   0r    r5   s    r   r   r      s    sd{{tt r   c                     dS )Nr   r    r5   s    r   r   r      s    t r   c                     | S r   r    r5   s    r   r   r      s    S r   c                 D    t          |                                           S r   )strr   r5   s    r   r   r      s    SXX__%6%6 r   c                 :    t          t          |                     S r   )r1   r   r5   s    r   r   r      s    ]2c77%;%; r   c                 X    t          |           j         |                      d          S )Nzutf-8)r#   r   r   r5   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   rK   rF   r5   s    r   r   r          tCyy/I/I r   zbuiltins.dict_itemsc                 :    t          t          |                     S r   r   r5   s    r   r   r      r   r   zbuiltins.getset_descriptorc                 4    | j                                         S r   )r   r   r5   s    r   r   r      s    s/?/F/F/H/H r   znumpy.ufuncc                 4    | j                                         S r   r   r   r5   s    r   r   r      s    s|/B/B/D/D r   c                 4    | j                                         S r   r   r5   s    r   r   r      s    S\-@-@-B-B r   c                     | j         S r   )r   r5   s    r   r   r      s    S\ r   c                 8    t          | d          o
| j        dk    S )Nrw   r    )hasattrro   rP   s    r   r   r      s    ga##52 r   c                 *    |                                  S r   )rw   rP   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_funcsrG   r3   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   rB   rC   r   rD   rE   rH   r1   r   )r$   r   outputerI   reduce_datarJ   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   rE   rE      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   )r6   )r:   rJ   s     r   r<   z_key.<locals>.<genexpr>   s*      00Dz$000000r   __listr   r   )r6   r9   r3   listr>   r(   r   	isbuiltin	isroutineiscoder   _INDETERMINATEr5   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    )r:   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sortedrG   )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   rG   )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   )rF   r   rD   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_MAPrB   rC   r   rD   rE   	hexdigest)rV   r   rU   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           	      n   
                                  dvrt          d d          pi  	fdS d}t          j                     f	d
t	                     r"t          j                    
fd            n!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)rV   r   r   r   to_disk
cache_path)r   )fr   r   r   r   r   s    r   r   zcache.<locals>.<lambda>X  s(    !!
 
 
 r   c                    	  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 r|||fS t?          ||           |||fS )
Nr   _dinfoc              3   D   K   | ]}t          d          |          V  dS )r   N)getattr)r:   drU   s     r   r<   z+cache.<locals>.hash_func.<locals>.<genexpr>o  s1      YYagd1gq11YYYYYYr   dependenciesr    r   kw__main__Index) r   r3   objectr   r#   paramParameterizedr=   getrF   r   _TIME_FNsysmodulesr   __file__	func_hashr   r   curdocrB   sha256rE   r   _memoize_cache	diskcacher   ospathjoinr   r   )rU   r   	func_name	is_method	hash_argshash_kwargsdinfor   r   r&   fname
func_cacher   r   r   rV   r   r   per_sessionr   r   r   wrapped_funcs   `             r   r   zcache.<locals>.hash_funcd  sk    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$///##z4// :vy$???:t++r   c                     K    
| i |\  }}}||v r45  ||         \  }}}}|||dz   |f||<   d d d            n# 1 swxY w Y   n1 	| i | d {V }5  ||d|f||<   d d d            n# 1 swxY w Y   |S Nr   r   r    rU   r   r  r   r   retr   countr   rV   r   locks            r   r  zcache.<locals>.wrapped_func  sp     +49d+Ef+E+E(J
DZ'' F F(2:(>%CUA.12uQw-EJz*F F F F F F F F F F F F F F F !D$1&11111111 B B.14D-AJz*B B B B B B B B B B B B B B BJs!   <A A 
A..A25A2c                       
| i |\  }}}||v r45  ||         \  }}}}|||dz   |f||<   d d d            n# 1 swxY w Y   n+ 	| i |}5  ||d|f||<   d d d            n# 1 swxY w Y   |S r	  r    r
  s            r   r  zcache.<locals>.wrapped_func  sZ   +49d+Ef+E+E(J
DZ'' F F(2:(>%CUA.12uQw-EJz*F F F F F F F F F F F F F F F dD+F++ B B.14D-AJz*B B B B B B B B B B B B B B BJs   :>>
A&&A*-A*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RLockr   	functoolswrapsr   r   r   r  on_session_destroyed__dict__rD   AttributeError)rV   r   r   r   r   r   r   r  r   r   r   r  r  s   ````````  @@@r   r   r   1  s
   @ ||~~333PvPPP
 
 	
 !rJ|
 
 
 
 
 
 
 
 	
 I?D1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,f 4   			
	 
	 
	 
	 
	 
	 
	
	 
	 
		
	 
	 
	 
	 
	 
	 
	
	
 
 
 
 
 
 
 L 1u| 1(D 1))%000$$T]3333    s   D% %
D21D2)NNNr   NFr   F)P__doc__datetimedtr  rB   r   ior   pathlibrh   r   r  r   unittestunittest.mockweakref
contextlibr   r   param.parameterizedr   r   r   _FFI_TYPE_NAMESr   WeakKeyDictionary_HASH_STACKSr#   r   r   bytesr   floatintbool	bytearrayr4   rp   rv   ra   rc   platformperf_counterr   	monotonicr
   r(   r1   r6   r9   rK   rR   rY   rl   ry   r}   slicer   r=   rF   PathpartialmockMockStringIOBytesIOdater   ismoduler   r'   r   r   rE   r   r   r   r   r   r   r    r   r   <module>r6     s{              				 				   



            % % % % % %  3 3 3 3 3 3       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V V V V V Vr   