
    0PhJH                         d Z ddlZddlZddlmZ ddlmZ ddlmZ  G d d	e	          Z
 G d
 de
          Zd Z G d dej                  ZddZdS )ztThis module contains the _EstimatorPrettyPrinter class used in
BaseEstimator.__repr__ for pretty-printing estimators    N   )
get_config)BaseEstimator   )is_scalar_nanc                   "     e Zd ZdZ fdZ xZS )KeyValTuplez@Dummy class for correctly rendering key-value tuples from dicts.c                 D    t                                                      S )N)super__repr__)self	__class__s    U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/utils/_pprint.pyr   zKeyValTuple.__repr__P   s    ww!!!    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r	   r	   M   s>        JJ" " " " " " " " "r   r	   c                       e Zd ZdZdS )KeyValTupleParamzEDummy class for correctly rendering key-value tuples from parameters.N)r   r   r   r    r   r   r   r   U   s        OODr   r   c                     |                      d          }t          | j        d| j                  }t          j        |          j        d                                 D             fdfd|                                D             S )zgReturn dict (param_name: value) of parameters that were given to
    estimator with non-default values.Fdeepdeprecated_originalc                 $    i | ]\  }}||j         S r   )default).0nameparams      r   
<dictcomp>z#_changed_params.<locals>.<dictcomp>b   s     NNN;44NNNr   c                 6   | vrdS |          t           j        k    rdS t          |t                    r|j        |          j        k    rdS t          |          t          |                    k    r&t          |                    rt          |          sdS dS )NTF)inspect_empty
isinstancer   r   reprr   )kvinit_paramss     r   has_changedz$_changed_params.<locals>.has_changedd   s    K4q>W^++4a'' 	AK;q>;S,S,S477d;q>****+a.)) +.;A.>.> + 4ur   c                 4    i | ]\  }} ||          ||S r   r   )r    r)   r*   r,   s      r   r#   z#_changed_params.<locals>.<dictcomp>s   s0    AAATQ{{1a/@/@AAqAAAr   )
get_paramsgetattr__init__r%   	signature
parametersitems)	estimatorparams	init_funcr,   r+   s      @@r   _changed_paramsr7   [   s     !!u!--F	*,A9CUVVI#I..9KNN+:K:K:M:MNNNK     BAAAV\\^^AAAAr   c                        e Zd ZdZ	 	 	 	 ddddd fdZd	 Zd
 Zd Zd Zd Z	d Z
d Zej        j                                        Zeeej        <   eeej        <    xZS )_EstimatorPrettyPrintera
  Pretty Printer class for estimator objects.

    This extends the pprint.PrettyPrinter class, because:
    - we need estimators to be printed with their parameters, e.g.
      Estimator(param1=value1, ...) which is not supported by default.
    - the 'compact' parameter of PrettyPrinter is ignored for dicts, which
      may lead to very long representations that we want to avoid.

    Quick overview of pprint.PrettyPrinter (see also
    https://stackoverflow.com/questions/49565047/pprint-with-hex-numbers):

    - the entry point is the _format() method which calls format() (overridden
      here)
    - format() directly calls _safe_repr() for a first try at rendering the
      object
    - _safe_repr formats the whole object recursively, only calling itself,
      not caring about line length or anything
    - back to _format(), if the output string is too long, _format() then calls
      the appropriate _pprint_TYPE() method (e.g. _pprint_list()) depending on
      the type of the object. This where the line length and the compact
      parameters are taken into account.
    - those _pprint_TYPE() methods will internally use the format() method for
      rendering the nested objects of an object (e.g. the elements of a list)

    In the end, everything has to be implemented twice: in _safe_repr and in
    the custom _pprint_TYPE methods. Unfortunately PrettyPrinter is really not
    straightforward to extend (especially when we want a compact output), so
    the code is a bit convoluted.

    This class overrides:
    - format() to support the changed_only parameter
    - _safe_repr to support printing of estimators (for when they fit on a
      single line)
    - _format_dict_items so that dict are correctly 'compacted'
    - _format_items so that ellipsis is used on long lists and tuples

    When estimators cannot be printed on a single line, the builtin _format()
    will call _pprint_estimator() because it was registered to do so (see
    _dispatch[BaseEstimator.__repr__] = _pprint_estimator).

    both _format_dict_items() and _pprint_estimator() use the
    _format_params_or_dict_items() method that will format parameters and
    key-value pairs respecting the compact parameter. This method needs another
    subroutine _pprint_key_val_tuple() used when a parameter or a key-value
    pair is too long to fit on a single line. This subroutine is called in
    _format() and is registered as well in the _dispatch dict (just like
    _pprint_estimator). We had to create the two classes KeyValTuple and
    KeyValTupleParam for this.
    r   P   NFT)compactindent_at_namen_max_elements_to_showc                    t                                          |||||           || _        | j        rd| _        t	                      d         | _        || _        d S )N)r;   r   print_changed_only)r   r0   _indent_at_name_indent_per_levelr   _changed_onlyr=   )	r   indentwidthdepthstreamr;   r<   r=   r   s	           r   r0   z _EstimatorPrettyPrinter.__init__   se     	vwGGG- 	'%&D"'\\*>? '=###r   c                 4    t          ||||| j                  S )Nchanged_only)
