U
    /eK                  
   @  s  U d Z ddlmZ ddlZeeZddlZddlZ	ddl
Z
ddlZddlmZm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 ddlZddlmZmZm Z  erddl!Z"ndd	l#m$Z$ e$d
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,m-Z- e.ej/e.ej0e.ej1e.ej2e.ej3e.ej4e.ej5e.ej6hZ7e	j8e	jej9hZ:de;d< e"rz
e"j<Z=W n e>k
r   e"j?j<Z=Y nX e:@e= e:@e"jA e:@e"jB e:@eCe"jD e9ddZEeFddZGe	jHdZIe)e dJdd e7D dZ dZKed ZLG dd deZMG dd deZNerTee- ZOddd d!d"ZPdd#d d$d%ZQd&d'd d(d)ZRd*d'd d+d,ZSd-d'd d.d/ZTed0eee d1f d2ZUd0d0d3d4d5ZVd6d7d8d9ZWd6d7d:d;ZXd1d<d3d=d>ZYe)eYj d?Jd@d e7D deY_ dad1d<dBdCdDdEZZd1dFd3dGdHZ[dbdId<dBdJdKdLZ\dcd1d<dBdCdMdNZ]dddFdBdOdPdQZ^dedRdBdSdRdTdUdVZ_d1dWddXdYdZZ`d1dd3d[d\Zadd1d]d^d_Zbd`ace Zdeee:ZfdS )fz
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)isinfisnan)Lock)	TYPE_CHECKINGAnyListSequenceSetTupleTypeVarUnioncast)Literal	TypedDict	TypeGuard   )import_optionalpandas   )ID)settings)format_docstring)DataDict)	BufferRefz	Set[type]DATETIME_TYPESms
c                 c  s   | ]}d | dV  qdS z* ``np.z``N .0xr   r   </tmp/pip-unpacked-wheel-f5fndrjf/bokeh/util/serialization.py	<genexpr>h   s     r$   )Zbinary_array_types)array_encoding_disabledconvert_date_to_datetimeconvert_datetime_arrayconvert_datetime_typeconvert_timedelta_typedecode_base64_dictencode_binary_dictencode_base64_dictis_datetime_typeis_timedelta_typemake_globally_unique_idmake_idserialize_arraytransform_arraytransform_array_to_listtransform_column_source_datatraverse_datatransform_series)littlebigc                   @  s.   e Zd ZU ded< ded< ded< ded< d	S )

BufferJsonr   
__buffer__Tuple[int, ...]shapestrdtype	ByteOrderorderN__name__
__module____qualname____annotations__r   r   r   r#   r9      s   
r9   c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )	Base64BufferJsonr=   __ndarray__r;   r<   r>   r?   r@   NrA   r   r   r   r#   rF      s   
rF   r   z0TypeGuard[dt.time | dt.datetime | np.datetime64])objreturnc                 C  s
   t | tS )z Whether an object is any date, time, or datetime type recognized by
    Bokeh.

    Arg:
        obj (object) : the object to test

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

    )
isinstance	_dt_tuplerH   r   r   r#   r-      s    r-   z(TypeGuard[dt.timedelta | np.timedelta64]c                 C  s   t | tjtjfS )z Whether an object is any timedelta type recognized by Bokeh.

    Arg:
        obj (object) : the object to test

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

    )rJ   dt	timedeltanptimedelta64rL   r   r   r#   r.      s    
r.   zdt.datefloatc                 C  s(   t j|  dd ddit  d S )z Convert a date object to a datetime

    Args:
        obj (date) : the object to convert

    Returns:
        datetime

    N   tzinfo  )rM   datetime	timetupleDT_EPOCHtotal_secondsrL   r   r   r#   r&      s    
r&   zdt.timedelta | np.timedelta64c                 C  s4   t | tjr|  d S t | tjr0t| t S dS )z Convert any recognized timedelta value to floating point absolute
    milliseconds.

    Arg:
        obj (object) : the object to convert

    Returns:
        float : milliseconds

         @@N)rJ   rM   rN   rX   rO   rP   rQ   NP_MS_DELTArL   r   r   r#   r)      s    r)   zbpd.NaT | pd.Period | pd.Timestamp | pd.Timedelta | dt.datetime | dt.date | dt.time | np.datetime64c                 C  s   t r| t jkrtjS t r2t| t jr2|  jd S t rJt| trJ| jd S t rdt| t j	rd| jd S t| t
jr| jddt }| d S t| t
jrt| S t| tjr| t }t|t S t| t
jr| jd | jd  | j d | jd  S dS )z Convert any recognized date, time, or datetime value to floating point
    milliseconds since epoch.

    Arg:
        obj (object) : the object to convert

    Returns:
        float : milliseconds

    g    .AN)rS   rT   i  <   rY   )pdNaTrO   nanrJ   Periodto_timestampvalue_pd_timestamp	TimedeltarM   rU   replacerW   rX   dater&   
