
    WhlB              	       ,   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j8                        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)y)+    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                    t        |       } t        dd      }|r%t        | |j                        r| j                  dk(  r| S t        | j                        }|dk(  r| j                         }| j                         }|t        k  r|t        k\  r| j                  d      } n|t        k  r|t        k\  r| j                  d      } n|t        k  r|t        k\  r| j                  d      } n| S |d	k(  r| j                         }| j                         }|t         k  r|dk\  r| j                  d
      } nB|t"        k  r|dk\  r| j                  d      } n"|t$        k  r|dk\  r| j                  d      } n| S t        | j                        }|t&        v rZt&        |   t)        j*                  |       j-                  d      d}| j.                  dkD  rt        | j0                        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         M/var/www/html/jupyter_env/lib/python3.12/site-packages/_plotly_utils/utils.pyto_typed_array_specr@   '   s   
 	%Q'A 
G	/BZ2::.!&&A+LE eegeeg'>cWn AH_!AH_!AH	(	eegeeg(?sax!AI#("AI#("AHLE""'.%%a(//8

 66A:!!''l1R0F7OH    c                 4     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(    y wN ).0skipped_keykeys     r?   	<genexpr>z!is_skipped_key.<locals>.<genexpr>d   s     Bk{c!Bs   )any)rL   skipped_keyss   ` r?   is_skipped_keyrP   _   s     ;LB\BBBrA   c                    t        | t              rJ| j                         D ]6  \  }}t        |      rt	        |      rt        |      | |<   ,t        |       8 y t        | t              st        | t              r| D ]  }t        |        y y rH   )	r'   dictitemsrP   r   r@   convert_to_base64listtuple)objrL   values      r?   rT   rT   g   s    #t))+ 	)JCc"%e,.u5C!%(	) 
C	*S%"8 	%Ee$	% #9rA   c                 &    d }t        || g       }|S )z0
    Custom cumsum to avoid a numpy import.
    c                 <    t        |       dk(  r|gS | | d   |z   gz   S Nr   r%   )len)axs     r?   _reducerzcumsum.<locals>._reducerz   s(    q6Q;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y|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LrA   c                 8   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9!<	 "jI&=
	KK	$:O:OPE ;;..{{ //1C1CD	   	) 	s   !B Bc           	      L   | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  f	}|D ]  }	  ||      c S  t        j                  j                  | |      S # t        $ r Y :w xY w)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   s   ,B	B#"B#c                 J    	 | j                         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                 F    t        | d      r| j                         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   sH     j)(++:HKKs8OrA   c                     t        dd      }|st        | |j                  u ryt        |d      r| |j                  u ry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  sH     H%8&** 64 SFII%5rA   c                 X   t        dd      }|st        | |j                  j                  j                  u rt        d      S t        | |j                        r9| j                  j                  dk(  r 	 |j                  |       j                         S t        # t        $ r Y t        w xY w)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%((--&&&<U]]+		#0E//4;;==   s   3B 	B)(B)c                 J    	 | j                         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_datetime/  s(    	==?" 		r   c                 `    	 | j                         }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_date7  s6    	:--/K -[99  		s    -c                 X    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_decimalA  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_pilI  s8     ;'C!=$55c::rA   )__name__
__module____qualname____doc__ri   rp   r   staticmethodr|   r   r}   r   r~   r   r   r   r   __classcell__)rz   s   @r?   rc   rc      s    		"H04d              : :    rA   rc   c                       e Zd Zy)r   N)r   r   r   rI   rA   r?   r   r   S  s    rA   r   c                 "   | j                  d      dd dk(  s| j                  d      d   dk(  rt        d      | j                  dd	      j                  d
d	      } | j                  d      r| j                  dd	      S | j                  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   W  s     	bq!W,*2B2B32G2Jg2U8
 	

 ##Hb199(BGJ;'!!+r22!!#s++rA   c                        fd}|S )Nc                     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>._decoratori  sB    #v-||'2t||22;U;rA   rI   )r   r   s   ` r?   template_docr   h  s     rA   c                 $    d }t        | ||      S )Nc                     t        j                  d|       }t        t        |            D ]  }	 t	        ||         ||<    t        |      S # t
        $ r Y -w xY w)Nz(\d+))rer   rF   r\   r   rv   rV   )r<   v_partsis      r?   rL   z"_natural_sort_strings.<locals>.keys  s`    ((8Q's7|$ 	A _
	 W~  s   A	AA)rL   reversesorted)valsr   rL   s      r?   _natural_sort_stringsr   r  s     $C11rA   c                  Z    t        dd      } | rt        | j                  f}|S t        f}|S )Nr   Fr   )r   r   integer)r=   int_types     r?   _get_int_typer     s4    	G	/B	$ O 6OrA   c                     t        |      dk(  r| S |j                         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                 &    | j                        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     sB     5zQ			A	"C(<b$A	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     sL    " !c"gsT#c2cr72C-D'D EF	
 rA   c                 ,   t        t        |       dz   |z   |z         D cg c]  }d }}d}|%| D ]  }	t        |      D ]  }
|	|z   |
z   }|||<    ! n t        |      D ]  }
| |   |z   |
z   }|||<    dj                  |      }|r|d|dz    }|S c c}w )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.
    r$   r   r   Nr   )rF   r+   join)pr   offsetlengthchartrim_smaxaddrp_tempr`   s               r?   display_string_positionsr     s    : CFQJ/&89::A:Gy 	"Bf "v+,!'
"	"
 &M 	DdVmd*GAgJ	 ''!*C-GaK J 	;s   	Bc           
         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 cg c]
  }|d d d    c}d d d   S c c}w )Nr%   rI   )r   r   s     r?   _revz!chomp_empty_strings.<locals>._rev  s'    !%&A$B$&tt,,&s   r   r$   c                       e Zd Zd Zd Zy)%chomp_empty_strings.<locals>._Chomperc                     || _         y rH   )r   )rg   r   s     r?   __init__z.chomp_empty_strings.<locals>._Chomper.__init__  s	    DFrA   c                 \    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:    
 1v{"v2 000A3wrA   N)r   r   r   r   r  rI   rA   r?   _Chomperr     s    		rA   r  r   )chomp_empty_stringsr\   sumr   rU   filterr   )stringsr   r   r   r  s        r?   r  r    s    &- 'Wq9::w<
3sG"S\A%&''  sF8A;">?@@rA   c           	         t        |       t        |      k  rt        ||       S t        |      dk(  rt        |       S t        t        |      dz         }t        |       D ]Z  \  }}|dz   g}t        |      D ]?  \  }}||dz      dz   }||   dz   }	||   ||k7  z   }
|j	                  t        ||	|
             A |}\ |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    
2wR2r""
2w!|2wR1%L2 	#21ugr] 	JEAr &a!e,q0J#A*I(OrRx8Ms:y-HI	J #	# 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  '  s    +
 't$Q''rA   )F)Nr   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     s       
 	  5  
		 	 5pC%M)) M`	9 	,"2&(2+\+A`&(rA   