B
    0dTH                 @   s   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
 d
eZG dd deZdd ZG dd dejZdddZdS )ztThis module contains the _EstimatorPrettyPrinter class used in
BaseEstimator.__repr__ for pretty-printing estimators    N)OrderedDict   )BaseEstimator)
get_config   )is_scalar_nanc                   s    e Zd ZdZ fddZ  ZS )KeyValTuplez@Dummy class for correctly rendering key-value tuples from dicts.c                s
   t   S )N)super__repr__)self)	__class__ G/var/www/html/venv/lib/python3.7/site-packages/sklearn/utils/_pprint.pyr
   N   s    zKeyValTuple.__repr__)__name__
__module____qualname____doc__r
   __classcell__r   r   )r   r   r   K   s   r   c               @   s   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   r   S   s   r   c                s\   | j dd}t| jd| j}t|jdd  D fdd  fdd| D S )	zgReturn dict (param_name: value) of parameters that were given to
    estimator with non-default values.F)deepZdeprecated_originalc             S   s   i | ]\}}|j |qS r   )default).0nameparamr   r   r   
<dictcomp>`   s    z#_changed_params.<locals>.<dictcomp>c                sl   |  krdS  |  t jkrdS t|tr<|j |  jkr<dS t|t |  krht |  rdt|shdS dS )NTF)inspect_empty
isinstancer   r   reprr   )kv)init_paramsr   r   has_changedb   s    z$_changed_params.<locals>.has_changedc                s    i | ]\}} ||r||qS r   r   )r   r   r    )r"   r   r   r   q   s    )
get_paramsgetattr__init__r   	signature
parametersitems)Z	estimatorparamsZ	init_funcr   )r"   r!   r   _changed_paramsY   s    r*   c                   s   e Zd ZdZddddd fdd	Zd
d Zdd Zdd Zdd Zdd Z	dd Z
dd Zejj Zeeej< eeej<   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               s>   t  j|||||d || _| jr(d| _t d | _|| _d S )N)r-   r   Zprint_changed_only)r	   r%   _indent_at_name_indent_per_levelr   _changed_onlyr/   )r   indentwidthdepthstreamr-   r.   r/   )r   r   r   r%      s    z _EstimatorPrettyPrinter.__init__c             C   s   t ||||| jdS )N)changed_only)
_safe_reprr2   )r   objectcontext	maxlevelslevelr   r   r   format   s    z_EstimatorPrettyPrinter.formatc             C   s   | |jjd  | jr(|t|jj7 }| jr8t|}n|jdd}tdd t	|
 D }| |
 |||d || | d d S )N(F)r   c             s   s   | ]\}}||fV  qd S )Nr   )r   r   valr   r   r   	<genexpr>   s    z<_EstimatorPrettyPrinter._pprint_estimator.<locals>.<genexpr>r   ))writer   r   r0   lenr2   r*   r#   r   sortedr(   _format_params)r   r9   r6   r3   	allowancer:   r<   r)   r   r   r   _pprint_estimator   s    
z)_EstimatorPrettyPrinter._pprint_estimatorc          	   C   s   | j ||||||ddS )NT)is_dict)_format_params_or_dict_items)r   r(   r6   r3   rF   r:   r<   r   r   r   _format_dict_items   s    z*_EstimatorPrettyPrinter._format_dict_itemsc          	   C   s   | j ||||||ddS )NF)rH   )rI   )r   r(   r6   r3   rF   r:   r<   r   r   r   rE      s    z&_EstimatorPrettyPrinter._format_paramsc             C   s  |j }|| j7 }dd|  }	d}
| j| d  }}t|}yt|}W n tk
rZ   dS X d}d}x>|s|| jkr|d P |d7 }|}yt|}W n( tk
r   d	}||8 }||8 }Y nX | jrf|\}}| |||}| |||}|s |	d
}|r
dnd}|| | }t
|d }||k r>|}|
r>|	}
||krf||8 }||
 d}
|| qh||
 |	}
|r|tnt}| |||||r|nd|| qhW 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.
        z,
  r   NFr   z, ...T'z: =r   z, )rB   r1   _widthiternextStopIterationr/   _compact_reprstriprC   r   r   _format)r   r9   r6   r3   rF   r:   r<   rH   rB   delimnldelimr4   	max_widthitnext_entlastn_itemsentr   r    kreprvreprmiddlerepwclass_r   r   r   rI      s`    