datetime64NP_EPOCHrQ   rZ   timehourminutesecondmicrosecond)rH   ZdiffZepoch_deltar   r   r#   r(      s$    

r(   ARz
np.ndarray)bound)arrayrI   c                 C  s   t | tjs| S | jjdkr0| ddd S | jjdkrP| ddd S | jjdkrt| dkrt | d tjrz| ddd W S  t	k
r   Y nX | 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]Zint64rY   mztimedelta64[us]Or   )
rJ   rO   ndarrayr>   kindastypelenrM   re   	Exceptionro   r   r   r#   r'      s    (r'   r   )rI   c                
   C  s@   t  r6t" td7 atttW  5 Q R  S Q R X nt S d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   N)r   Z
simple_ids_simple_id_lock
_simple_idr   r=   r/   r   r   r   r#   r0     s
     r0   c                   C  s   t tt 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=   uuidZuuid4r   r   r   r#   r/   /  s    
r/   boolc                 C  s
   | j tk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

    )r>   BINARY_ARRAY_TYPESrx   r   r   r#   r%   ;  s    r%   z
    c                 c  s   | ]}d | dV  qdS r   r   r    r   r   r#   r$   O  s     FzBuffers | Nonero   
force_listbuffersc                 C  s   t | } t| ||dS )a   Transform a NumPy arrays into serialized format

    Converts un-serializable dtypes and returns JSON serializable
    format

    Args:
        array (np.ndarray) : a NumPy array to be transformed
        force_list (bool, optional) : whether to only output to standard lists
            This function can encode some dtypes using a binary encoding, but
            setting this argument to True will override that and cause only
            standard Python lists to be emitted. (default: False)

        buffers (set, optional) :
            If binary buffers are desired, the buffers parameter may be
            provided, and any columns that may be sent as binary buffers
            will be added to the set. If None, then only base64 encoding
            will be used (default: None)

            If force_list is True, then this value will be ignored, and
            no buffers will be generated.

            **This is an "out" parameter**. The values it contains will be
            modified in-place.


    Returns:
        JSON

    r   r   )r'   r1   r~   r   r   r#   r2   R  s    r2   zSequence[Any]c                 C  s   | j jdkrXt|   rX| d}d|t| < d|t| < d|t| < |	 S | j jdkrt
rt
|  r| d}d|t
| < |	 S | 	 S )z Transforms a NumPy array into a list of values

    Args:
        array (np.nadarray) : the NumPy array series to transform

    Returns:
        list or dict

    )uifobjectNaNInfinity	-Infinityrr   )r>   rt   rO   isfiniteanyru   r   ZisposinfZisneginftolistr\   Zisnull)ro   Ztransformedr   r   r#   r3   u  s    


r3   zpd.Series | pd.Index)seriesr   r   c                 C  s,   t | tjr|  j}n| j}t|||dS )a   Transforms a Pandas series into serialized form

    Args:
        series (pd.Series) : the Pandas series to transform
        force_list (bool, optional) : whether to only output to standard lists
            This function can encode some dtypes using a binary encoding, but
            setting this argument to True will override that and cause only
            standard Python lists to be emitted. (default: False)

        buffers (set, optional) :
            If binary buffers are desired, the buffers parameter may be
            provided, and any columns that may be sent as binary buffers
            will be added to the set. If None, then only base64 encoding
            will be used (default: None)

            If force_list is True, then this value will be ignored, and
            no buffers will be generated.

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

    Returns:
        list or dict

    r   )rJ   r\   ZPeriodIndexr`   valuesr2   )r   r   r   valsr   r   r#   r6     s    r6   c                 C  s`   t | tjjr| tj} t| s&|r.t| S | jd sBt	| } |dkrRt
| S t| |S dS )a   Transforms a NumPy array into serialized form.

    Args:
        array (np.ndarray) : the NumPy array to transform
        force_list (bool, optional) : whether to only output to standard lists
            This function can encode some dtypes using a binary encoding, but
            setting this argument to True will override that and cause only
            standard Python lists to be emitted. (default: False)

        buffers (set, optional) :
            If binary buffers are desired, the buffers parameter may be
            provided, and any columns that may be sent as binary buffers
            will be added to the set. If None, then only base64 encoding
            will be used (default: None)

            If force_list is True, then this value will be ignored, and
            no buffers will be generated.

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

    Returns:
        list or dict

    ZC_CONTIGUOUSN)rJ   rO   maZMaskedArrayZfilledr^   r%   r3   flagsZascontiguousarrayr,   r+   r~   r   r   r#   r1     s    

