
    .dgH,                     t    d Z ddlZddlZ G d de      Z G d de      Zd Z G d d	      Z G d
 de      Zy)zW
Classes used to support string serialization of Parameters and
Parameterized objects.
    Nc                       e Zd Zy)UnserializableExceptionN__name__
__module____qualname__     0lib/python3.12/site-packages/param/serializer.pyr   r   	       r
   r   c                       e Zd Zy)UnsafeserializableExceptionNr   r	   r
   r   r   r      r   r
   r   c                     d| ddigiS )zSExpress a JSON schema type as nullable to easily support Parameters that allow_NoneanyOftypenullr	   )	json_types    r   JSONNullabler      s    y66"2355r
   c                   f    e Zd ZdZedd       Zedd       Zedd       Zed        Zed        Z	y)	Serializationz>Base class used to implement different types of serialization.Nc                     t         NNotImplementedErrorclspobjsubsets      r   schemazSerialization.schema   s    !!r
   c                     t         )z
        Serialize the parameters on a Parameterized object into a
        single serialized object, e.g. a JSON string.
        r   r   s      r   serialize_parametersz"Serialization.serialize_parameters   
     "!r
   c                     t         )z
        Deserialize a serialized object representing one or
        more Parameters into a dictionary of parameter values.
        r   )r   r   
serializedr   s       r   deserialize_parametersz$Serialization.deserialize_parameters$   r"   r
   c                     t         )z#Serialize a single parameter value.r   )r   r   pnames      r   serialize_parameter_valuez'Serialization.serialize_parameter_value,   
     "!r
   c                     t         )z%Deserialize a single parameter value.r   r   r   r'   values       r   deserialize_parameter_valuez)Serialization.deserialize_parameter_value1   r)   r
   r   )
r   r   r   __doc__classmethodr   r!   r%   r(   r-   r	   r
   r   r   r      sg    H" " " " " " " " " "r
   r   c            	          e Zd ZdZdgZededed ed      diZ	e
d        Ze
d	        Ze
d#d
       Ze
d$d       Ze
d$d       Ze
d        Ze
d#d       Ze
d        Ze
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        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%y)&JSONSerializationz
    Class responsible for specifying JSON serialization, deserialization
    and JSON schemas for Parameters and Parameterized classes and
    objects.
    CallableintegernumberstringNr   c                 ,    t        j                  |      S r   )jsonloads)r   r$   s     r   r8   zJSONSerialization.loadsE   s    zz*%%r
   c                 ,    t        j                  |      S r   )r7   dumps)r   objs     r   r:   zJSONSerialization.dumpsI   s    zz#r
   c                    i }|j                   j                  d      j                         D ]  \  }}|||vr|j                  |      ||<   |j                  rCt        j                  |j                        j                  dd      j                         ||   d<   |j                  s~|j                  ||   d<    |S )Nexistingsafe
 descriptiontitle)
paramobjectsitemsr   doctextwrapdedentreplacestriplabel)r   r   r?   r   r   nameps          r   r   zJSONSerialization.schemaM   s    zz))*5;;=GD!!d&&8888.F4Luu.6ooaee.D.L.LTSV.W.].]._t]+ww()tW% > r
   c                     i }|j                   j                  d      j                         D ];  \  }}|||vr|j                   j                  |      }|j	                  |      ||<   = | j                  |      S )Nr=   )rD   rE   rF   get_value_generator	serializer:   )r   r   r   
componentsrM   rN   r,   s          r   r!   z&JSONSerialization.serialize_parametersZ   su    
zz))*5;;=GD!!d&&8JJ2248E {{51Jt	 >
 yy$$r
   c                     | j                  |      }i }|j                         D ]/  \  }}|||vr|j                  |   j                  |      }|||<   1 |S r   )r8   rF   rD   deserialize)r   r   serializationr   deserializedrR   rM   r,   s           r   r%   z(JSONSerialization.deserialize_parametersd   sg    yy/
'--/KD%!d&&8::d+77>L+Jt	 0
 r
   c                 H    |j                         dz   |z   }t        | |d      S )z7Returns specialized method if available, otherwise None_N)lowergetattr)r   ptypesuffixmethod_names       r   _get_methodzJSONSerialization._get_methodq   s(     kkmC'&0sK..r
   c                     || j                   v rt        | j                  |d      }|r |||      }nd|j                         i}|j                  rt        |      S |S )Nr   r>   r   )unserializable_parameter_typesr   r^   rY   
