
    .dg:                         d Z 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Z
dZdZdZd	Z G d
 d      ZdZedz  ZdaddZ G d d      Zd Z ede       y)an  
Optional IPython extension for working with Parameters.

This extension offers extended but completely optional functionality
for IPython users.  From within IPython, it may be loaded using:

%load_ext param.ipython

This will register the %params line magic to allow easy inspection of
all the parameters defined on a parameterized class or object:

%params <parameterized class or object>

All parameters of the class or object will be listed in the IPython
pager together with all their corresponding attributes and
docstrings. Note that the class or object to be inspected must already
exist in the active namespace.
    N)register_display_accessor)async_executorFz[1;31m%s[0mz[1;34m%s[0mz[1;32m%s[0mz[1;36m%s[0mc                   B    e Zd ZdZd
dZddZddZd ZddZd Z	d Z
y	)
ParamPagerz}
    Callable class that displays information about the supplied
    Parameterized object or class in the IPython pager.
    c                 $    g d| _         || _        y)z
        If metaclass is set to True, the checks for Parameterized
        classes objects are disabled. This option is for use in
        ParameterizedMetaclass for automatic docstring generation.
        )namechangedvaluetypeboundsmodeN)order	metaclass)selfr   s     -lib/python3.12/site-packages/param/ipython.py__init__zParamPager.__init__/   s     L
"    c                 *   t        |j                  j                  d            }t        |t              r2g }|j                         D ci c]  \  }}||j                   }}}|}nYt        |j                  j                  d      j                               }|j                  j                         }|j                  }|s1|j                         D 	ci c]  \  }}	||j                  v r||	 }}}	|j                  d       |||fS c c}}w c c}	}w )z
        Get the parameter dictionary, the list of modifed parameters
        and the dictionary of parameter values. If include_super is
        True, parameters are also collected from the super classes.
        existingT)onlychangedr   )dictparamobjects
isinstancer   itemsdefaultlistvalueskeys	__class____dict__pop)
r   objinclude_superparamsr	   kpval_dict
self_classvs
             r   get_param_infozParamPager.get_param_info:   s     cii''
34c$G17@1!))H@J399+++=BBDEGyy'')HJ'-||~ 3~eqj111 c~F 3 	

6'** A3s   
D	Dc                    |\  }}}g }g }| j                  |      D ]  }	|r|	|vr
|j                  |	||	   f       ! t        d |D              dz   }
t        |      D ]  \  }\  }	}d|	z  }t	        j
                  |j                  dn|j                        }t        rM|j                  d      s<t        |j                               dkD  r t        j                  j                  d|	       |j                  d      r|dd }|j                  d      r|j                         }t        |      dkD  r5|dd D cg c]  }d	|
z   |  }}|j                  |
      |d
   z   g|z   }n)t        |      dk(  r|j                  |
      |d
   z   g}ng }|dz  r(|j                  |D cg c]  }t         |z   c}       `|j                  |D cg c]  }t"        |z   c}        dj%                  |      S c c}w c c}w c c}w )z
        Build a string to that presents all of the parameter
        docstrings in a clean format (alternating red and blue for
        readability).
        c              3   8   K   | ]  \  }}t        |        y wN)len).0r   _s      r   	<genexpr>z.ParamPager.param_docstrings.<locals>.<genexpr>`   s     D3Ca#d)3Cs      z%s: Nz< No docstring available >
   zQMulti-line docstring for %r is incorrectly formatted  (should start with newline) r   )sort_by_precedenceappendmax	enumeratetextwrapdedentdocWARN_MISFORMATTED_DOCSTRINGS
startswithr/   
splitlinesr   mainwarningljustextendredbluejoin)r   infomax_col_lenonly_changedr%   r(   r	   contentsdisplayed_paramsr   right_shiftir'   heading
unindentedlineslinetail	all_linesels                       r   param_docstringszParamPager.param_docstringsR   s    '+#7++F3DG 3##T6$<$89 4
 D3CDDQF%&67LAyatmG!)E\]\a\abJ,"--d3Z=R=R=T9UXY9Y

"" $BCGI ''-'^
 ''- ))+E5zA~BG)L)$3+-.tf5)L%mmK858CDtK	Uq%mmK858CD		1u9 =9Rb9 =>I >IbrI >?1 84 yy"" M !> >s   :G8%G=
H
c                 N   |j                         D cg c]	  \  }}||f }}}d }t        ||      }t        j                  ||      }|D 	cg c]  \  }}	t	        |	       }
}}	|
D cg c]  }|D ]  }|d   dk7  s|d   |v r|d     }}}|S c c}}w c c}	}w c c}}w )z
        Sort the provided dictionary of parameters by their precedence value,
        preserving the original ordering for parameters with the
        same precedence.
        c                 B    | d   j                   | d   j                   S dS )Nr5   g:0yE>)
