3
Êb_žA  ã            	   @   s®  d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ eeeef ee f Zeee edœdd„Zd+ee eeee d
œdd„Zd,e	eee edœdd„Zd-eeee edœdd„Zd.eeee eeee edœdd„Zd/eeedœdd„Zeddœdd„Zd0dd„Zd1eeee eeed!œd"d#„Zee	e  ee	e  e
ee
ed$f  ee
ed$f  f d%œd&d'„Zd2e	eee
eeeef f  d(œd)d*„ZdS )3z
Printing tools.
é    N)	ÚAnyÚCallableÚIterableÚListÚMappingÚOptionalÚSequenceÚTupleÚUnion)Ú
get_option)Úis_sequence)ÚspaceÚlistsÚreturnc                sä   |j dtƒ‰|j dtƒ}g }g }‡ ‡fdd„|dd… D ƒ}|jttt|d ƒƒƒ ttt|ƒƒ}xPt|ƒD ]D\}}	||	|| dd�}
|
jd	||  g|t|	ƒ  ƒ |j|
ƒ qlW t|Ž }x|D ]}|jd
j	|ƒƒ qÂW dj	|ƒS )a»  
    Glues together two sets of strings using the amount of space requested.
    The idea is to prettify.

    ----------
    space : int
        number of spaces for padding
    lists : str
        list of str which being joined
    strlen : callable
        function used to calculate the length of each str. Needed for unicode
        handling.
    justfunc : callable
        function used to justify str. Needed for unicode handling.
    ÚstrlenÚjustfuncc                s   g | ]}t tˆ|ƒƒˆ  ‘qS © )ÚmaxÚmap)Ú.0Úx)r   r   r   ú</tmp/pip-build-jkwsxyiy/pandas/pandas/io/formats/printing.pyú
<listcomp>.   s    zadjoin.<locals>.<listcomp>Né   Úleft)Úmodeú Ú Ú
éÿÿÿÿr   )
ÚpopÚlenÚjustifyÚappendr   r   Ú	enumerateÚextendÚzipÚjoin)r   r   Úkwargsr   Z	out_linesZnewListsÚlengthsÚmaxLenÚiÚlstÚnlZtoJoinÚlinesr   )r   r   r   Úadjoin   s     
r/   Úright)ÚtextsÚmax_lenr   r   c                sJ   |dkr‡ fdd„| D ƒS |dkr4‡ fdd„| D ƒS ‡ fdd„| D ƒS dS )zB
    Perform ljust, center, rjust against string or list-like
    r   c                s   g | ]}|j ˆ ƒ‘qS r   )Úljust)r   r   )r2   r   r   r   A   s    zjustify.<locals>.<listcomp>Úcenterc                s   g | ]}|j ˆ ƒ‘qS r   )r4   )r   r   )r2   r   r   r   C   s    c                s   g | ]}|j ˆ ƒ‘qS r   )Úrjust)r   r   )r2   r   r   r   E   s    Nr   )r1   r2   r   r   )r2   r   r"   <   s
    r"   )ÚseqÚ	_nest_lvlÚmax_seq_itemsr   c                sÀ   t | tƒrd}nt| dƒrdnd}ˆdkr4t| ƒ}nˆpFtdƒpFt| ƒ}t| ƒ‰‡ ‡‡‡fdd„tt|t| ƒƒƒD ƒ}d	j|ƒ}|t| ƒk r–|d
7 }nt | t	ƒr´t| ƒdkr´|d7 }|j
|d�S )z¶
    internal. pprinter for iterables. you should probably use pprint_thing()
    rather then calling this directly.

    bounds length of printed sequence, depending on options
    z
{{{body}}}Ú__setitem__z[{body}]z({body})Fr8   c                s*   g | ]"}t tˆƒˆ d  fdˆiˆ—Ž‘qS )r   r8   )Úpprint_thingÚnext)r   r+   )r7   Úkwdsr8   Úsr   r   r   v   s   z_pprint_seq.<locals>.<listcomp>z, z, ...r   ú,)Úbody)Ú
isinstanceÚsetÚhasattrr!   r   ÚiterÚrangeÚminr'   ÚtupleÚformat)r6   r7   r8   r<   ÚfmtÚnitemsÚrr?   r   )r7   r<   r8   r=   r   Ú_pprint_seq`   s    	



