
    -Ph[B              	       0   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m	Z	 d dl
mZmZmZ dZdZdZdZd	Zd
ZdZdZdZdddddddddZd Zd Zd Zd Z G d dej                  Z G d de          Zd Z d  Z!d-d"Z"d# Z#d$ Z$d% Z%d.d)Z&d-d*Z'd+ Z(d, Z)dS )/    Nreduce)
get_module)ImageUriValidatorcopy_to_readonly_numpy_arrayis_homogeneous_arrayi   i i  i   i   i  l    i1u1i2u2i4u4f4f8)int8uint8int16uint16int32uint32float32float64c                 d   t          |           } t          dd          }|r t          | |j                  r| j        dk    r| S t          | j                  }|dk    r|                                 }|                                 }|t          k    r!|t          k    r|                     d          } n|t          k    r!|t          k    r|                     d          } n|t          k    r!|t          k    r|                     d          } n| S |d	k    r|                                 }|                                 }|t           k    r|dk    r|                     d
          } nP|t"          k    r|dk    r|                     d          } n)|t$          k    r|dk    r|                     d          } n| S t          | j                  }|t&          v rat&          |         t)          j        |                               d          d}| j        dk    rt          | j                  dd         |d<   |S | S )zi
    Convert numpy array to plotly.js typed array spec
    If not possible return the original value
    numpyFshould_loadr   int64r   r   r   uint64r   r   r   ascii)dtypebdata   shape)r   r   
isinstancendarraysizestrr"   maxminint8maxint8minastypeint16maxint16minint32maxint32minuint8max	uint16max	uint32maxplotlyjsShortTypesbase64	b64encodedecodendimr&   )vnpr"   r+   r,   arrObjs         S/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/_plotly_utils/utils.pyto_typed_array_specr@   '   s   
 	%Q''A 
