
    {Kg\H                         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 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y)ztThis module contains the _EstimatorPrettyPrinter class used in
BaseEstimator.__repr__ for pretty-printing estimators    N)OrderedDict   )
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                      t         |          S N)super__repr__)self	__class__s    Y/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/sklearn/utils/_pprint.pyr   zKeyValTuple.__repr__N   s    w!!    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r
   r
   K   s    J" "r   r
   c                       e Zd ZdZy)KeyValTupleParamzEDummy class for correctly rendering key-value tuples from parameters.N)r   r   r   r    r   r   r   r   S   s    Or   r   c                    | j                  d      }t        | j                  d| j                        }t        j                  |      j
                  j                         D ci c]  \  }}||j                   c}}fd}|j                         D ci c]  \  }} |||      s|| c}}S c c}}w c c}}w )zgReturn dict (param_name: value) of parameters that were given to
    estimator with non-default values.Fdeepdeprecated_originalc                     | vry|    t         j                  k(  ryt        |t              r|j                  |    j                  k7  ryt        |      t        |          k7  rt        |          rt        |      syy)NTF)inspect_empty
isinstancer   r   reprr   )kvinit_paramss     r   has_changedz$_changed_params.<locals>.has_changedb   sq    Kq>W^^+a'AKK;q>;S;S,S7d;q>**+a.)mA.>r   )
get_paramsgetattr__init__r!   	signature
parametersitemsdefault)		estimatorparams	init_funcnameparamr(   r%   r&   r'   s	           @r   _changed_paramsr5   Y   s     !!u!-F	**,A9CUCUVI##I.99K:E:K:K:MN:M;44&:MNK $\\^A^TQ{1a/@AqD^AA# O" Bs   'B5B;,B;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                  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.
    NFT)compactindent_at_namen_max_elements_to_showc                    t         |   |||||       || _        | j                  rd| _        t	               d   | _        || _        y )N)r8   r   print_changed_only)r   r+   _indent_at_name_indent_per_levelr   _changed_onlyr:   )	r   indentwidthdepthstreamr8   r9   r:   r   s	           r   r+   z _EstimatorPrettyPrinter.__init__   sP     	vwG-%&D"'\*>? '=#r   c                 6    t        ||||| j                        S )Nchanged_only)
_safe_reprr?   )r   objectcontext	maxlevelslevels        r   formatz_EstimatorPrettyPrinter.format   s     GYD<N<N
 	
r   c                    |j                  |j                  j                  dz          | j                  r"|t	        |j                  j                        z  }| j
                  rt        |      }n|j                  d      }t        d t        |j                               D              }| j                  |j                         |||dz   ||       |j                  d       y )N(Fr   c              3   *   K   | ]  \  }}||f  y wr   r   ).0r3   vals      r   	<genexpr>z<_EstimatorPrettyPrinter._pprint_estimator.<locals>.<genexpr>   s     S<R[dCdC[<Rs   r   ))writer   r   r=   lenr?   r5   r)   r   sortedr.   _format_params)r   rH   rC   r@   	allowancerI   rK   r1   s           r   _pprint_estimatorz)_EstimatorPrettyPrinter._pprint_estimator   s    V%%..45c&**3344F$V,F&&E&2FSF6<<><RSSLLNFFIM7E	
 	Sr   c           	      2    | j                  ||||||d      S )NTis_dict_format_params_or_dict_itemsr   r.   rC   r@   rX   rI   rK   s          r   _format_dict_itemsz*_EstimatorPrettyPrinter._format_dict_items   s)    00669gud 1 
 	
r   c           	      2    | j                  ||||||d      S )NFr[   r]   r_   s          r   rW   z&_EstimatorPrettyPrinter._format_params   s)    00669gue 1 
 	
r   c                    |j                   }|| j                  z  }dd|z  z   }	d}
| j                  |z
  dz   x}}t        |      }	 t	        |      }d}d}|s|| j                  k(  r	 |d       y|dz  }|}	 t	        |      }| j                  r|\  }}| j                  |||      }| j                  |||      }|s|j                  d
      }|rdnd}||z   |z   }t        |      dz   }||k  r|}|
r|	}
||k\  r||z  } ||
       d}
 ||        ||
       |	}
|rt        nt        }| j                   ||      |||r|nd||       |syy# t
        $ r Y yw xY w# t
        $ r d	}||z  }||z  }Y w xY w)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   , )rT   r>   _widthiternextStopIterationr:   _compact_reprstriprU   r
   r   _format)r   rH   rC   r@   rX   rI   rK   r\   rT   delimnldelimrA   	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   s    $(((#,& KK&0144	&\	BxH $555gqLGC#8
 }}1

1gu5

1gu5!KK,E!(cfnu,HqL19%E 'A:QJE%L E#J%LE$+[1AFLLsVV$YAwPUE 	  		 ! #Y&	"#s$   E 7E 	EEE43E4c                    |j                   }|| j                  z  }| j                  dkD  r || j                  dz
  dz         dd|z  z   }d}	| j                  |z
  dz   x}
}t        |      }	 t	        |      }d}d}|s|| j                  k(  r	 |d       y|dz  }|}	 t	        |      }| j                  rI| j                  |||      }t        |      d
z   }|
|k  r|}
|	r|}	|
|k\  r|
|z  }
 ||	       d}	 ||        ||	       |}	| j                  ||||r|nd||       |syy# t
        $ r Y yw xY w# t
        $ r d	}||z  }|