r1   )rH   r   c                   s   t dd | D r$ fdd| D S g }| D ]l}t|tkrpt|rJd}nt|rd|dkr`d}nd}|| q,t|ttfr|t	| q,|| q,|S )	z Recursively traverse an object until a flat list is found.

    The flat list is converted to a numpy array and passed to transform_array()
    to handle ``nan``, ``inf``, and ``-inf``.

    Args:
        obj (list) : a list of values or lists

    c                 s  s   | ]}t |tjV  qd S )N)rJ   rO   rs   r!   elr   r   r#   r$     s     z traverse_data.<locals>.<genexpr>c                   s   g | ]}t | d qS )r   )r2   r   r   r   r#   
<listcomp>  s     z!traverse_data.<locals>.<listcomp>r   r   r   r   )
alltyperQ   r   r   appendrJ   listtupler5   )rH   r   Zobj_copyitemr   r   r#   r5     s     
r5   r   zList[str] | None)datar   colsrI   c                 C  s   |dkrt | nt |}i }|D ]n}trTt| | tjtjfrTt| | |d||< q t| | tjrzt| | |d||< q t	| | |d||< q |S )a   Transform ``ColumnSourceData`` data to a serialized format

    Args:
        data (dict) : the mapping of names to data columns to transform

        buffers (set, optional) :
            If binary buffers are desired, the buffers parameter may be
            provided, and any columns that may be sent as binary buffers
            will be added to the set. If None, then only base64 encoding
            will be used (default: None)

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

        cols (list[str], optional) :
            Optional list of subset of columns to transform. If None, all
            columns will be transformed (default: None)

    Returns:
        JSON compatible dict

    Nr   )
setr\   rJ   ZSeriesZIndexr6   rO   rs   r2   r5   )r   r   r   Zto_transformZ	data_copykeyr   r   r#   r4     s    r4   Buffers)ro   r   rI   c                 C  sD   t  }t|d|  f}|| t|| jt| jjt	t
tjdS )a|   Send a numpy array as an unencoded binary buffer

    The encoded format is a dict with the following structure:

    .. code:: python

        {
            '__buffer__' :  << an ID to locate the buffer >>,
            'shape'      : << array shape >>,
            'dtype'      : << dtype name >>,
            'order'      : << byte order at origin (little or big)>>
        }

    Args:
        array (np.ndarray) : an array to encode

        buffers (set) :
            Set to add buffers to

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

    Returns:
        dict

    )id)r:   r<   r>   r@   )r0   dicttobytesr   r9   r<   r=   r>   namer   r?   sys	byteorder)ro   r   Z	buffer_idbufr   r   r#   r+     s    


r+   c                 C  s0   t t| jd| jt| jjt	t
tjdS )a   Encode a NumPy array using base64:

    The encoded format is a dict with the following structure:

    .. code:: python

        {
            '__ndarray__' : << base64 encoded array data >>,
            'shape'       : << array shape >>,
            'dtype'       : << dtype name >>,
        }

    Args:

        array (np.ndarray) : an array to encode

    Returns:
        dict

    zutf-8)rG   r<   r>   r@   )rF   base64	b64encoder   decoder<   r=   r>   r   r   r?   r   r   rx   r   r   r#   r,   ;  s    

r,   )r   rI   c                 C  sH   t | d }ttj|| d d}t| d dkrD|| d }|S )z Decode a base64 encoded array into a NumPy array.

    Args:
        data (dict) : encoded array data to decode

    Data should have the format encoded by :func:`encode_base64_dict`.

    Returns:
        np.ndarray

    rG   r>   )r>   r<   r   )r   	b64decoderO   copyZ
frombufferrv   Zreshape)r   Zb64ro   r   r   r#   r*   W  s
    r*   i  )FN)FN)FN)N)NN)g__doc__
__future__r   logging	getLoggerrB   logr   rU   rM   r   r{   mathr   r   	threadingr   typingr   r   r   r	   r
   r   r   r   r   ZnumpyrO   Ztyping_extensionsr   r   r   r   r\   Zdependenciesr   Z
core.typesr   r   stringr   Zmodels.sourcesr   Zprotocol.messager   r>   Zfloat32Zfloat64Zuint8Zint8Zuint16Zint16Zuint32Zint32r}   rh   rf   r   rE   Z	Timestamprb   AttributeErrorZtslibaddrc   r_   r   r]   rg   rP   rZ   utcfromtimestamprW   join__all__r?   r9   rF   r   r-   r.   r&   r)   r(   rm   r'   r0   r/   r%   r2   r3   r6   r1   r5   r4   r+   r,   r*   rz   ry   r   rK   r   r   r   r#   <module>   s   
,










.!#"%$&