G	/	/	/B Z2:.. !&A++LLE eeggeegg'>>cWnn  AAH__!!AAH__!!AAH	(		eeggeegg(??saxx!!AAI#((""AAI#((""AAHLLE"""'.%a((//88
 

 6A::!!'ll1R40F7OH    c                 B     g d}t           fd|D                       S )zR
    Return whether the key is skipped for conversion to the typed array spec
    )geojsonlayerlayersrangec              3   $   K   | ]
}|k    V  d S N ).0skipped_keykeys     r?   	<genexpr>z!is_skipped_key.<locals>.<genexpr>d   s(      BBk{c!BBBBBBrA   )any)rL   skipped_keyss   ` r?   is_skipped_keyrP   _   s3     ;::LBBBB\BBBBBBrA   c                 j   t          | t                    r]|                                 D ]F\  }}t          |          rt	          |          rt          |          | |<   7t          |           Gd S t          | t                    st          | t                    r| D ]}t          |           d S d S rH   )	r'   dictitemsrP   r   r@   convert_to_base64listtuple)objrL   values      r?   rT   rT   g   s    #t 
%))++ 	) 	)JCc"" )%e,, ).u55C!%((((	) 	) 
C		 %*S%"8"8 % 	% 	%Ee$$$$% %	% 	%rA   c                 .    d }t          || g           }|S )z0
    Custom cumsum to avoid a numpy import.
    c                 L    t          |           dk    r|gS | | d         |z   gz   S Nr   r%   )len)axs     r?   _reducerzcumsum.<locals>._reducerz   s,    q66Q;;3JAbEAI;rA   r   )r^   r_   rets      r?   cumsumra   u   s)    
  
 1b
!
!CJrA   c                        e Zd ZdZd Z fdZd Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zed             Zed             Zed             Z x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                     |dv rdS |S )zU
        This is used to ultimately *encode* into strict JSON, see `encode`

        )Infinityz	-InfinityNaNNrI   )selfconsts     r?   coerce_to_strictz"PlotlyJSONEncoder.coerce_to_strict   s     4444LrA   c                 6   t          t          |                               |          }d|v sd|v s|S 	 t          j        || j                  }t          j        || j        | j        | j	        | j
        f          S # t          $ r t          d          w xY w)z
        Load and then dump the result using parse_constant kwarg

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

        rf   re   )parse_constant)	sort_keysindent
separatorszSEncoding into strict JSON failed. Did you set the separators valid JSON separators?)superrc   encode_jsonloadsri   dumpsrl   rm   item_separatorkey_separator
ValueError)rg   o	encoded_onew_o	__class__s       r?   rp   zPlotlyJSONEncoder.encode   s     +T2299!<<	 ""jI&=&=
	K	$:OPPPE ;.{ /1CD	     	 	 	 )  	s   A> >Bc           	          | j         | j        | j        | j        | j        | j        | j        | j        | j        f	}|D ]}	  ||          c S # t          $ r Y w xY 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NotEncodablerq   JSONEncoderdefault)rg   rW   encoding_methodsencoding_methods       r?   r   zPlotlyJSONEncoder.default   s    B ! !#"

  0 	 	O&s+++++    ((s333s   
A
AAc                 V    	 |                                  S # t          $ r t          w xY w)z1Attempt to use a builtin `to_plotly_json` method.)to_plotly_jsonAttributeErrorr   rW   s    r?   r|   z"PlotlyJSONEncoder.encode_as_plotly   s:    	%%''' 	 	 		    (c                 X    t          | d          r|                                 S t          )z@Attempt to use `tolist` method to convert to normal Python list.tolist)hasattrr   r   r   s    r?   r   z PlotlyJSONEncoder.encode_as_list   s*     3!! 	::<<rA   c                     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   r   RRfloatZZint)rW   sage_alls     r?   r}   z PlotlyJSONEncoder.encode_as_sage  sV     j)) 	(+::HKs88OrA   c                     t          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pandasFr   NNA)r   r   NaTr   r   )rW   r   s     r?   r   z"PlotlyJSONEncoder.encode_as_pandas  sa     H%888 	&*4 64   	SFI%5%54rA   c                 B   t          dd          }|st          | |j        j        j        u rt          d          S t          | |j                  rH| j        j	        dk    r8	 |
                    |                                           S # t          $ r Y nw xY wt          )z'Attempt to convert numpy.ma.core.maskedr   Fr   nanM)r   r   macoremaskedr   r'   r(   r"   kinddatetime_as_stringr   	TypeError)rW   r   s     r?   r~   z!PlotlyJSONEncoder.encode_as_numpy  s     7666 	%(-&&&<<U]++ 		#0E0E//44;;===    s   #&B
 

BBc                 V    	 |                                  S # t          $ r t          w xY w)z.Convert datetime objects to iso-format strings)	isoformatr   r   r   s    r?   r   z$PlotlyJSONEncoder.encode_as_datetime0  s6    	==??" 	 	 		r   c                 t    	 |                                  }t          |          S # t          $ r t          w xY w)z=Attempt to convert to utc-iso time string using date methods.)r   iso_to_plotly_time_stringr   r   )rW   time_strings     r?   r   z PlotlyJSONEncoder.encode_as_date8  sG    	:--//K -[999  	 	 		s   % 7c                 b    t          | t          j                  rt          |           S t          )z3Attempt to encode decimal by converting it to float)r'   decimalDecimalr   r   r   s    r?   r   z#PlotlyJSONEncoder.encode_as_decimalB  s*     c7?++ 	::rA   c                     t          d          }|)t          | |j                  rt          j        |           S t
          )z5Attempt to convert PIL.Image.Image to base64 data uriz	PIL.Image)r   r'   Imager   pil_image_to_urir   )rW   images     r?   r   zPlotlyJSONEncoder.encode_as_pilJ  s?     ;''C!=!=$5c:::rA   )__name__
__module____qualname____doc__ri   rp   r   staticmethodr|   r   r}   r   r~   r   r   r   r   __classcell__)rz   s   @r?   rc   rc      sU       	 		 	 	# # # # #J04 04 04d   \   \   \   \   \    \ : : \:   \   \    rA   rc   c                       e Zd ZdS )r   N)r   r   r   rI   rA   r?   r   r   T  s        DrA   r   c                 v   |                      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).-N   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_strings    r?   r   r   X  s     	bqb!W,,*2B2B32G2G2Jg2U2U8
 
 	

 ##Hb1199(BGGJ;'' ,!!+r222!!#s+++rA   c                        fd}|S )Nc                 t    t           j        d d         dk    s| j         | j        j        di | _        | S )N   )r   r   rI   )sysversion_infor   format)funcnamess    r?   
_decoratorz template_doc.<locals>._decoratorj  sC    #v--|'2t|2;;U;;rA   rI   )r   r   s   ` r?   template_docr   i  s$         rA   Fc                 ,    d }t          | ||          S )Nc                     t          j        d|           }t          t          |                    D ]+}	 t	          ||                   ||<   # t
          $ r Y (w xY wt          |          S )Nz(\d+))rer   rF   r\   r   rv   rV   )r<   v_partsis      r?   rL   z"_natural_sort_strings.<locals>.keyt  sx    (8Q''s7||$$ 	 	A __

    W~~s   A