rK   c       
      K   sÌ   d}g }d}|dkrt | ƒ}n|p0tdƒp0t | ƒ}x`t| jƒ ƒd|… D ]H\}}	|j|jt||d fd|i|—Žt|	|d fd|i|—Žd�ƒ qHW |t | ƒk r¶|jdj|ƒd	 d
�S |jdj|ƒd
�S dS )zy
    internal. pprinter for iterables. you should probably use pprint_thing()
    rather then calling this directly.
    z{{{things}}}z{key}: {val}Fr8   Nr   )ÚkeyÚvalz, z, ...)Zthings)r!   r   ÚlistÚitemsr#   rG   r:   r'   )
r6   r7   r8   r<   rH   ÚpairsZpfmtrI   ÚkÚvr   r   r   Ú_pprint_dictƒ   s    
$rS   F)Úthingr7   Úescape_charsÚdefault_escapesÚquote_stringsr8   r   c                s¬   |ft tt tdœ‡ fdd„}t| dƒr0t| ƒS t| tƒrX|tdƒk rXt| |d|d�}nPt	| ƒr€|tdƒk r€t
| ||||d�}n(t| tƒr |r d	j|| ƒd
�}n|| ƒ}|S )a  
    This function is the sanctioned way of converting objects
    to a string representation and properly handles nested sequences.

    Parameters
    ----------
    thing : anything to be formatted
    _nest_lvl : internal use only. pprint_thing() is mutually-recursive
        with pprint_sequence, this argument is used to keep track of the
        current nesting level, and limit it.
    escape_chars : list or dict, optional
        Characters to escape. If a dict is passed the values are the
        replacements
    default_escapes : bool, default False
        Whether the input escape characters replaces or adds to the defaults
    max_seq_items : int or None, default None
        Pass through to other pretty printers to limit sequence printing

    Returns
    -------
    str
    )rT   rU   r   c                sl   ddddœ}t |tƒr8ˆ r&|j|ƒ n|}t|jƒ ƒ}n
|p@tƒ }t| ƒ}x|D ]}|j||| ƒ}qPW |S )Nz\tz\nz\r)ú	r   ú)r@   ÚdictÚupdaterN   ÚkeysrF   ÚstrÚreplace)rT   rU   Ú	translateÚresultÚc)rV   r   r   Úas_escaped_stringÁ   s    


z'pprint_thing.<locals>.as_escaped_stringÚ__next__zdisplay.pprint_nest_depthT)rW   r8   )rU   rW   r8   z	'{thing}')rT   )r   r   ÚEscapeCharsr]   rB   r@   rZ   r   rS   r   rK   rG   )rT   r7   rU   rV   rW   r8   rb   r`   r   )rV   r   r:   ¢   s&     

r:   úutf-8r^   )ÚencodingÚerrorsr   c             C   s   t | ƒ}|j||ƒS )N)r:   Úencode)Úobjectrf   rg   Úvaluer   r   r   Úpprint_thing_encodedë   s    rk   )Úenabler   c             C   sŒ   dt jkrd S ddlm} |ƒ }|d kr,d S |jj}d}| rv||krjddlm} G dd„ d|ƒ}|ƒ ||< d|| _n||krˆd	|| _d S )
NÚIPythonr   )Úget_ipythonz!application/vnd.dataresource+json)ÚBaseFormatterc               @   s   e Zd ZdZefZdS )z=_enable_data_resource_formatter.<locals>.TableSchemaFormatterZ_repr_data_resource_N)Ú__name__Ú
__module__Ú__qualname__Zprint_methodrZ   Z_return_typer   r   r   r   ÚTableSchemaFormatter  s   rs   TF)	ÚsysÚmodulesrm   rn   Zdisplay_formatterÚ
formattersZIPython.core.formattersro   Zenabled)rl   rn   Úiprv   Úmimetypero   rs   r   r   r   Ú_enable_data_resource_formatterò   s     

