
    -e,                    ,   d Z ddlmZ ddlZ ej        e          ZddlZddl	Z	ddl
mZ ddlmZ ddlmZmZ ddlZddlmZ dd	lmZ d
dlmZ erddlmZ ddlZddlmZ  ed          d7d            Zd8dZ ej         ej!                   ej         ej"                   ej         ej#                   ej         ej$                   ej         ej%                   ej         ej&                   ej         ej'                   ej         ej(                   ej         ej)                  h	Z* ej+        dd          Z, ej-        d
d          Z.ej        /                    dej0        j1                  Z2 ee d3                    d e*D                                 Z dZ4d9dZ5d:dZ6d;d!Z7d<d#Z8d=d%Z9d>d)Z:d?d+Z;d?d,Z<d?d-Z=d@d/Z> ee>j         d03                    d1 e*D                                 e>_         dAd2Z?dBd5Z@d6aA e            ZBdS )Cz
Functions for helping with serialization and deserialization of
Bokeh objects.

Certain NumPy array dtypes can be serialized to a binary format for
performance and efficiency. The list of supported dtypes is:

{binary_array_types}

    )annotationsN)	lru_cache)Lock)TYPE_CHECKINGAny   )ID)settings   )format_docstring)	TypeGuardreturn	set[type]c                 >   dd l } t          j        t          j        t          j        h}|                    | j                   |                    | j                   |                    | j	                   |                    t          | j                             |S )Nr   )pandasdttimedatetimenp
datetime64add	Timestamp	TimedeltaPeriodtypeNaT)pdresults     8lib/python3.11/site-packages/bokeh/util/serialization.py_compute_datetime_typesr    6   s{    gr{BM2F
JJr|
JJr|
JJry
JJtBF||M    namestrr   c                8    | dk    rt                      S t          )NDATETIME_TYPES)r    AttributeError)r"   s    r   __getattr__r'   A   s     &(((
r!   ms)tz
c              #  "   K   | ]
}d | dV  dS z* ``np.z``N .0xs     r   	<genexpr>r1   Y   s+      @m@mUV1@m@m@m@m@m@mr!   )binary_array_types)array_encoding_disabledconvert_date_to_datetimeconvert_datetime_arrayconvert_datetime_typeconvert_timedelta_typeis_datetime_typeis_timedelta_type make_globally_unique_css_safe_idmake_globally_unique_idmake_idtransform_arraytransform_seriesobj0TypeGuard[dt.time | dt.datetime | np.datetime64]c                X    t          t                                }t          | |          S )z Whether an object is any date, time, or datetime type recognized by
    Bokeh.

    Args:
        obj (object) : the object to test

    Returns:
        bool : True if ``obj`` is a datetime type

    )tupler    
isinstance)r?   	_dt_tuples     r   r8   r8   n   s(     -//00Ic9%%%r!   (TypeGuard[dt.timedelta | np.timedelta64]c                N    t          | t          j        t          j        f          S )z Whether an object is any timedelta type recognized by Bokeh.

    Args:
        obj (object) : the object to test

    Returns:
        bool : True if ``obj`` is a timedelta type

    )rC   r   	timedeltar   timedelta64r?   s    r   r9   r9   }   s     cBL".9:::r!   dt.datefloatc                    t          j        |                                 dd         dt           j        j        it
          z
                                  dz  S )z Convert a date object to a datetime

    Args:
        obj (date) : the object to convert

    Returns:
        datetime

    N   tzinfo  )r   r   	timetupletimezoneutcDT_EPOCHtotal_secondsrI   s    r   r4   r4      sE     K!,ER[_EEP__aadhhhr!   dt.timedelta | np.timedelta64c                    t          | t          j                  r|                                 dz  S t          | t          j                  rt          | t          z            S t          d|           )z Convert any recognized timedelta value to floating point absolute
    milliseconds.

    Args:
        obj (object) : the object to convert

    Returns:
        float : milliseconds

         @@zUnknown timedelta object: )	rC   r   rG   rT   r   rH   rK   NP_MS_DELTA