allow_Noner   )r   r[   rN   r?   r   dispatch_methodr   s          r   param_schemazJSONSerialization.param_schemaw   s[    C666))//%:$QT2Fekkm,F'(|||F#??r
   c                     |j                   j                  |      }| j                  |j                   |   j                  |            S r   )rD   rP   r:   rQ   r+   s       r   r(   z+JSONSerialization.serialize_parameter_value   s9    

..u5yyE*44U;<<r
   c                 `    | j                  |      }|j                  |   j                  |      S r   )r8   rD   rT   r+   s       r   r-   z-JSONSerialization.deserialize_parameter_value   s*    		% zz% ,,U33r
   c                    ddl m} t        |t              r!d|D cg c]  }| j	                  |       c}iS || j
                  v rd| j
                  |   iS t        ||      rd|j                  j                  |      dS ddiS c c}w )N   )Parameterizedr   r   objectr   
properties)	parameterizedrh   
isinstancetupleclass__schemajson_schema_literal_types
issubclassrD   r   )r   class_r?   rh   cls_s        r   ro   zJSONSerialization.class__schema   s    0fe$&I&$c//5&IJJs444C99&ABB.$FLL4G4G4MNNH%% Js   Bc                 ,    |du rd}t        |      ddiS )NTzLArray is not guaranteed to be safe for serialization as the dtype is unknownr   arrayr   r   rN   r?   msgs       r   array_schemazJSONSerialization.array_schema   s&    4<;C-c22  r
   c                 <    | j                  |j                  |      S Nr>   )ro   rr   r   rN   r?   s      r   classselector_schemaz&JSONSerialization.classselector_schema   s       55r
   c                 ,    |du rd}t        |      ddiS )NTzZDict is not guaranteed to be safe for serialization as the key and value types are unknownr   ri   rv   rw   s       r   dict_schemazJSONSerialization.dict_schema   s'    4<JC-c22!!r
   c                     dddS )Nr5   z	date-timer   formatr	   r|   s      r   date_schemazJSONSerialization.date_schema   s     K88r
   c                     dddS )Nr5   dater   r	   r|   s      r   calendardate_schemaz%JSONSerialization.calendardate_schema   s     F33r
   c                 b    ddi}|j                   |j                   |d<   |j                   |d<   |S )Nr   ru   minItemsmaxItems)lengthr   rN   r?   r   s       r   tuple_schemazJSONSerialization.tuple_schema   s7    '"88"#((F:"#((F:r
   c                     d|j                   j                  j                         i}| j                  ||j                  |j
                        S )Nr   )	__class__r   rY   declare_numeric_boundsboundsinclusive_boundsr   s       r   number_schemazJSONSerialization.number_schema   s>    !++..4468))&!((A<N<NOOr
   c                 T    |%|\  }}||d   rdnd}|||<   ||d   rdnd}|||<   |S )zCGiven an applicable numeric schema, augment with bounds informationr   minimumexclusiveMinimumrg   maximumexclusiveMaximumr	   )r   r   r   r   lowhighkeys          r   r   z(JSONSerialization.declare_numeric_bounds   sS      KS$#3A#6i<N!s#3A#6i<N"sr
   c                 $    | j                  |      S r   )r   r|   s      r   integer_schemaz JSONSerialization.integer_schema   s      ##r
   c                 :    | j                  ||      }ddi|d<   |S Nr>   r   r4   additionalItems)r   r   s       r   numerictuple_schemaz%JSONSerialization.numerictuple_schema   s,    !!!$!/%+X$6 !r
   c                 (    | j                  ||      S r{   )r   r|   s      r   xycoordinates_schemaz&JSONSerialization.xycoordinates_schema   s    &&qt&44r
   c                     | j                  ||      }| j                  ddi|j                  |j                        }||d<   |S r   )r   r   r   r   )r   rN   r?   r   bounded_numbers        r   range_schemazJSONSerialization.range_schema   sM    !!!$!/33X!*<*<>$2 !r
   c                     ddi}|du r|j                   d}t        |      |j                   | j                  |j                   |      |d<   |S )Nr   ru   TzPList without a class specified cannot be guaranteed to be safe for serializationr>   rF   )	item_typer   rr   ro   )r   rN   r?   r   rx   s        r   list_schemazJSONSerialization.list_schema   sZ    '"4<AKK/2C-c2288!//$/GF7Or
   c                     	 |j                   D cg c]  }d| j                  t        |         i }}d|i}|j                   |d<   |S c c}w # t        $ r |du rd}t	        |      i cY S w xY w)Nr   r   enumTzfObjectSelector cannot be guaranteed to be safe for serialization due to unserializable type in objects)rE   rp   r   	Exceptionr   r   rN   r?   r;   allowed_typesr   rx   s          r   objectselector_schemaz'JSONSerialization.objectselector_schema   s    	()		3(1 %c&C&CDI&NO(1  3}-FYYF6NM	3
  	t|M1#66I	s!   A !AA A A,+A,c                     	 |j                   j                         D cg c]  }d| j                  t        |         i }}d|i}|j                   |d<   |S c c}w # t        $ r |du rd}t        |      i cY S w xY w)Nr   r   r   Tz`Selector cannot be guaranteed to be safe for serialization due to unserializable type in objects)rE   valuesrp   r   r   r   r   s          r   selector_schemaz!JSONSerialization.selector_schema   s    	()		(8(8(:<(: %c&C&CDI&NO(:  <}-FYYF6NM	<
  	t|M1#66I	s!   A !AA A A:9A:c                     |j                   
|du rd}ddiS |j                   D ]2  }t        |      | j                  vsdt        |      z  }t        |       dd|j                   idS )NTz]ListSelector cannot be guaranteed to be safe for serialization as allowed objects unspecifiedr   ru   z%ListSelector cannot serialize type %sr   )r   rF   )rE   r   rp   r   )r   rN   r?   rx   r;   s        r   listselector_schemaz%JSONSerialization.listselector_schema  su    99t|FG$$99CCy = ===S	I-c22   6199*=>>r
   c                    ddi}|du rd}t        |      |j                  	ddi|d<   |S d\  }}t        |j                  t              r|j                  |j                  }}n)t        |j                  t              r|j                  \  }}t        |j                  t              st        |j                  t              r	d||d|d<   t        |j                  t
              st        |j                  t              rO| j                  j                         D cg c]  }d|i }}d	|i}	|j                  D 
