o
    d:8                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 ejdkZdd ZG dd	 d	ejZG d
d deZdd Zdd Zd$ddZdd Zdd Zdd Zd%ddZd$ddZd d! Zd"d# ZdS )&    Nreduce)
get_module)ImageUriValidator)      c                 C   s   dd }t || g }|S )z0
    Custom cumsum to avoid a numpy import.
    c                 S   s$   t | dkr	|gS | | d | g S Nr   )len)ax r   X/var/www/html/visualizacion-main/env/lib/python3.10/site-packages/_plotly_utils/utils.py_reducer   s   zcumsum.<locals>._reducerr   )r   r   retr   r   r   cumsum   s   r   c                       s   e Zd ZdZdd Z fddZdd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Z  ZS )PlotlyJSONEncodera9  
    Meant to be passed as the `cls` kwarg to json.dumps(obj, cls=..)

    See PlotlyJSONEncoder.default for more implementation information.

    Additionally, this encoder overrides nan functionality so that 'Inf',
    'NaN' and '-Inf' encode to 'null'. Which is stricter JSON than the Python
    version.

    c                 C   s   |dv rdS |S )zU
        This is used to ultimately *encode* into strict JSON, see `encode`

        )Infinityz	-InfinityNaNNr   )selfconstr   r   r   coerce_to_strict(   s   z"PlotlyJSONEncoder.coerce_to_strictc                    sn   t t| |}d|v sd|v s|S z
tj|| jd}W n ty'   tdw tj|| j| j	| j
| jfdS )z
        Load and then dump the result using parse_constant kwarg

        Note that setting invalid separators will cause a failure at this step.

        r   r   )parse_constantzSEncoding into strict JSON failed. Did you set the separators valid JSON separators?)	sort_keysindent
separators)superr   encode_jsonloadsr   
ValueErrordumpsr   r   item_separatorkey_separator)r   o	encoded_onew_o	__class__r   r   r   3   s    
zPlotlyJSONEncoder.encodec              	   C   s`   | j | j| j| j| j| j| j| j| jf	}|D ]}z||W   S  t	y(   Y qw t
j| |S )a  
        Accept an object (of unknown type) and try to encode with priority:
        1. builtin:     user-defined objects
        2. sage:        sage math cloud
        3. pandas:      dataframes/series
        4. numpy:       ndarrays
        5. datetime:    time/datetime objects

        Each method throws a NotEncoded exception if it fails.

        The default method will only get hit if the object is not a type that
        is naturally encoded by json:

            Normal objects:
                dict                object
                list, tuple         array
                str, unicode        string
                int, long, float    number
                True                true
                False               false
                None                null

            Extended objects:
                float('nan')        'NaN'
                float('infinity')   'Infinity'
                float('-infinity')  '-Infinity'

        Therefore, we only anticipate either unknown iterables or values here.

        )encode_as_plotlyencode_as_sageencode_as_numpyencode_as_pandasencode_as_datetimeencode_as_dateencode_as_listencode_as_decimalencode_as_pilNotEncodabler   JSONEncoderdefault)r   objencoding_methodsencoding_methodr   r   r   r4   X   s"   !zPlotlyJSONEncoder.defaultc                 C      z|   W S  ty   tw )z1Attempt to use a builtin `to_plotly_json` method.)to_plotly_jsonAttributeErrorr2   r5   r   r   r   r)      
   
z"PlotlyJSONEncoder.encode_as_plotlyc                 C   s   t | dr	|  S t)z@Attempt to use `tolist` method to convert to normal Python list.tolist)hasattrr=   r2   r;   r   r   r   r/      s   
z PlotlyJSONEncoder.encode_as_listc                 C   s8   t d}|st| |jv rt| S | |jv rt| S t)z@Attempt to convert sage.all.RR to floats and sage.all.ZZ to intszsage.all)r   r2   RRfloatZZint)r5   sage_allr   r   r   r*      s   

z PlotlyJSONEncoder.encode_as_sagec                 C   s>   t ddd}|s
t| |ju rdS t|dr| |ju rdS t)z)Attempt to convert pandas.NaT / pandas.NApandasFshould_loadNNA)r   r2   NaTr>   rG   )r5   rD   r   r   r   r,      s   
z"PlotlyJSONEncoder.encode_as_pandasc                 C   sl   t ddd}|s
t| |jjju rtdS t| |jr4| jj	dkr4z|
|  W S  ty3   Y tw t)z'Attempt to convert numpy.ma.core.maskednumpyFrE   nanM)r   r2   macoremaskedr@   
isinstancendarraydtypekinddatetime_as_stringr=   	TypeError)r5   rI   r   r   r   r+      s   z!PlotlyJSONEncoder.encode_as_numpyc                 C   r8   )z.Convert datetime objects to iso-format strings)	isoformatr:   r2   r;   r   r   r   r-      r<   z$PlotlyJSONEncoder.encode_as_datetimec                 C   s&   z	|   }W t|S  ty   tw )z=Attempt to convert to utc-iso time string using date methods.)rU   r:   r2   iso_to_plotly_time_string)r5   time_stringr   r   r   r.      s   
z PlotlyJSONEncoder.encode_as_datec                 C   s   t | tjr
t| S t)z3Attempt to encode decimal by converting it to float)rO   decimalDecimalr@   r2   r;   r   r   r   r0      s   z#PlotlyJSONEncoder.encode_as_decimalc                 C   s*   t d}|durt| |jrt| S t)z5Attempt to convert PIL.Image.Image to base64 data uriz	PIL.ImageN)r   rO   Imager   pil_image_to_urir2   )r5   imager   r   r   r1      s   
zPlotlyJSONEncoder.encode_as_pil)__name__
__module____qualname____doc__r   r   r4   staticmethodr)   r/   r*   r,   r+   r-   r.   r0   r1   __classcell__r   r   r'   r   r      s.    %2






	
r   c                   @   s   e Zd ZdS )r2   N)r]   r^   r_   r   r   r   r   r2      s    r2   c                 C   sf   |  ddd dks|  dd dkrtd| dd	d
d	} | dr-| dd	S | ddS )z=Remove timezone info and replace 'T' delimeter with ' ' (ws).-Nr   z00:00+r   z]Plotly won't accept timestrings with timezone info.
All timestrings are assumed to be in UTC.z-00:00 z+00:00z	T00:00:00T )split	Exceptionreplaceendswith)
iso_stringr   r   r   rV      s   (
rV   c                     s    fdd}|S )Nc                    s4   t jd d dks| jd ur| jjdi  | _| S )N   )r   rm   r   )sysversion_infor`   format)funcnamesr   r   
_decorator  s   
z template_doc.<locals>._decoratorr   )rs   rt   r   rr   r   template_doc  s   ru   Fc                 C   s   dd }t | ||dS )Nc              	   S   sL   t d| }tt|D ]}z
t|| ||< W q ty!   Y qw t|S )Nz(\d+))rerh   ranger
   rB   r    tuple)vv_partsir   r   r   key  s   z"_natural_sort_strings.<locals>.key)r|   reversesorted)valsr}   r|   r   r   r   _natural_sort_strings  s   