ValueErrorrI   s    r   r7   r7      sm     #r|$$ (  ""U**	C	(	( (S;&'''
9#99
:
::r!   SAny | pd.Timestamp | pd.Timedelta | dt.datetime | dt.date | dt.time | np.datetime64c                D   ddl }| |j        u rt          j        S t	          | |j                  r|                                 j        dz  S t	          | |j                  r
| j        dz  S t	          | |j	                  r
| j        dz  S t	          | t          j                  rD|                     t          j        j                  t          z
  }|                                dz  S t	          | t          j                  rt%          |           S t	          | t          j                  r!| t(          z
  }t+          |t,          z            S t	          | t          j                  r+| j        dz  | j        dz  z   | j        z   dz  | j        dz  z   S t9          d	|           )
z Convert any recognized date, time, or datetime value to floating point
    milliseconds since epoch.

    Args:
        obj (object) : the object to convert

    Returns:
        float : milliseconds

    r   Ng    .A)rN   rO   i  <   rW   zunknown datetime object: )r   r   r   nanrC   r   to_timestampvaluer   r   r   r   replacerQ   rR   rS   rT   dater4   r   NP_EPOCHrK   rX   r   hourminutesecondmicrosecondrY   )r?   r   diffepoch_deltas       r   r6   r6      s     bf}}v #ry!! 2!!''11 #r|$$ ay7"" 
C	&	& ay7"" 
C	%	% a{{"+/{22X=!!##d** 
C	!	! 
a',,, 
C	'	' aHn[;./// 
C	!	! a4#*r/1CJ>$F[`I```
888
9
99r!   arraynpt.NDArray[Any]npt.NDArray[np.floating[Any]]c                   | j         j        dk    r+|                     d                              d          dz  S | j         j        dk    r+|                     d                              d          dz  S | j         j        dk    rot          |           dk    r\t	          | d         t
          j                  r<	 |                     d                              d          dz  S # t          $ r Y nw xY w| S )	a   Convert NumPy datetime arrays to arrays to milliseconds since epoch.

    Args:
        array : (obj)
            A NumPy array of datetime to convert

            If the value passed in is not a NumPy array, it will be returned as-is.

    Returns:
        array

    Mzdatetime64[us]int64rW   mztimedelta64[us]Or   )dtypekindastypelenrC   r   ra   	Exceptionri   s    r   r5   r5      s     {3||,--44W==FF		S	 	 ||-..55g>>GG 
	S	 	 SZZ!^^
58RW8U8U^	<< 01188AAFJJ 	 	 	D	 Ls   ;*C& &
C32C3r	   c                     t          j                    rBt          5  t          dz  at	          dt                     cddd           S # 1 swxY w Y   dS t                      S )a   Return a new unique ID for a Bokeh object.

    Normally this function will return simple monotonically increasing integer
    IDs (as strings) for identifying Bokeh objects within a Document. However,
    if it is desirable to have globally unique for every object, this behavior
    can be overridden by setting the environment variable ``BOKEH_SIMPLE_IDS=no``.

    Returns:
        str

    r   pN)r
   
simple_ids_simple_id_lock
_simple_idr	   r;   r-   r!   r   r<   r<      s      ) 	( 	(!OJ&*&&''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( '(((s   !A		AAc                 \    t          t          t          j                                        S )z Return a globally unique UUID.

    Some situations, e.g. id'ing dynamically created Divs in HTML documents,
    always require globally unique IDs.

    Returns:
        str

    )r	   r#   uuiduuid4r-   r!   r   r;   r;     s      c$*,,   r!   c                     d} t          d|           D ].}t                      }|d                                         r|c S /t          dt                                 S )a   Return a globally unique CSS-safe UUID.

    Some situations, e.g. id'ing dynamically created Divs in HTML documents,
    always require globally unique IDs. ID generated with this function can
    be used in APIs like ``document.querySelector("#id")``.

    Returns:
        str

    d   r   zbk-)ranger;   isalphar	   )max_iter_iids      r   r:   r:     so     HAx    $&&a5==?? 	III	 /+--//000r!   boolc                    | j         t          vS )z Determine whether an array may be binary encoded.

    The NumPy array dtypes that can be encoded are:

    {binary_array_types}

    Args:
        array (np.ndarray) : the array to check

    Returns:
        bool

    )rq   BINARY_ARRAY_TYPESrv   s    r   r3   r3   -  s      ;000r!   z
    c              #  "   K   | ]
}d | dV  dS r,   r-   r.   s     r   r1   r1   A  s*      $Q$Q_q___$Q$Q$Q$Q$Q$Qr!   c                   t          |           } dd}| j        t          j        t          j                  k    r || t          j                  } n=| j        t          j        t          j                  k    r || t          j                  } t          | t          j        j	                  r| 
                    t          j                  } | j        d         st          j        |           } | S )	z Transform a ndarray into a serializable ndarray.

    Converts un-serializable dtypes and returns JSON serializable
    format

    Args:
        array (np.ndarray) : a NumPy array to be transformed

    Returns:
        ndarray

    ri   rj   rq   	type[Any]r   c                    t          j        |          }t          j        | |j        k     |j        | k     z            r| S |                     |d          S )Nunsafe)casting)r   iinfoanyminmaxrs   )ri   rq   infos      r   _cast_if_canz%transform_array.<locals>._cast_if_canV  sO    x6548#5(89:: 	9L<<x<888r!   C_CONTIGUOUS)ri   rj   rq   r   r   rj   )r5   rq   r   rn   int32uint64uint32rC   maMaskedArrayfilledr]   flagsascontiguousarray)ri   r   s     r   r=   r=   D  s     #5))E
