U
    f/efC                  	   @  s  d Z ddlm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dZedZd	d
ddddZdBdd	dd
dddZdCdd	dddddZdDdd	dddddZdEdd	dddddd d!d"ZdFddd%d&d'd(Zdd)d*d+d,ZdGdddd-d.d/ZdHd1dd2dddd3d4d5Zd6d6d7d8d9d:ZdId;dd<d=d>d?Z G d@dA dAeeef Z!dS )Jz
Printing tools.
    )annotationsN)	AnyCallableDictIterableMappingSequenceSizedTypeVarUnion)
get_option)is_sequence_KT_VTint	list[str]str)spacelistsreturnc                   s   | dt| dt}g }g } fdd|dd D }|ttt|d  ttt|}t|D ]D\}}	||	|| dd}
|
d	||  g|t|	   ||
 qjt| }|D ]}|d
	| qd	|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-unpacked-wheel-tiezk1ph/pandas/io/formats/printing.py
<listcomp>1   s     zadjoin.<locals>.<listcomp>Nleft)mode  
)
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   adjoin   s     r6   rightzIterable[str])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 | ]}|  qS r   )ljustr   r9   r   r   r    D   s     zjustify.<locals>.<listcomp>centerc                   s   g | ]}|  qS r   )r<   r   r;   r   r   r    F   s     c                   s   g | ]}|  qS r   rjustr   r;   r   r   r    H   s     Nr   )r8   r9   r#   r   r;   r   r)   ?   s
    r)   r   z
int | None)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	|}|t| k r|d
7 }nt | t	rt| dkr|d7 }|j
|dS )z
    internal. pprinter for iterables. you should probably use pprint_thing()
    rather than calling this directly.

    bounds length of printed sequence, depending on options
    z
{{{body}}}__setitem__z[{body}]z({body})FrA   c                   s*   g | ]"}t t d  fdiqS )   rA   )pprint_thingnext)r   r2   r@   kwdsrA   sr   r   r    x   s   z_pprint_seq.<locals>.<listcomp>, , ...rC   ,)body)
isinstancesethasattrr(   r   iterrangeminr.   tupleformat)r?   r@   rA   rG   fmtnitemsrrL   r   rF   r   _pprint_seqc   s     	



rX   r   c           
   	   K  s   d}g }d}|dkrt | }n|p0tdp0t | }t|  d| D ]H\}}	||jt||d fd|i|t|	|d fd|i|d qF|t | k r|jd|d	 d
S |jd|d
S dS )zy
    internal. pprinter for iterables. you should probably use pprint_thing()
    rather than calling this directly.
    z{{{things}}}z{key}: {val}FrA   NrC   )keyvalrI   rJ   )Zthings)r(   r   listitemsr*   rT   rD   r.   )
r?   r@   rA   rG   rU   pairsZpfmtrV   kvr   r   r   _pprint_dict   s     
r`   Fr   EscapeChars | Nonebool)thingr@   escape_charsdefault_escapesquote_stringsrA   r   c                   s   |fdddd fdd}t | dr,t| S t| trT|tdk rTt| |d	|d
}nPt| r||tdk r|t| ||||d}n(t| tr|rd||  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
    r   ra   r   )rc   rd   r   c                   sf   dddd}t |tr8 r&|| n|}t| }n|p>d}t| }|D ]}|||| }qL|S )Nz\tz\nz\r)	r&   r   )rM   dictupdater[   keysr   replace)rc   rd   	translateresultcre   r   r   as_escaped_string   s    
z'pprint_thing.<locals>.as_escaped_string__next__zdisplay.pprint_nest_depthT)rf   rA   rd   rf   rA   ')rO   r   rM   ri   r   r`   r   rX   )rc   r@   rd   re   rf   rA   rq   rn   r   rp   r   rD      s2     
   rD   utf-8rl   bytes)encodingerrorsr   c                 C  s   t | }|||S N)rD   encode)objectrw   rx   valuer   r   r   pprint_thing_encoded   s    r}   None)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rd	|| _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_methodri   Z_return_typer   r   r   r   TableSchemaFormatter  s   r   TF)	sysmodulesr   r   Zdisplay_formatter
formattersZIPython.core.formattersr   Zenabled)r   r   ipr   mimetyper   r   r   r   r   enable_data_resource_formatter   s     

r   )rc   rA   r   c                 C  s   t | dd|dS )N)rg   rh   r&   Trs   rD   )rc   rA   r   r   r   default_pprint  s    r   Tr   z
str | None)	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| }|rddt|  }nd}tdp|}||k}|  ddddddd fdd}ddd fdd}d}|dkr
d| }n|d	kr:|s:| d }d| d| }n|d
kr||s|| d }| d }d| d| d| }n|d	krg }fdd| dd D }nf||krt|d
 d }fd!d| d| D }fd"d| | d D }ng }fd#d| D }|r|r(t	||\}}nd|sVtd
