U
    Y»{j: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   úR/var/www/html/bwave/python/venv/lib/python3.8/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krdS |S d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                    sx   t t| ƒ |¡}d|ks$d|ks$|S ztj|| jd�}W n tk
rT   tdƒ‚Y n X tj|| j| j	| j
| jfd�S 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Ú
separatorsN)Úsuperr   ÚencodeÚ_jsonÚloadsr   Ú
ValueErrorÚdumpsr   r   Úitem_separatorÚkey_separator)r   ÚoZ	encoded_oZnew_o©Ú	__class__r   r   r   3   s    ÿ

üzPlotlyJSONEncoder.encodec              	   C   sd   | j | j| j| j| j| j| j| j| jf	}|D ](}z||ƒW   S  t	k
rR   Y q,X q,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   ÚobjZencoding_methodsZencoding_methodr   r   r   r2   X   s     !÷zPlotlyJSONEncoder.defaultc                 C   s(   z
|   ¡ W S  tk
r"   t‚Y nX dS )z1Attempt to use a builtin `to_plotly_json` method.N)Zto_plotly_jsonÚAttributeErrorr0   ©r3   r   r   r   r'   Š   s    
z"PlotlyJSONEncoder.encode_as_plotlyc                 C   s   t | dƒr|  ¡ S t‚dS )z@Attempt to use `tolist` method to convert to normal Python list.ÚtolistN)Úhasattrr6   r0   r5   r   r   r   r-   ’   s    
z PlotlyJSONEncoder.encode_as_listc                 C   s<   t dƒ}|st‚| |jkr"t| ƒS | |jkr4t| ƒS t‚dS )z@Attempt to convert sage.all.RR to floats and sage.all.ZZ to intszsage.allN)r   r0   ZRRÚfloatZZZÚint)r3   Zsage_allr   r   r   r(   š   s    

z PlotlyJSONEncoder.encode_as_sagec                 C   sB   t ddd�}|st‚| |jkr"dS t|dƒr:| |jkr:dS t‚dS )z)Attempt to convert pandas.NaT / pandas.NAÚpandasF©Zshould_loadNÚNA)r   r0   ZNaTr7   r<   )r3   r:   r   r   r   r*   ¨   s    
z"PlotlyJSONEncoder.encode_as_pandasc                 C   sp   t ddd�}|st‚| |jjjkr*tdƒS t| |jƒrh| jj	dkrhz| 
| ¡ ¡ W S  tk
rf   Y nX t‚dS )z'Attempt to convert numpy.ma.core.maskedÚnumpyFr;   ÚnanÚMN)r   r0   ÚmaÚcoreZmaskedr8   Ú
isinstanceZndarrayZdtypeÚkindZdatetime_as_stringr6   Ú	TypeError)r3   r=   r   r   r   r)   ¸   s    z!PlotlyJSONEncoder.encode_as_numpyc                 C   s(   z
|   ¡ W S  tk
r"   t‚Y nX dS )z.Convert datetime objects to iso-format stringsN)Ú	isoformatr4   r0   r5   r   r   r   r+   É   s    
z$PlotlyJSONEncoder.encode_as_datetimec                 C   s2   z|   ¡ }W n tk
r$   t‚Y n
X t|ƒS dS )z=Attempt to convert to utc-iso time string using date methods.N)rE   r4   r0   Úiso_to_plotly_time_string)r3   Ztime_stringr   r   r   r,   Ñ   s
    