|z  }
Y w xY w)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   rd   rc   re   NFr   rf   Tr   rj   )rT   r>   rk   rl   rm   rn   r:   ro   rp   rU   rr   )r   r.   rC   r@   rX   rI   rK   rT   rs   rt   rA   ru   rv   rw   rx   ry   rz   r~   r   s                      r   _format_itemsz%_EstimatorPrettyPrinter._format_items  s   
 $(((!!A%4))A-45#,& KK&0144	%[	BxH $555gqLGC#8
 }}jjgu5HqL19%E 'A:QJE%L E#J%LELLff4iQQVW7 	  		 ! #Y&	"#s$   -D' D6 '	D32D36EEc                     |\  }}| j                  |||      }	t        |t              r|	j                  d      }	d}
nd}
|j	                  |	       |j	                  |
       | j                  |||t        |	      z   t        |
      z   |||       y)z=Pretty printing for key-value tuples from dict or parameters.rg   ri   rh   N)rp   r#   r   rq   rT   rr   rU   )r   rH   rC   r@   rX   rI   rK   r%   r&   r~   r}   s              r   _pprint_key_val_tuplez-_EstimatorPrettyPrinter._pprint_key_val_tupleK  s    1jjGU+f./))C.CFFSVvvC(3v;6	7E	
r   )r   P   NN)r   r   r   r   r+   rL   rY   r`   rW   r^   r   r   pprintPrettyPrinter	_dispatchcopyr   r   r
   r   r   s   @r   r7   r7   t   s    0h = #=*

"



=~.X`
& $$..335I(9Im$$%&;Ik""#r   r7   c                 B   t        |       }|t        j                  v rt        |       ddfS t	        |dd      }t        |t              r|t        j                  u r| sy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        | j                         t        j                        }|D ]E  \  }} ||||||	      \  }}} ||||||	      \  }}} ||d
|       |xr |xr |}|s|sDd}	G ||= ddj                  |
      z  ||	fS t        |t               r|t         j                  u s"t        |t"              r|t"        j                  u rt        |t               r| syd}nt%        |       dk(  rd}n| sy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}|s&d}	) ||= |dj                  |
      z  ||	fS t        |t&              r t        |       }|r||k\  rdd||v fS ||v rt        j                  |       ddfS d||<   d}d}	|rt)        |       }n| j+                  d      }g }
|
j                  }|dz  }t        }t        |j                         t        j                        }|D ]T  \  }} ||||||	      \  }}} ||||||	      \  }}} ||j-                  d      d|       |xr |xr |}|s|sSd}	V ||= |j.                  ddj                  |
      d||	fS t        |       }||xr |j1                  d       dfS )zMSame as the builtin _safe_repr, with added support for Estimator
    objects.TFr   N)z{}TFz{...}r   )keyrE   rh   z{%s}rj   )z[]TFz[%s]z(%s,))z()TFz(%s)z...r   rg   ri   rN   rS   <)typer   _builtin_scalarsr$   r*   
issubclassdictr   id
_recursionappendrG   rV   r.   _safe_tuplejoinlisttuplerU   r   r5   r)   rq   r   
startswith)rH   rI   rJ   rK   rF   typrobjidreadable	recursive
componentsr   safereprr.   r%   r&   r{   	kreadablekrecurr|   	vreadablevrecurrL   oorepr	oreadableorecurr1   r~   s                                r   rG   rG   c  s*    v,C
f%%%F|T5((Z&A#tdmm!3$6
)+E5G#333G$$V,eT99	
""
v||~6+=+=>DAq'/7Iu<($E9f (07Iu<($E9f ue,-;I;)H 	  EN		*--xBB3!t}}"431#6c4 (F[AF(F6
)+E>5%7*:::G$$V,eT99	
""
A'17Iu<($E9f 5M  	  EN		*--xBB#}%6
)+E5G#333G$$V,eT99	$V,F&&E&2F
""
v||~6+=+=>DAq'/7Iu<($E9f (07Iu<($E9f ekk#.67;I;)H 	  ENLL$))J*?@(IVV
v,C0S^^C00588r   )F)r   r!   r   collectionsr   _configr   baser   _missingr   r   r
   r   r5   r   r7   rG   r   r   r   <module>r      sX   9B   #     #"% "	{ 	B6l<f22 l<^l9r   