||k rVtd
||k st||||fd$d|D }fd%d|D }|r|t| }|d }ttd	t|d	 D ]&t|d&}t||k r qqfd'd|D }fd(d|D }d)}|}tt|D ]*| | d }||||||\}}q|rf|| | d* 7 }|}tt|d	 D ]*| | d }||||||\}}qv||||d |d
 |\}}||7 }d|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 : bool
        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 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.widthP   r&   r$   rC      z
 z,
 rK   display.max_seq_itemsr   r   ztuple[str, str])rH   liner|   display_widthnext_line_prefixr   c                   s@     |   |  |kr0| | 7 } |}||7 }| |fS ry   )r(   rstrip)rH   r   r|   r   r   adjr   r   _extend_lineb  s
     z+format_object_summary.<locals>._extend_liner   )valuesr   c                   s"   | rt  fdd| D S dS d S )Nc                 3  s   | ]}  |V  qd S ry   r(   r   r   r   r   	<genexpr>n  s     z:format_object_summary.<locals>.best_len.<locals>.<genexpr>r   r   )r   r   r   r   best_lenl  s    z'format_object_summary.<locals>.best_lenrI   z[][]r!   c                   s   g | ]} |qS r   r   r   r   r   r   r      s     z)format_object_summary.<locals>.<listcomp>
   c                   s   g | ]} |qS r   r   r   r   r   r   r      s     c                   s   g | ]} |qS r   r   r   r   r   r   r      s     c                   s   g | ]} |qS r   r   r   r   r   r   r      s     c                   s   g | ]}|  qS r   r=   r   
max_lengthr   r   r      s     c                   s   g | ]}|  qS r   r=   r   r   r   r   r      s     rA   c                   s   g | ]}t | d qS r   rX   r   	max_itemsr   r   r      s     c                   s   g | ]}t | d qS r   r   r   r   r   r   r      s     r%   z...)Zpandas.io.formats.consoler   Zpandas.io.formats.formatr   r   typer   r(   rR   _justifyr.   r   reversedrQ   rX   r   )objr   r   r   r   r   r   r   r   _Zname_lenZspace1Zspace2nseprA   Zis_truncatedr   r   closesummaryfirstlastheadtailZ	max_spacer|   Zpprinted_seqr   wordr   )r   r   r   r   r   format_object_summary  s    #







r   zlist[Sequence[str]]z3tuple[list[tuple[str, ...]], list[tuple[str, ...]]])r   r   r   c                   sr   | | }dgt |d   |D ]&}dd |D }dd t |D  q 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   | ]\}}| |V  qd S ry   r=   r   r   r9   r   r   r   r     s     &_justify.<locals>.<listcomp>.<genexpr>rS   r-   r   r?   r   r   r   r      s    c                   s$   g | ]}t d d t| D qS )c                 s  s   | ]\}}| |V  qd S ry   r=   r   r   r   r   r     s     r   r   r   r   r   r   r      s    )r(   r-   )r   r   ZcombinedZ	inner_seqlengthr   r   r   r     s    

r   r	   zlist[tuple[str, str | int]])r   include_dtyper   c                 C  s   g }t | dr*|r*|dd| j df t| dddk	rP|dt| jf n.t| dddk	r~t| jr~|dt| jf tdpt	| }t	| |kr|dt	| f |S )a8  
    Return a list of tuples of the (attr, formatted_value)
    for common attrs, including dtype, name, length

    Parameters
    ----------
    obj : object
        Must be sized.
    include_dtype : bool
        If False, dtype won't be in the returned list

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

    dtypert   r   Nnamesr   r   )
rO   r*   r   getattrr   r   anyr   r   r(   )r   r   attrsrA   r   r   r   format_object_attrs  s    r   c                   @  s   e Zd ZdZddddZdS )
PrettyDictz.Dict extension to support abbreviated __repr__r   )r   c                 C  s   t | S ry   r   )selfr   r   r   __repr__%  s    zPrettyDict.__repr__N)r   r   r   __doc__r   r   r   r   r   r   "  s   r   )r7   )r   N)r   N)r   NFFN)ru   rl   )N)TNTF)T)"r   
__future__r   r   typingr   r   r   r   r   r   r	   r
   r   Zpandas._configr   Zpandas.core.dtypes.inferencer   r   ZEscapeCharsr   r   r6   r)   rX   r`   rD   r}   r   r   r   r   r   r   r   r   r   r   <module>   sJ   ,#%   $   !     J   !     1. &