z PlotlyJSONEncoder.encode_as_datec                 C   s   t | tjƒrt| ƒS t‚dS )z3Attempt to encode decimal by converting it to floatN)rB   ÚdecimalÚDecimalr8   r0   r5   r   r   r   r.   Û   s    z#PlotlyJSONEncoder.encode_as_decimalc                 C   s.   t dƒ}|dk	r&t| |jƒr&t | ¡S t‚dS )z5Attempt to convert PIL.Image.Image to base64 data uriz	PIL.ImageN)r   rB   ZImager   Zpil_image_to_urir0   )r3   Úimager   r   r   r/   ã   s    
zPlotlyJSONEncoder.encode_as_pil)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r2   Ústaticmethodr'   r-   r(   r*   r)   r+   r,   r.   r/   Ú__classcell__r   r   r%   r   r      s,   %2






	
r   c                   @   s   e Zd ZdS )r0   N)rJ   rK   rL   r   r   r   r   r0   í   s   r0   c                 C   sj   |   d¡dd… dks(|   d¡d dkr0tdƒ‚|  dd	¡ d
d	¡} |  d¡rZ|  dd	¡S |  dd¡S 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)Z
iso_stringr   r   r   rF   ñ   s    (ÿ
rF   c                     s   ‡ fdd„}|S )Nc                    s0   t jd d… dks,| jd k	r,| jjf ˆ Ž| _| S )Né   )r   rY   )ÚsysÚversion_inforM   Úformat)Úfunc©Únamesr   r   Ú
_decorator  s    
z template_doc.<locals>._decoratorr   )r_   r`   r   r^   r   Útemplate_doc  s    ra   Fc                 C   s   dd„ }t | ||d�S )Nc              	   S   sP   t  d| ¡}tt|ƒƒD ].}zt|| ƒ||< W q tk
rD   Y qX qt|ƒS )Nz(\d+))ÚrerU   Úranger
   r9   r    Útuple)ÚvZv_partsÚir   r   r   Úkey  s    z"_natural_sort_strings.<locals>.key)rg   Úreverse©Úsorted)Úvalsrh   rg   r   r   r   Ú_natural_sort_strings  s    
rl   c                  C   s&   t ddd�} | rt| jf}ntf}|S )Nr=   Fr;   )r   r9   Úinteger)ÚnpZint_typer   r   r   Ú_get_int_type  s
    ro   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  ó    z!split_multichar.<locals>.<lambda>c                    s
   |   ˆ ¡S rp   )rU   )r   ©Úcr   r   rr   3  rs   )r
   Úpopr   ÚmapÚsplit_multichar)ÚssÚcharsr   rt   r   rx   #  s
    rx   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   rr   J  rs   z(split_string_positions.<locals>.<lambda>r   Nr	   )Úlistrw   Úziprc   r
   r   )ry   r   r   r   Úsplit_string_positions7  s    ,þÿr   r{   ú^Tc                 C   s¤   dd„ t t| ƒd | | ƒD ƒ}d}|dkr\| D ]&}t |ƒD ]}	|| |	 }|||< q>q2n&t |ƒD ]}	| | | |	 }|||< qdd |¡}
|r |
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 )rT   r   )Ú.0Ú_r   r   r   Ú
<listcomp>m  s     z,display_string_positions.<locals>.<listcomp>r{   r   NrR   )rc   ÚmaxÚjoin)Úprf   ÚoffsetÚlengthÚcharZtrimÚsZmaxaddrZ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rN|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 rp   rt   )r   ru   r   r   r   Ú__init__œ  s    z.chomp_empty_strings.<locals>._Chomper.__init__c                 S   s6   t |ƒdkr(|d d… |d | j g S ||g S d S r   )r
   ru   )r   r   rq   r   r   r   Ú__call__Ÿ  s    z.chomp_empty_strings.<locals>._Chomper.__call__N)rJ   rK   rL   rŽ   r�   r   r   r   r   Ú_Chomper›  s   r�   rR   )Úchomp_empty_stringsr
   Úsumrw   r}   Úfilterr   )Ústringsru   rh   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 ]f\}}|d g}t|ƒD ]F\}}||d  d }|| d }	|| ||k }
| t||	|
ƒ¡ q`|}qF|d S )Nr   r{   r	   )r
   Úlevenshteinrc   Ú	enumerateÚappendÚmin)Ús1Ús2Zprevious_rowrf   Úc1Zcurrent_rowÚjÚc2Ú
insertionsZ	deletionsZsubstitutionsr   r   r   r•   ®  s    

r•   c                    s   ‡ fdd„}t ||d�d S )Nc                    s   t | ˆ ƒ| fS rp   )r•   )rŠ   ©Ústringr   r   Ú_keyÂ  s    z!find_closest_string.<locals>._key)rg   r   ri   )r    r”   r¡   r   rŸ   r   Úfind_closest_stringÁ  s    r¢   )F)Nr   r{   r€   T)F)rG   Újsonr   rZ   rb   Ú	functoolsr   Z_plotly_utils.optional_importsr   Z_plotly_utils.basevalidatorsr   r[   ZPY36_OR_LATERr   r1   r   rV   r0   rF   ra   rl   ro   rx   r   rŒ   r‘   r•   r¢   r   r   r   r   Ú<module>   s*   
 R

	
.
0