
    [6g8                         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
 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)ImageUriValidatorc                 .    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   )len)axs     R/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/_plotly_utils/utils.py_reducerzcumsum.<locals>._reducer   s,    q66Q;;3JAbEAI;    r   )r   r   rets      r   cumsumr      s)    
  
 1b
!
!CJr   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NaNN )selfconsts     r   coerce_to_strictz"PlotlyJSONEncoder.coerce_to_strict%   s     4444Lr   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.

        r   r   )parse_constant)	sort_keysindent
separatorszSEncoding into strict JSON failed. Did you set the separators valid JSON separators?)superr   encode_jsonloadsr   dumpsr   r   item_separatorkey_separator
ValueError)r   o	encoded_onew_o	__class__s       r   r"   zPlotlyJSONEncoder.encode0   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NotEncodabler#   JSONEncoderdefault)r   objencoding_methodsencoding_methods       r   r9   zPlotlyJSONEncoder.defaultU   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AttributeErrorr7   r:   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)hasattrrC   r7   r@   s    r   r4   z PlotlyJSONEncoder.encode_as_list   s*     3!! 	::<<r   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   r7   RRfloatZZint)r:   sage_alls     r   r/   z PlotlyJSONEncoder.encode_as_sage   sV     j)) 	(+::HKs88Or   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pandasFshould_loadNNA)r   r7   NaTrD   rO   )r:   rL   s     r   r1   z"PlotlyJSONEncoder.encode_as_pandas   sa     H%888 	&*4 64   	SFI%5%54r   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.maskednumpyFrM   nanM)r   r7   macoremaskedrG   
isinstancendarraydtypekinddatetime_as_stringrC   	TypeError)r:   rR   s     r   r0   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?   r7   r@   s    r   r2   z$PlotlyJSONEncoder.encode_as_datetime   s6    	==??" 	 	 		rA   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?   r7   )r:   time_strings     r   r3   z PlotlyJSONEncoder.encode_as_date   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)rX   decimalDecimalrG   r7   r@   s    r   r5   z#PlotlyJSONEncoder.encode_as_decimal   s*     c7?++ 	::r   c                     t          d          }|)t          | |j                  rt          j        |           S t
          )z5Attempt to convert PIL.Image.Image to base64 data uriz	PIL.Image)r   rX   Imager   pil_image_to_urir7   )r:   images     r   r6   zPlotlyJSONEncoder.encode_as_pil   s?     ;''C!=!=$5c:::r   )__name__
__module____qualname____doc__r   r"   r9   staticmethodr.   r4   r/   r1   r0   r2   r3   r5   r6   __classcell__)r,   s   @r   r   r      sU       	 		 	 	# # # # #J04 04 04d   \   \   \   \   \    \ : : \:   \   \    r   r   c                       e Zd ZdS )r7   N)rj   rk   rl   r   r   r   r7   r7      s        Dr   r7   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   ra   ra      s     	bqb!W,,*2B2B32G2G2Jg2U2U8
 
 	

 ##Hb1199(BGGJ;'' ,!!+r222!!#s+++r   c                        fd}|S )Nc                 t    t           j        d d         dk    s| j         | j        j        di | _        | S )N   )rs   r   r   )sysversion_inform   format)funcnamess    r   
_decoratorz template_doc.<locals>._decorator   sC    #v--|'2t|2;;U;;r   r   )r   r   s   ` r   template_docr      s$         r   Fc                 ,    d }t          | ||          S )Nc                     t          j        d|           }t          t          |                    D ]+}	 t	          ||                   ||<   # t
          $ r Y (w xY wt          |          S )Nz(\d+))rerx   ranger   rI   r(   tuple)vv_partsis      r   keyz"_natural_sort_strings.<locals>.key
  sx    (8Q''s7||$$ 	 	A __

    W~~s   A
AA)r   reversesorted)valsr   r   s      r   _natural_sort_stringsr   	  s(       $C1111r   c                  Z    t          dd          } | rt          | j        f}nt          f}|S )NrR   FrM   )r   rI   integer)npint_types     r   _get_int_typer     s6    	G	/	/	/B	 $6Or   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 Nr   )r   ys     r   <lambda>z!split_multichar.<locals>.<lambda>0  s
    QU r   c                 .    |                                S r   )rx   )r   cs    r   r   z!split_multichar.<locals>.<lambda>0  s    !''!** r   )r   popr   mapsplit_multichar)sscharsr   s     @r   r   r      s]     5zzQ			A	""C(<(<(<(<b$A$A	B	BB2u%%%r   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   )ts    r   r   z(split_string_positions.<locals>.<lambda>G  s    adQqTk r   r   Nr
   )listr   zipr   r   r   )r   s    r   split_string_positionsr   4  sk    " !!c"ggsT#c2crc72C2C-D-D'D E EFF	
 	
  r   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 )rw   r   ).0_s     r   
<listcomp>z,display_string_positions.<locals>.<listcomp>j  s    ::::::r   r   r   Nru   )r   maxjoin)pr   offsetlengthchartrimsmaxaddrp_lr   s              r   display_string_positionsr   M  s    : 	;:eCFFQJ/&899:::AGy 	" 	"B6]] " "v+/!'

"	"
 v 	 	AdVma'GAgJJ
''!**C !-GaK- Jr   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
   r   )r   r   s     r   r   z5chomp_empty_strings.<locals>._rev.<locals>.<listcomp>  s"    ###A$$B$###r   r
   r   )r   s    r   _revz!chomp_empty_strings.<locals>._rev  s!    #####DDbD))r   r   r   c                       e Zd Zd Zd ZdS )%chomp_empty_strings.<locals>._Chomperc                     || _         d S r   )r   )r   r   s     r   __init__z.chomp_empty_strings.<locals>._Chomper.__init__  s    DFFFr   c                 l    t          |          dk    r|d d         |d         | j        z   gz   S ||gz   S r	   )r   r   )r   r   r   s      r   __call__z.chomp_empty_strings.<locals>._Chomper.__call__  s>    
 1vv{{"v2 000A3wr   N)rj   rk   rl   r   r   r   r   r   _Chomperr     s2        	 	 		 	 	 	 	r   r   ru   )chomp_empty_stringsr   sumr   r   filterr   )stringsr   r   r   r   s        r   r   r   {  s    &* * *  ;t'Wq99:::w<< 
3sG""S\\A%&''        sF88A;;">>??@@@r   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   levenshteinr   	enumerateappendmin)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"r   c                 <      fd}t          ||          d         S )Nc                 (    t          |           | fS r   )r   )r   strings    r   _keyz!find_closest_string.<locals>._key  s     Av&&**r   )r   r   r   )r   r   r   s   `  r   find_closest_stringr     s6    + + + + +
 't$$$Q''r   )F)Nr   r   r   T)rd   jsonr#   r   r   	functoolsr   _plotly_utils.optional_importsr   _plotly_utils.basevalidatorsr   r   r8   r   ry   r7   ra   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s        



 				       5 5 5 5 5 5 : : : : : :  N N N N N) N N Nb	 	 	 	 	9 	 	 	, , ,"  2 2 2 2  & & &(  2+ + + +\+A +A +A +A`  &( ( ( ( (r   