r   c                  C   s(   t ddd} | rt| jf}|S tf}|S )NrI   FrE   )r   rB   integer)npint_typer   r   r   _get_int_type  s   
r   c                    s>   t |dkr| S |  tdd t fdd| } t| |S )ar  
    Split all the strings in ss at any of the characters in chars.
    Example:

        >>> ss = ["a.string[0].with_separators"]
        >>> chars = list(".[]_")
        >>> split_multichar(ss, chars)
        ['a', 'string', '0', '', 'with', 'separators']

    :param (list) ss: A list of strings.
    :param (list) chars: Is a list of chars (note: not a string).
    r   c                 S   s   | | S Nr   )r   yr   r   r   <lambda>3  s    z!split_multichar.<locals>.<lambda>c                    s
   |   S r   )rh   )r   cr   r   r   3  s   
 )r
   popr   mapsplit_multichar)sscharsr   r   r   r   #  s
   
r   c                 C   s<   t tdd ttt| tdgt tt| dd  S )a  
    Given a list of strings split using split_multichar, return a list of
    integers representing the indices of the first character of every string in
    the original string.
    Example:

        >>> ss = ["a.string[0].with_separators"]
        >>> chars = list(".[]_")
        >>> ss_split = split_multichar(ss, chars)
        >>> ss_split
        ['a', 'string', '0', '', 'with', 'separators']
        >>> split_string_positions(ss_split)
        [0, 2, 9, 11, 12, 17]

    :param (list) ss: A list of strings.
    c                 S   s   | d | d  S )Nr      r   )tr   r   r   r   J      z(split_string_positions.<locals>.<lambda>r   Nr	   )listr   ziprw   r
   r   )r   r   r   r   split_string_positions7  s   ,r   r   ^Tc                 C   s   dd t t| d | | D }d}|du r.| D ]}t |D ]}	|| |	 }|||< qqnt |D ]}	| | | |	 }|||< q2d|}