precedence)xs    r   <lambda>z/ParamPager.sort_by_precedence.<locals>.<lambda>   s!    adoo.I1Q4??StSr   )keyr   r   )r   sorted	itertoolsgroupbyr   )r   
parametersr'   pobjr%   key_fnsorted_paramsgroupsr1   grpordered_groupsgrouprU   ordered_paramss                 r   r7   zParamPager.sort_by_precedence   s     ,6+;+;+=>+=41d)+=>Sv62""=f=4:;FC$s)F;-; G^Ee evoA*1D Q%e%^ G ? <Gs   BB."B!c           	      .   g i }}|\  }}}	|D 
ci c]  }
|
d }}
| j                  |      }|D ]K  }||   }|r||	vr|j                  rdnd}|j                  rdnd}t        |d      r|j                  rdnd}| d	| | }t        ||         }t        |      |d
z
  kD  r|d|d
z
   dz   }||j                  j                  ||d}t        |d      rk|j                  dn|j                  \  }}d\  }}t        |d      r#|j                         \  }}|||}d}|||}d}||fdk7  r||f||<   d| d| d|d<   |D ]#  }t        ||   t        ||         g      }|||<   % |j                  ||f       N | j                  |||	||      S c c}
w )z
        Collect the information about parameters needed to build a
        properly formatted table and then tabulate it.
        r   CVRORW
allow_Nonez AN r6      Nz...)r   r   r   r
   r   )NN)FFget_soft_boundsT(z, ))r7   constantreadonlyhasattrrn   reprr/   r    __name__r   rq   r9   r8   	_tabulate)r   rH   r   rI   rJ   	info_listbounds_dictr%   r(   r	   r&   
col_widthsorderingr   r'   rt   ru   rn   r   r
   p_dictlbounduboundmark_lboundmark_uboundsoft_lboundsoft_uboundcol	max_widths                                r   _build_tablezParamPager._build_table   s   
 "$R;	&*#7#()5aac5
)**62DtAG 3jjscH zzttH")!\":q||QSJZq
:,7D$(E5z[1_-n{1}-5"AKK,@,@"U4F q(#010@ahh+7([1/0/0/@/@/B,K~+*A!,&*~+*A!,&*F#{2)4k(BK%)*6("VHA'>F8$C#fSk2B CD	"+
3  dF^,Q T ~~iWe[QQ[ *s   
Fc                    g g }}|D 	
ch c]  \  }}	|	D ]  }
|
  }}	}}
|D cg c]	  }||v s| }}g }t        |      D ]U  \  }}||   dz   }|j                         }|dk(  r|j                  |      n|j                  |      }|j	                  |       W |j	                  t
        dj                  |      z  dz          |D ]  \  }	}g }t        |      D ]  \  }}||   dz   }||v r||   nd}|dk(  r|j                  |      n|j                  |      }|dk(  r|j                  |	d      rq||	   \  }}|j                  d      \  }}|j                  d      \  }}|j                  d	      \  }}|r|dz   t        |z  z   n|}|rt        |z  d	z   |z   n|}| d| }|j	                  |        dj                  |      }|	|v r	t        |z  }|j	                  |         dj                  ||z         S c c}
}	}w c c}w )
a  
        Returns the supplied information as a table suitable for
        printing or paging.

        info_list:  List of the parameters name, type and mode.
        col_widths: Dictionary of column widths in characters
        changed:    List of parameters modified from their defaults.
        order:      The order of the table columns
        bound_dict: Dictionary of appropriately formatted bounds
        r3   r   ro   r4   r   F,rr   rs   )r:   
capitalizerC   centerr8   rF   rG   getrsplitcyanrE   ) r   rz   r|   r	   r   r{   rK   rS   r1   rowr&   
column_setr   columns	title_rowrN   width	formattedrH   row_listvalr   r   lvaluvallspacelstrustruspacer   r   row_texts                                    r   ry   zParamPager._tabulate   s    R$&/=iFAsAaai
="'=%33*+<3%=	(FAssOA%E.."C,-F		%(

58IIY'	 )
 	rwwy11$67 #ICH"7+#"3)$'4Kd3ib011CIIe,#**U:K	(?{s5'A1<S1A.[+!*!1!1#!6JD$#';;s#3LFD#';;s#3LD&;FVc\4$;7DF9DdTk3.v5$F#)(!F8 4I	* , wwx(Hg~>OOH%+ #. yy$''I >=s   G<	HHc                 .   d}| j                   st        |t        j                        }t        |t              xr t        |t        j                        }|s|st        d       y|rW|j                  j                  }t        j                  d|z   dz   |j                        }|rdnd|j                  z  }d|d| }|d	|j                  z  }d
t        |      z  }| d| d}	| j                  |d      }
|
d   st        |	z  }| dS | j                  |
| j                   dd      }| j#                  |
dd      }d}t        |	z  }|dt$        |z  z  z  }|dt&        dz  z  z  }|dz  }d}	|	 dd
t        |	      z   }t        |z  }| d| d| d| S )z
        Given a Parameterized object or class, display information
        about the parameters in the IPython pager.
        Nz.Object is not a Parameterized class or object.^z[0-9]+$ro   z %rzParameters of z	 instancezParameters of %r=r4   T)r$   r   z