_safe_reprrB   )r   objectcontext	maxlevelslevels        r   formatz_EstimatorPrettyPrinter.format   s'    GYD<N
 
 
 	
r   c                    |                     |j        j        dz              | j        r|t	          |j        j                  z  }| j        rt          |          }n|                    d          }|                     t          |
                                          |||dz   ||           |                     d           d S )N(Fr   r   ))writer   r   r@   lenrB   r7   r.   _format_paramssortedr3   )r   rK   rF   rC   	allowancerL   rN   r5   s           r   _pprint_estimatorz)_EstimatorPrettyPrinter._pprint_estimator   s    V%.4555 	5c&*3444F 	3$V,,FF&&E&22F6<<>>""FFIM7E	
 	
 	
 	Sr   c           	      :    |                      ||||||d          S )NTis_dict_format_params_or_dict_itemsr   r3   rF   rC   rW   rL   rN   s          r   _format_dict_itemsz*_EstimatorPrettyPrinter._format_dict_items   s.    00669gud 1 
 
 	
r   c           	      :    |                      ||||||d          S )NFrZ   r\   r^   s          r   rU   z&_EstimatorPrettyPrinter._format_params   s.    00669gue 1 
 
 	
r   c                 6   |j         }|| j        z  }dd|z  z   }	d}
| j        |z
  dz   x}}t          |          }	 t	          |          }n# t
          $ r Y dS w xY wd}d}|s6|| j        k    r |d           dS |dz  }|}	 t	          |          }n# t
          $ r d	}||z  }||z  }Y nw xY w| j        r|\  }}|                     |||          }|                     |||          }|s|	                    d
          }|rdnd}||z   |z   }t          |          dz   }||k     r|}|
r|	}
||k    r||z  } ||
           d}
 ||            ||
           |	}
|rt          nt          }|                      ||          |||r|nd||           |4dS dS )a-  Format dict items or parameters respecting the compact=True
        parameter. For some reason, the builtin rendering of dict items doesn't
        respect compact=True and will use one line per key-value if all cannot
        fit in a single line.
        Dict items will be rendered as <'key': value> while params will be
        rendered as <key=value>. The implementation is mostly copy/pasting from
        the builtin _format_items().
        This also adds ellipsis if the number of items is greater than
        self.n_max_elements_to_show.
        ,
  r   NFr   , ...T': =r   , )rS   rA   _widthiternextStopIterationr=   _compact_reprstriprT   r	   r   _format)r   rK   rF   rC   rW   rL   rN   r[   rS   delimnldelimrD   	max_widthitnext_entlastn_itemsentr)   r*   kreprvreprmiddlerepwclass_s                             r   r]   z4_EstimatorPrettyPrinter._format_params_or_dict_items   sQ    $((#,& K&0144	&\\	BxxHH 	 	 	FF	 $	$555gqLGC#88  # # #Y&	"# } 1

1gu55

1gu55 -!KK,,E!(1cfnu,HHqL199%E ( 'A::QJEE%LLL EE#JJJE%LLLE$+A[[1AFLLsVV$-EYYAwPU  E  $	 $	 $	 $	 $	s#   A 
AAB B,+B,c                    |j         }|| j        z  }| j        dk    r || j        dz
  dz             dd|z  z   }d}	| j        |z
  dz   x}
}t          |          }	 t	          |          }n# t
          $ r Y dS w xY wd}d}|s|| j        k    r |d           dS |dz  }|}	 t	          |          }n# t
          $ r d	}||z  }|