9 9 9 9 {bhrx((((UBH--	++	+	+UBI..%*++ %RV$$;~& ,$U++Lr!   series<pd.Series[Any] | pd.Index | pd.api.extensions.ExtensionArrayc                    ddl }t          | |j                  r|                                 j        }n|                                 }|S )z Transforms a Pandas series into serialized form

    Args:
        series (pd.Series) : the Pandas series to transform

    Returns:
        ndarray

    r   N)r   rC   PeriodIndexr^   valuesto_numpy)r   r   valss      r   r>   r>   j  sP      &".)) !""$$+  Kr!   i  )r   r   )r"   r#   r   r   )r?   r   r   r@   )r?   r   r   rE   )r?   rJ   r   rK   )r?   rU   r   rK   )r?   rZ   r   rK   )ri   rj   r   rk   )r   r	   )ri   rj   r   r   )ri   rj   r   rj   )r   r   r   rj   )C__doc__
__future__r   logging	getLogger__name__logr   r   r}   	functoolsr   	threadingr   typingr   r   numpyr   
core.typesr	   r
   stringsr   numpy.typingnptr   r   typing_extensionsr   r    r'   rq   bool_uint8int8uint16int16r   r   float32float64r   r   rb   rH   rX   fromtimestamprQ   rR   rS   join__all__r8   r9   r4   r7   r6   r5   r<   r;   r:   r3   r=   r>   r{   rz   r-   r!   r   <module>r      s  	 	 # " " " " " g!!                  % % % % % % % %                 % % % % % % ,++++++ 4       BHRXBHRXBHRWBHRYBHRXBHRYBHRX BHRZBHRZ  2=D!!bnQ%%;$$Q2;?$;;

7tyy@m@mZl@m@m@m7m7m
n
n
n&& & & &
; 
; 
; 
;
i 
i 
i 
i; ; ; ;&0: 0: 0: 0:f   :) ) ) )*
! 
! 
! 
!1 1 1 1(1 1 1 1$ #3"2#}}$Q$Q>P$Q$Q$QQQ# # #  
$ $ $ $L   8 
$&&r!   