z4_EstimatorPrettyPrinter._format_params_or_dict_itemsc             C   sr  |j }|| j7 }| jdkr,|| jd d  dd|  }d}	| j| d  }
}t|}yt|}W n tk
rv   dS X d}d}x|sl|| jkr|d P |d7 }|}yt|}W n( tk
r   d	}||8 }|
|8 }
Y nX | jr@| |||}t	|d
 }|
|k r|}
|	r|}	|
|kr@|
|8 }
||	 d}	|| q||	 |}	| 
||||r`|nd|| qW 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   rK   z,
rL   NFr   z, ...Tr   z, )rB   r1   rO   rP   rQ   rR   r/   rS   rT   rC   rV   )r   r(   r6   r3   rF   r:   r<   rB   rW   rX   r4   rY   rZ   r[   r\   r]   r^   rb   rc   r   r   r   _format_items  sT    




z%_EstimatorPrettyPrinter._format_itemsc             C   sp   |\}}|  |||}	t|tr0|	d}	d}
nd}
||	 ||
 | |||t|	 t|
 ||| dS )z=Pretty printing for key-value tuples from dict or parameters.rM   rN   z: N)rT   r   r   rU   rB   rV   rC   )r   r9   r6   r3   rF   r:   r<   r   r    rb   ra   r   r   r   _pprint_key_val_tupleK  s    



z-_EstimatorPrettyPrinter._pprint_key_val_tuple)r   r,   NN)r   r   r   r   r%   r=   rG   rJ   rE   rI   re   rf   pprintPrettyPrinter	_dispatchcopyr   r
   r   r   r   r   )r   r   r+   t   s$   1   ?0
r+   Fc             C   s  t | }|tjkr t| ddfS t|dd}t|trH|tjkrH| sLdS t| }|rn||krndd||kfS ||krt	| ddfS d||< d}d}	g }
|
j
}|d7 }t}t|  tjd}xn|D ]f\}}||||||d	\}}}||||||d	\}}}|d
||f  |o|o|}|s&|rd}	qW ||= dd|
 ||	fS t|tr`|tjksxt|trz|tjkrzt|tr| sdS d}n"t| dkrd}n| sdS d}t| }|r||kr|d d||kfS ||krt	| ddfS d||< d}d}	g }
|
j
}|d7 }xB| D ]:}t|||||d	\}}}|| |sPd}|r"d}	q"W ||= |d|
 ||	fS t|trt| }|r||krdd||kfS ||krt	| ddfS d||< d}d}	|rt| }n| jdd}g }
|
j
}|d7 }t}t| tjd}xx|D ]p\}}||||||d	\}}}||||||d	\}}}|d|d|f  |o||o||}|s|r d}	q W ||= d|jd|
f ||	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   )key)r7   z%s: %sz{%s}z, )z[]TFz[%s]z(%s,))z()TFz(%s)z...)r   z%s=%srM   z%s(%s)<)typerg   _builtin_scalarsr   r$   
issubclassdictr
   id
_recursionappendr8   rD   r(   _safe_tuplejoinlisttuplerC   r   r*   r#   rU   r   
startswith)r9   r:   r;   r<   r7   typrobjidreadable	recursive
componentsrs   safereprr(   r   r    r_   	kreadablekrecurr`   	vreadablevrecurr=   oorepr	oreadableorecurr)   rb   r   r   r   r8   c  s    







r8   )F)r   r   rg   collectionsr   baser   _configr   rL   r   rw   r   r   r*   rh   r+   r8   r   r   r   r   <module>   s   @ p