|z  }
Y nw xY w| j        rY|                     |||          }t          |          d
z   }|
|k     r|}
|	r|}	|
|k    r|
|z  }
 ||	           d}	 ||            ||	           |}	| 
                    ||||r|nd||           |dS dS )zFormat the items of an iterable (list, tuple...). Same as the
        built-in _format_items, with support for ellipsis if the number of
        elements is greater than self.n_max_elements_to_show.
        r   rc   rb   rd   NFr   re   Tr   ri   )rS   rA   rj   rk   rl   rm   r=   rn   ro   rT   rq   )r   r3   rF   rC   rW   rL   rN   rS   rr   rs   rD   rt   ru   rv   rw   rx   ry   r}   r~   s                      r   _format_itemsz%_EstimatorPrettyPrinter._format_items  s   
 $((!A%%E4)A-4555#,& K&0144	%[[	BxxHH 	 	 	FF	 	X$555gqLGC#88  # # #Y&	"# } jjgu55HHqL199%E ( 'A::QJEE%LLL EE#JJJE%LLLELLff4.FiiQQVWWW7  	X 	X 	X 	X 	Xs$   A, ,
A:9A:#B3 3CCc                 d   |\  }}|                      |||          }	t          |t                    r|	                    d          }	d}
nd}
|                    |	           |                    |
           |                     |||t          |	          z   t          |
          z   |||           dS )z=Pretty printing for key-value tuples from dict or parameters.rf   rh   rg   N)ro   r'   r   rp   rS   rq   rT   )r   rK   rF   rC   rW   rL   rN   r)   r*   r}   r|   s              r   _pprint_key_val_tuplez-_EstimatorPrettyPrinter._pprint_key_val_tupleK  s    1jjGU++f.// 	))C..CFFFSVvvC(3v;;6	7E	
 	
 	
 	
 	
r   )r   r:   NN)r   r   r   r   r0   rO   rX   r_   rU   r]   r   r   pprintPrettyPrinter	_dispatchcopyr   r   r	   r   r   s   @r   r9   r9   v   s       0 0h = #= = = = = = =*
 
 

  
 
 


 
 

= = =~.X .X .X`
 
 
& $.3355I(9Im$%&;Ik"#####r   r9   Fc                    t          |           }|t          j        v rt          |           ddfS t	          |dd          }t          |t                    r |t          j        u r| sdS t          |           }|r||k    rdd||v fS ||v rt          j	        |           ddfS d||<   d}d}	g }
|
j
        }|dz  }t          }t          |                                 t          j                  }|D ]I\  }} ||||||	          \  }}} ||||||	          \  }}} ||d
|           |o|o|}|s|rd}	J||= dd                    |
          z  ||	fS t          |t                     r|t           j        u s#t          |t"                    r|t"          j        u rt          |t                     r| sdS d}nt%          |           dk    rd}n| sdS d}t          |           }|r||k    r
|dz  d||v fS ||v rt          j	        |           ddfS d||<   d}d}	g }
|
j
        }|dz  }| D ]-}t          |||||	          \  }}} ||           |sd}|rd}	.||= |d                    |
          z  ||	fS t          |t&                    r9t          |           }|r||k    r|j         dd||v fS ||v rt          j	        |           ddfS d||<   d}d}	|rt+          |           }n|                     d          }g }
|
j
        }|dz  }t          }t          |                                t          j                  }|D ]\\  }} ||||||	          \  }}} ||||||	          \  }}} ||                    d          d|           |o|o|}|s|rd}	]||= |j        dd                    |
          d||	fS t          |           }||o|                    d           dfS )zMSame as the builtin _safe_repr, with added support for Estimator
    objects.TFr   N)z{}TFz{...}r   )keyrH   rg   z{%s}ri   )z[]TFz[%s]z(%s,))z()TFz(%s)z...z(...)r   rf   rh   rQ   rR   <)typer   _builtin_scalarsr(   r/   
issubclassdictr   id
_recursionappendrJ   rV   r3   _safe_tuplejoinlisttuplerT   r   r   r7   r.   rp   
startswith)rK   rL   rM   rN   rI   typrobjidreadable	recursive
componentsr   safereprr3   r)   r*   rz   	kreadablekrecurr{   	vreadablevrecurrO   oorepr	oreadableorecurr5   r}   s                                r   rJ   rJ   c  sc    v,,C
f%%%F||T5((Z&&A#t Cdm!3!3 	%$$6

 	4)++E5G#333G$V,,eT99	
"
v||~~6+=>>> 
	! 
	!DAq'/x7Iu<( ( ($E9f (0x7Iu<( ( ($E9f Fuuuee,---;I;)H ! ! 	EN		*---xBB3 "C!t}"4"43 #5#$#6#6c4   		 )((FF[[AFF )((F6

 	;)++E>5%7*:::G$V,,eT99	
"
 	! 	!A'17Iu<( ( ($E9f F5MMM !  ! 	EN		*---xBB#}%% W6

 	C)++l)))5%72BBBG$V,,eT99	 	3$V,,FF&&E&22F
"
v||~~6+=>>> 
	! 
	!DAq'/x7Iu<( ( ($E9f (0x7Iu<( ( ($E9f Fekk#....6777;I;)H ! ! 	ENLLL$))J*?*?*?*?@(IVV
v,,C0S^^C000588r   )F)r   r%   r   _configr   baser   _missingr   r   r	   r   r7   r   r9   rJ   r   r   r   <module>r      s  9 9H                           # # # # # #" " " " "% " " "	 	 	 	 	{ 	 	 	B B B6j< j< j< j< j<f2 j< j< j<Zl9 l9 l9 l9 l9 l9r   