ci c]  }
|
|	 }}
d|d
|d<   d\  }}t        |j                  t              r|j                  |j                  }}n)t        |j                  t              r|j                  \  }}|||d<   |||d<   |S c c}w c c}
w )Nr   ru   TzYDataFrame is not guaranteed to be safe for serialization as the column dtypes are unknownri   rF   )NN)r   r   r   r   rj   r   r   )
r   columnsrm   intrn   listsetrp   r   rows)r   rN   r?   r   rx   mincolsmaxcolselliteral_typesallowable_typesrM   rk   minrowsmaxrowss                 r   dataframe_schemaz"JSONSerialization.dataframe_schema  s   '"4<DC-c2299%x0F7OM%aii% yy!))WG		5) yyGWaii%AIIu)E(0g,3 5F7O aii&*QYY*D363P3P3W3W3YZ3YRfR[3YMZ&6O<=IIFID$/IJF(0
KF7O%affc" vvqvvWG& vvGW!(F:!(F:! [Fs   F=8
G)FNr   )F)&r   r   r   r.   r`   r   floatstrr   rp   r/   r8   r:   r   r!   r%   r^   rc   r(   r-   ro   ry   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r
   r   r1   r1   7   sQ    '1\" 	IuXs8T
F!
 & &   
 
 % %   / /
 @ @ = = 4 4 	& 	& ! ! 6 6 " " 9 9 4 4   P P 
 
 $ $  
 5 5         
? 
? % %r
   r1   )	r.   r7   rH   r   r   r   r   r   r1   r	   r
   r   <module>r      sH   
  	i 		) 	6" "DK Kr
   