Object has no parameters.(   F)rI   rJ   d   z?Parameters changed from their default values are marked in red.z
%sz%Soft bound values are marked in cyan.zB
C/V= Constant/Variable, RO/RW = ReadOnly/ReadWrite, AN=Allow NonezParameter docstrings:z

)r   r   r   Parameterizedr   
issubclassprintr    rx   rematchr   r/   r+   greenr   r   rV   rE   r   )r   	param_objtitleparameterized_objectparameterized_class
class_namedefault_nameobj_nameheading_lineheading_text
param_infotop_headingtable
docstringsdflt_msgheading_stringdocstring_headings                    r   __call__zParamPager.__call__   s   
 ~~#-i9L9L#M #-i#= $R(29U=P=P(Q   ),?FG#&0099
!xxJy(@)..Q!-2EINN4J(izJ=&7ESZ'<.3(($(G
!},.K!]"=>>!!*djjb/4 " 6 **:3UZ*[
T|+vx00v(O!OPP\\.(>C#l2C,C+DE"^3d5'.?-@ZLQQr   N)FT)r   F)r   F)rx   
__module____qualname____doc__r   r+   rV   r7   r   ry   r    r   r   r   r   )   s/    
#+0*#Z 4Rn0(f.Rr   r   zDWelcome to the param IPython extension! (https://param.holoviz.org/)z
Available magics: %paramsc                     ddl m}m}m ddlm | G fdd|             }|rt        t               t        sda| j                  |       y y )Nr   )Magicsmagics_class
line_magic)pagec                   <     e Zd ZdZ fdZW dfd	       Z xZS )+load_ipython_extension.<locals>.ParamMagicsz~
        Implements the %params line magic used to inspect the parameters
        of a parameterized class or object.
        c                 B    t        |   |i | t               | _        y r.   )superr   r   param_pager)r   argskwargsr    s      r   r   z4load_ipython_extension.<locals>.ParamMagics.__init__<  s    Gd-f-)|Dr   c                     |dk(  rt        d       y| j                  j                  |      }|j                  du rt        d|z         yj	                  | j                  |j                               y)aj  
            The %params line magic accepts a single argument which is a
            handle on the parameterized object to be inspected. If the
            object can be found in the active namespace, information about
            the object's parameters is displayed in the IPython pager.

            Usage: %params <parameterized class or object>
            ro   z$Please specify an object to inspect.NFz%Object %r not found in the namespace.)r   shell_object_findfoundr   r   r#   )r   parameter_s
namespacesr#   r   s       r   r%   z2load_ipython_extension.<locals>.ParamMagics.paramsA  sf     B<= **))+6CyyE!=KLIId&&sww/0r   )ro   N)rx   r   r   r   r   r%   __classcell__)r    r   r   s   @r   ParamMagicsr   5  s!    	
	,
 
	1 
	1r   r   T)
IPython.core.magicr   r   r   IPython.corer   r   message_loadedregister_magics)ipverboser   r   r   r   r   s        @@r   load_ipython_extensionr   /  sP    CC! 1f 1 1D g 
;' r   c                        e Zd ZdZdZd Zd Zy)IPythonDisplayz1Reactive display handler that updates the output.Tc                     || _         y r.   )	_reactive)r   reactives     r   r   zIPythonDisplay.__init__e  s	    !r   c                    ddl m} ddlm}m} ddlm} d t        | j                  |      r<| j                  j                   || j                  j                  ddifd       }n#| j                   | |      ddifd       }	         }||u rd }t        |t        j                         j                  	      y # t        $ r t         w xY w)
Nr   )depends)	Undefinedresolve_ref)rxwatchTc                  8    j                                 y y r.   updater   r   cbhandles     r   update_handlez.IPythonDisplay.__call__.<locals>.update_handlep      %MM"$' &r   c                  8    j                                 y y r.   r   r   s     r   r   z.IPythonDisplay.__call__.<locals>.update_handlev  r   r   )
display_id)param.dependsr   param.parameterizedr   r   param.reactiver   r   r   	_callback_paramsdisplayuuiduuid4hex	TypeErrorNotImplementedError)	r   r   r   r   r   r   r#   r   r   s	          @@r   r   zIPythonDisplay.__call__h  s    )>%dnnb)))Bdnn,,9D9( :( Bk"o2T2( 3(	&$CiSTZZ\-=-=>F 	&%%	&s   6C CN)rx   r   r   r   enabledr   r   r   r   r   r   r   `  s    ;G"&r   r   c                 .   d }	 t               }|j                  r[ddlm} |j	                         }|j
                  }|j                         r|j                  |        y |j                   |               y 	 t        |        y # t        t        f$ r Y w xY w)Nr   )IOLoop)get_ipythonkerneltornado.ioloopr   currentasyncio_loop
is_runningadd_callbackrun_until_complete	NameErrorAttributeErrorr   )func
event_loopr   r   ioloops        r   ipython_async_executorr    s    J]99-^^%F,,J$$&##D)  --df5  4 ~& s   AB B BB_ipython_display_r   )r   r   r^   r;   r   r   param.displayr   param._utilsr   r>   rE   rF   r   r   r   r   r   r   r   r  r   r   r   <module>r     s   & 
     3 '  %  	~R ~RB U ( (
.(b &  &D$ -~ >r   