AA)rL   reversesorted)valsr   rL   s      r?   _natural_sort_stringsr   s  s(       $C1111rA   c                  Z    t          dd          } | rt          | j        f}nt          f}|S )Nr   Fr   )r   r   integer)r=   int_types     r?   _get_int_typer     s6    	G	/	/	/B	 $6OrA   c                     t          |          dk    r| S |                                t          d t          f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                     | |z   S rH   rI   )r^   ys     r?   <lambda>z!split_multichar.<locals>.<lambda>  s
    QU rA   c                 .    |                                S rH   )r   )r^   cs    r?   r   z!split_multichar.<locals>.<lambda>  s    !''!** rA   )r\   popr   mapsplit_multichar)sscharsr   s     @r?   r   r     s]     5zzQ			A	""C(<(<(<(<b$A$A	B	BB2u%%%rA   c                     t          t          d t          t          t	          |                     t          dgt          t          t          | dd                             z                                           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                 $    | d         | d         z   S )Nr   r$   rI   )ts    r?   r   z(split_string_positions.<locals>.<lambda>  s    adQqTk rA   r   Nr%   )rU   r   ziprF   r\   ra   )r   s    r?   split_string_positionsr     sk    " !!c"ggsT#c2crc72C2C-D-D'D E EFF	
 	
  rA   r$   ^Tc                 H   d t          t          |           dz   |z   |z             D             }d}|%| D ]!}t          |          D ]}	||z   |	z   }|||<   "n%t          |          D ]}	| |         |z   |	z   }|||<   d                    |          }
|r|
d|dz            }
|
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                     g | ]}d S )r   rI   )rJ   _s     r?   
<listcomp>z,display_string_positions.<locals>.<listcomp>  s    ::::::rA   r$   r   Nr   )rF   r+   join)pr   offsetlengthchartrimsmaxaddrp_lr`   s              r?   display_string_positionsr     s    : 	;:eCFFQJ/&899:::AGy 	" 	"B6]] " "v+/!'

"	"
 v 	 	AdVma'GAgJJ
''!**C !-GaK- JrA   c           
      z   d }|r" |t           ||           |                    S t          |           s| S t          t          t          |                     dk    r|t          |           dz
  z  gS  G 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                 ,    d | D             d d d         S )Nc                 $    g | ]}|d d d         S )Nr%   rI   )rJ   r   s     r?   r   z5chomp_empty_strings.<locals>._rev.<locals>.<listcomp>  s"    ###A$$B$###rA   r%   rI   )r   s    r?   _revz!chomp_empty_strings.<locals>._rev  s!    #####DDbD))rA   r   r$   c                       e Zd Zd Zd ZdS )%chomp_empty_strings.<locals>._Chomperc                     || _         d S rH   )r   )rg   r   s     r?   __init__z.chomp_empty_strings.<locals>._Chomper.__init__  s    DFFFrA   c                 l    t          |          dk    r|d d         |d         | j        z   gz   S ||gz   S r[   )r\   r   )rg   r^   r   s      r?   __call__z.chomp_empty_strings.<locals>._Chomper.__call__  s>    
 1vv{{"v2 000A3wrA   N)r   r   r   r  r  rI   rA   r?   _Chomperr    s2        	 	 		 	 	 	 	rA   r  r   )chomp_empty_stringsr\   sumr   rU   filterr   )stringsr   r   r   r  s        r?   r  r    s    &* * *  ;t'Wq99:::w<< 
3sG""S\\A%&''        sF88A;;">>??@@@rA   c           	         t          |           t          |          k     rt          ||           S t          |          dk    rt          |           S t          t          |          dz             }t          |           D ]n\  }}|dz   g}t          |          D ]Q\  }}||dz            dz   }||         dz   }	||         ||k    z   }
|                    t          ||	|
                     R|}o|d         S )Nr   r$   r%   )r\   levenshteinrF   	enumerateappendr,   )s1s2previous_rowr   c1current_rowjc2
insertions	deletionssubstitutionss              r?   r  r    s   
2wwR2r"""
2ww!||2wwR1%%L2 	# 	#21ugr]] 	J 	JEAr &a!e,q0J#A*I(OrRx8Ms:y-HHIIII"rA   c                 <      fd}t          ||          d         S )Nc                 (    t          |           | fS rH   )r  )r   strings    r?   _keyz!find_closest_string.<locals>._key)  s     Av&&**rA   )rL   r   r   )r  r
  r  s   `  r?   find_closest_stringr  (  s6    + + + + +
 't$$$Q''rA   )F)Nr   r$   r   T)*r8   r   jsonrq   r   r   	functoolsr   _plotly_utils.optional_importsr   _plotly_utils.basevalidatorsr   r   r   r.   r-   r1   r0   r3   r2   r4   r5   r6   r7   r@   rP   rT   ra   r   rc   r   r   r   r   r   r   r   r   r   r  r  r  rI   rA   r?   <module>r"     s6         



 				       5 5 5 5 5 5          
		 	 	 5 5 5pC C C% % %  N N N N N) N N Nb	 	 	 	 	9 	 	 	, , ,"  2 2 2 2  & & &(  2+ + + +\+A +A +A +A`  &( ( ( ( (rA   