ry   c             C   s   t | dd|d�S )NrX   rY   r   T)rU   rW   r8   )rX   rY   r   )r:   )r   r8   r   r   r   Ú<lambda>  s   rz   T)Ú	formatterÚ
is_justifyÚnameÚindent_for_nameÚline_break_each_valuer   c                s
  ddl m} ddlm} |ƒ \}}	|dkr6tdƒp4d}|dkrHt| ƒj}|rzt|ƒ}
dd|
d	  › �}dd|
d
  › �}nd}d}t| ƒ}|r ddt|ƒ  }nd}tdƒp®|}||k}|ƒ ‰ tttt	tt
ttf dœ‡ fdd„}tt t	dœ‡ fdd„}d}|dk�rd|› �}�nð|d	k�rH| �rHˆ| d ƒ}d|› d|› �}�n¾|d
k�rŒ| �rŒˆ| d ƒ}ˆ| d% ƒ}d|› d|› d|› �}�nz||k�rÜt|d
 dƒ}‡fdd„| d|… D ƒ}‡fdd„| | d… D ƒ}ng }‡fdd„| D ƒ}|�rt|�rt||ƒ\}}nf|�s>tdj|ƒƒ|k �o8tdj|ƒƒ|k  �rtt||ƒ||ƒƒ‰‡fdd„|D ƒ}‡fdd„|D ƒ}|�rð|t|ƒ }|d }x<ttd	t|ƒd	 ƒƒD ]"‰t|ˆd �}t|ƒ|k �r¦P �q¦W ‡fd!d„|D ƒ}‡fd"d„|D ƒ}d#}|}x:tt|ƒƒD ]*‰|ˆ | d }||||||ƒ\}}�qW |�rR||jƒ | d$ 7 }|}x>tt|ƒd	 ƒD ]*‰|ˆ | d }||||||ƒ\}}�qdW ||||d& |d
 |ƒ\}}||7 }d|jdƒ }||7 }t|ƒ|k�sà|�rê||7 }n|d7 }d|t|ƒd…  }|S )'a  
    Return the formatted obj as a unicode string

    Parameters
    ----------
    obj : object
        must be iterable and support __getitem__
    formatter : callable
        string formatter for an element
    is_justify : boolean
        should justify the display
    name : name, optional
        defaults to the class name of the obj
    indent_for_name : bool, default True
        Whether subsequent lines should be be indented to
        align with the name.
    line_break_each_value : bool, default False
        If True, inserts a line break for each value of ``obj``.
        If False, only break lines when the a line of values gets wider
        than the display width.

        .. versionadded:: 0.25.0

    Returns
    -------
    summary string
    r   )Úget_console_size)Ú_get_adjustmentNzdisplay.widthéP   r   r   r   é   z
 z,
 r>   zdisplay.max_seq_items)r=   Úlinerj   Údisplay_widthÚnext_line_prefixr   c                s@   ˆ j |jƒ ƒˆ j |jƒ ƒ |kr0| |jƒ 7 } |}||7 }| |fS )N)r!   Úrstrip)r=   r„   rj   r…   r†   )Úadjr   r   Ú_extend_line[  s
     z+format_object_summary.<locals>._extend_line)Úvaluesr   c                s"   | rt ‡ fdd„| D ƒƒS dS d S )Nc             3   s   | ]}ˆ j |ƒV  qd S )N)r!   )r   r   )rˆ   r   r   ú	<genexpr>g  s    z:format_object_summary.<locals>.best_len.<locals>.<genexpr>r   )r   )rŠ   )rˆ   r   r   Úbest_lene  s    z'format_object_summary.<locals>.best_lenz, z[]ú[ú]é
   c                s   g | ]}ˆ |ƒ‘qS r   r   )r   r   )r{   r   r   r   z  s    z)format_object_summary.<locals>.<listcomp>c                s   g | ]}ˆ |ƒ‘qS r   r   )r   r   )r{   r   r   r   {  s    c                s   g | ]}ˆ |ƒ‘qS r   r   )r   r   )r{   r   r   r   ~  s    c                s   g | ]}|j ˆ ƒ‘qS r   )r5   )r   r   )Ú