|rP|
d|d  }
|
S )a  
    Return a string that is whitespace except at p[i] which is replaced with char.
    If i is None then all the indices of the string in p are replaced with char.

    Example:

        >>> ss = ["a.string[0].with_separators"]
        >>> chars = list(".[]_")
        >>> ss_split = split_multichar(ss, chars)
        >>> ss_split
        ['a', 'string', '0', '', 'with', 'separators']
        >>> ss_pos = split_string_positions(ss_split)
        >>> ss[0]
        'a.string[0].with_separators'
        >>> display_string_positions(ss_pos,4)
        '            ^'
        >>> display_string_positions(ss_pos,4,offset=1,length=3,char="~",trim=False)
        '             ~~~      '
        >>> display_string_positions(ss_pos)
        '^ ^      ^ ^^    ^'
    :param (list) p: A list of integers.
    :param (integer|None) i: Optional index of p to display.
    :param (integer) offset: Allows adding a number of spaces to the replacement.
    :param (integer) length: Allows adding a replacement that is the char
                             repeated length times.
    :param (str) char: allows customizing the replacement character.
    :param (boolean) trim: trims the remaining whitespace if True.
    c                 S   s   g | ]}d qS )rg   r   ).0_r   r   r   
<listcomp>m  r   z,display_string_positions.<locals>.<listcomp>r   r   Nre   )rw   maxjoin)pr{   offsetlengthchartrimsmaxaddrp_lr   r   r   r   display_string_positionsP  s    "


r   c                 C   sx   dd }|r|t || |S t| s| S ttt| dkr'|t| d  gS G dd d}tttt||| dgS )a  
    Given a list of strings, some of which are the empty string "", replace the
    empty strings with c and combine them with the closest non-empty string on
    the left or "" if it is the first string.
    Examples:
    for c="_"
    ['hey', '', 'why', '', '', 'whoa', '', ''] -> ['hey_', 'why__', 'whoa__']
    ['', 'hi', '', "I'm", 'bob', '', ''] -> ['_', 'hi_', "I'm", 'bob__']
    ['hi', "i'm", 'a', 'good', 'string'] -> ['hi', "i'm", 'a', 'good', 'string']
    Some special cases are:
    [] -> []
    [''] -> ['']
    ['', ''] -> ['_']
    ['', '', '', ''] -> ['___']
    If reverse is true, empty strings are combined with closest non-empty string
    on the right or "" if it is the last string.
    c                 S   s   dd | D d d d S )Nc                 S   s   g | ]	}|d d d qS )Nr	   r   )r   r   r   r   r   r     s    z5chomp_empty_strings.<locals>._rev.<locals>.<listcomp>r	   r   )r   r   r   r   _rev  s   z!chomp_empty_strings.<locals>._revr   r   c                   @   s   e Zd Zdd Zdd ZdS )z%chomp_empty_strings.<locals>._Chomperc                 S   s
   || _ d S r   r   )r   r   r   r   r   __init__  s   
z.chomp_empty_strings.<locals>._Chomper.__init__c                 S   s2   t |dkr|d d |d | j g S ||g S r   )r
   r   )r   r   r   r   r   r   __call__  s   
z.chomp_empty_strings.<locals>._Chomper.__call__N)r]   r^   r_   r   r   r   r   r   r   _Chomper  s    r   re   )chomp_empty_stringsr
   sumr   r   filterr   )stringsr   r}   r   r   r   r   r   r   ~  s   r   c                 C   s   t | t |k rt|| S t |dkrt | S tt |d }t| D ]3\}}|d g}t|D ]#\}}||d  d }|| d }	|| ||k }
|t||	|
 q0|}q#|d S )Nr   r   r	   )r
   levenshteinrw   	enumerateappendmin)s1s2previous_rowr{   c1current_rowjc2
insertions	deletionssubstitutionsr   r   r   r     s   

r   c                    s    fdd}t ||dd S )Nc                    s   t |  | fS r   )r   )r   stringr   r   _key  s   z!find_closest_string.<locals>._key)r|   r   r~   )r   r   r   r   r   r   find_closest_string  s   r   )F)Nr   r   r   T)rX   jsonr   rn   rv   	functoolsr   _plotly_utils.optional_importsr   _plotly_utils.basevalidatorsr   ro   PY36_OR_LATERr   r3   r   ri   r2   rV   ru   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s,    
 R

	

.0