
    g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y)    Nreduce)
get_module)ImageUriValidatorc                 &    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   )len)axs     P/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/_plotly_utils/utils.py_reducerzcumsum.<locals>._reducer   s(    q6Q;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y|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Lr   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.

        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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NotEncodabler#   JSONEncoderdefault)r   objencoding_methodsencoding_methods       r   r9   zPlotlyJSONEncoder.defaultU   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AttributeErrorr7   r:   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)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   sH     j)(++:HKKs8Or   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pandasFshould_loadNNA)r   r7   NaTrD   rO   )r:   rL   s     r   r1   z"PlotlyJSONEncoder.encode_as_pandas   sH     H%8&** 64 SFII%5r   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.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%((--&&&<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?   r7   r@   s    r   r2   z$PlotlyJSONEncoder.encode_as_datetime   s(    	==?" 		rA   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?   r7   )r:   time_strings     r   r3   z PlotlyJSONEncoder.encode_as_date   s6    	:--/K -[99  		s    -c                 X    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   s8     ;'C!=$55c::r   )__name__
__module____qualname____doc__r   r"   r9   staticmethodr.   r4   r/   r1   r0   r2   r3   r5   r6   __classcell__)r,   s   @r   r   r      s    		#J04d              : :    r   r   c                       e Zd Zy)r7   N)rj   rk   rl   r   r   r   r7   r7      s    r   r7   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   ra   ra      s     	bq!W,*2B2B32G2Jg2U8
 	

 ##Hb199(BGJ;'!!+r22!!#s++r   c                        fd}|S )Nc                     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   sB    #v-||'2t||22;U;r   r   )r   r   s   ` r   template_docr      s     r   c                 $    d }t        | ||      S )Nc                     t        j                  d|       }t        t        |            D ]  }	 t	        ||         ||<    t        |      S # t
        $ r Y -w xY w)Nz(\d+))rerx   ranger   rI   r(   tuple)vv_partsis      r   keyz"_natural_sort_strings.<locals>.key
  s`    ((8Q's7|$ 	A _
	 W~  s   A	AA)r   reversesorted)valsr   r   s      r   _natural_sort_stringsr   	  s     $C11r   c                  Z    t        dd      } | rt        | j                  f}|S t        f}|S )NrR   FrM   )r   rI   integer)npint_types     r   _get_int_typer     s4    	G	/B	$ O 6Or   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 Nr   )r   ys     r   <lambda>z!split_multichar.<locals>.<lambda>0  s
    QU r   c                 &    | j                        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      sB     5zQ			A	"C(<b$A	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  sL    " !c"gsT#c2cr72C-D'D EF	
 r   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   rw   r   Nru   )r   maxjoin)pr   offsetlengthchartrim_smaxaddrp_lr   s               r   display_string_positionsr   M  s    : CFQJ/&89::A:Gy 	"B6] "v+/!'
"	"
 v 	AdVma'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
   r   )r   r   s     r   _revz!chomp_empty_strings.<locals>._rev  s'    !"#A$B$#DbD))#s   r   r   c                       e Zd Zd Zd Zy)%chomp_empty_strings.<locals>._Chomperc                     || _         y r   )r   )r   r   s     r   __init__z.chomp_empty_strings.<locals>._Chomper.__init__  s	    DFr   c                 \    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:    
 1v{"v2 000A3wr   N)rj   rk   rl   r   r   r   r   r   _Chomperr     s    		r   r   ru   )chomp_empty_stringsr   sumr   r   filterr   )stringsr   r   r   r   s        r   r   r   {  s    &* 'Wq9::w<
3sG"S\A%&''  sF8A;">?@@r   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   levenshteinr   	enumerateappendmin)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 #	# 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     s    +
 't$Q''r   )F)Nr   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      su      
 	  5 :N)) Nb	9 	,"2&(2+\+A`&(r   