max_lengthr   r   r   �  s    c                s   g | ]}|j ˆ ƒ‘qS r   )r5   )r   r   )r�   r   r   r   Ž  s    )r8   c                s   g | ]}t |ˆ d �‘qS ))r8   )rK   )r   r   )Ú	max_itemsr   r   r   �  s    c                s   g | ]}t |ˆ d �‘qS ))r8   )rK   )r   r   )r‘   r   r   r   ž  s    r   z...r   r   )Zpandas.io.formats.consoler€   Zpandas.io.formats.formatr�   r   Útyperp   r!   r]   Úintr	   r   rE   Ú_justifyr'   r   ÚreversedrD   rK   r‡   )Úobjr{   r|   r}   r~   r   r€   r�   r…   Ú_Zname_lenZspace1Zspace2ÚnÚsepr8   Zis_truncatedr‰   rŒ   ÚcloseÚsummaryÚfirstÚlastÚheadÚtailZ	max_spacerj   Zpprinted_seqr„   Úwordr   )rˆ   r{   r‘   r�   r   Úformat_object_summary  sš    #





r¡   .)rž   rŸ   r   c                sv   | | }dgt |d ƒ ‰ x.|D ]&}dd„ |D ƒ}dd„ tˆ |ƒD ƒ‰ q W ‡ fdd„| D ƒ} ‡ fdd„|D ƒ}| |fS )aà  
    Justify items in head and tail, so they are right-aligned when stacked.

    Parameters
    ----------
    head : list-like of list-likes of strings
    tail : list-like of list-likes of strings

    Returns
    -------
    tuple of list of tuples of strings
        Same as head and tail, but items are right aligned when stacked
        vertically.

    Examples
    --------
    >>> _justify([['a', 'b']], [['abc', 'abcd']])
    ([('  a', '   b')], [('abc', 'abcd')])
    r   c             S   s   g | ]}t |ƒ‘qS r   )r!   )r   Úitemr   r   r   r   à  s    z_justify.<locals>.<listcomp>c             S   s   g | ]\}}t ||ƒ‘qS r   )r   )r   r   Úyr   r   r   r   á  s    c                s$   g | ]}t d d„ t|ˆ ƒD ƒƒ‘qS )c             s   s   | ]\}}|j |ƒV  qd S )N)r5   )r   r   r2   r   r   r   r‹   å  s    z&_justify.<locals>.<listcomp>.<genexpr>)rF   r&   )r   r6   )r�   r   r   r   å  s    c                s$   g | ]}t d d„ t|ˆ ƒD ƒƒ‘qS )c             s   s   | ]\}}|j |ƒV  qd S )N)r5   )r   r   r2   r   r   r   r‹   è  s    z&_justify.<locals>.<listcomp>.<genexpr>)rF   r&   )r   r6   )r�   r   r   r   è  s    )r!   r&   )rž   rŸ   ZcombinedZ	inner_seqÚlengthr   )r�   r   r”   Ä  s    
r”   )r–   Úinclude_dtyper   c             C   s°   g }t | dƒr*|r*|jdd| j› d�fƒ t| ddƒdk	rP|jdt| jƒfƒ n.t| ddƒdk	r~t| jƒr~|jdt| jƒfƒ tdƒpŒt	| ƒ}t	| ƒ|kr¬|jdt	| ƒfƒ |S )a:  
    Return a list of tuples of the (attr, formatted_value)
    for common attrs, including dtype, name, length

    Parameters
    ----------
    obj : object
        must be iterable
    include_dtype : bool
        If False, dtype won't be in the returned list

    Returns
    -------
    list of 2-tuple

    Údtypeú'r}   NÚnameszdisplay.max_seq_itemsr¤   )
rB   r#   r¦   ÚgetattrÚdefault_pprintr}   Úanyr¨   r   r!   )r–   r¥   Úattrsr8   r   r   r   Úformat_object_attrsñ  s    r­   )r0   )r   N)r   N)r   NFFN)re   r^   )N)TNTF)T) Ú__doc__rt   Útypingr   r   r   r   r   r   r   r	   r
   Zpandas._configr   Zpandas.core.dtypes.inferencer   r]   rd   r“   r/   r"   rK   rS   Úboolr:   Úbytesrk   ry   rª   r¡   r”   r­   r   r   r   r   Ú<module>   s>   ,#%"    C!
    '.,