U
    adS                  
   @   s  d Z ddlZddlZddlZddlZddlm	Z
 dddddd	d
gZd%dddddZd&dddddZdddd
Zdd Zdd Zdd ZG dd dZdd ZG dd	 d	Zdd ZeeeeeeeeedhZdd Z d'd d!Z!d"d# Z"e#d$k re!  dS )(a/  Support to pretty-print lists, tuples, & dictionaries recursively.

Very simple, but useful, especially in debugging data structures.

Classes
-------

PrettyPrinter()
    Handle pretty-printing operations onto a stream using a configured
    set of formatting parameters.

Functions
---------

pformat()
    Format a Python object into a pretty-printed representation.

pprint()
    Pretty-print a Python object to a stream [default is sys.stdout].

saferepr()
    Generate a 'standard' repr()-like value, but protect against recursive
    data structures.

    N)StringIOpprintpformat
isreadableisrecursivesafereprPrettyPrinterpp   P   FTcompact
sort_dictsc                C   s"   t ||||||d}||  dS )zAPretty-print a Python object to a stream [default is sys.stdout].)streamindentwidthdepthr   r   N)r   r   )objectr   r   r   r   r   r   Zprinter r   /usr/lib/python3.8/pprint.pyr   /   s        c                C   s   t |||||d| S )z<Format a Python object into a pretty-printed representation.)r   r   r   r   r   )r   r   )r   r   r   r   r   r   r   r   r   r   7   s     )r   c                O   s   t | f|d|i| dS )zPretty-print a Python objectr   N)r   )r   r   argskwargsr   r   r   r	   =   s    c                 C   s   t | i dddd S )z=Version of repr() which can handle recursive data structures.Nr   T
_safe_reprr   r   r   r   r   A   s    c                 C   s   t | i dddd S )z4Determine if saferepr(object) is readable by eval().Nr   Tr
   r   r   r   r   r   r   E   s    c                 C   s   t | i dddd S )z8Determine if object requires a recursive representation.Nr   T   r   r   r   r   r   r   I   s    c                   @   s&   e Zd ZdZdgZdd Zdd ZdS )	_safe_keyaU  Helper function for key functions when sorting unorderable objects.

    The wrapped-object will fallback to a Py2.x style comparison for
    unorderable types (sorting first comparing the type name and then by
    the obj ids).  Does not work recursively, so dict.items() must have
    _safe_key applied to both the key and the value.

    objc                 C   s
   || _ d S N)r   )selfr   r   r   r   __init__Y   s    z_safe_key.__init__c                 C   sX   z| j |j k W S  tk
rR   tt| j t| j ftt|j t|j fk  Y S X d S r   )r   	TypeErrorstrtypeid)r   otherr   r   r   __lt__\   s    z_safe_key.__lt__N)__name__
__module____qualname____doc__	__slots__r    r&   r   r   r   r   r   M   s   	r   c                 C   s   t | d t | d fS )z&Helper function for comparing 2-tuplesr   r
   )r   )tr   r   r   _safe_tuplec   s    r-   c                   @   s  e Zd Zd;dddddZd	d
 Zdd Zdd Zdd Zdd Zi Z	dd Z
e
e	ej< dd Zee	ejj< dd Zee	ej< dd Zee	ej< dd Zee	ej< ee	ej< dd Zee	ej< dd  Zee	ej< d!d" Zee	ej< d#d$ Zee	ejj< d%d& Z d'd( Z!d)d* Z"d+d, Z#d-d. Z$e$e	ej%j< d/d0 Z&e&e	ej'j< d1d2 Z(e(e	ej)j< d3d4 Z*e*e	ej+j< d5d6 Z,e,e	ej-j< d7d8 Z.e.e	ej/j< d9d: Z0e0e	ej1j< dS )<r   r
   r   NFTr   c                C   s   t |}t |}|dk r td|dk	r8|dkr8td|sDtd|| _|| _|| _|dk	rf|| _ntj| _t|| _	|| _
dS )a  Handle pretty printing operations onto a stream using a set of
        configured parameters.

        indent
            Number of spaces to indent for each level of nesting.

        width
            Attempted maximum number of columns in the output.

        depth
            The maximum depth to print out nested structures.

        stream
            The desired output stream.  If omitted (or false), the standard
            output stream available at construction will be used.

        compact
            If true, several items will be combined in one line.

        sort_dicts
            If true, dict keys are sorted.

        r   zindent must be >= 0Nzdepth must be > 0zwidth must be != 0)int
ValueError_depth_indent_per_level_width_stream_sysstdoutbool_compact_sort_dicts)r   r   r   r   r   r   r   r   r   r   r    h   s     
zPrettyPrinter.__init__c                 C   s&   |  || jddi d | jd d S )Nr   
)_formatr3   writer   r   r   r   r   r      s    zPrettyPrinter.pprintc                 C   s"   t  }| ||ddi d | S Nr   )	_StringIOr:   getvalue)r   r   Zsior   r   r   r      s    zPrettyPrinter.pformatc                 C   s   |  |i ddd S )Nr   r   formatr<   r   r   r   r      s    zPrettyPrinter.isrecursivec                 C   s    |  |i dd\}}}|o| S r=   r@   )r   r   sreadable	recursiver   r   r   r      s    zPrettyPrinter.isreadablec              	   C   s   t |}||kr.|t| d| _d| _d S | |||}| j| | }	t||	kr| j	t
|jd }
|
d k	rd||< |
| ||||||d  ||= d S t|trd||< | ||||||d  ||= d S || d S )NTFr
   )r$   r;   
_recursion
_recursive	_readable_reprr2   len	_dispatchgetr#   __repr__
isinstancedict_pprint_dict)r   r   r   r   	allowancecontextlevelobjidrep	max_widthpr   r   r   r:      s0    
 zPrettyPrinter._formatc           
      C   sz   |j }|d | jdkr*|| jd d  t|}|rn| jrNt| td}	n| }	| |	|||d || |d d S )N{r
    key})r;   r1   rI   r8   sorteditemsr-   _format_dict_items)
r   r   r   r   rP   rQ   rR   r;   Zlengthr]   r   r   r   rO      s    
 zPrettyPrinter._pprint_dictc                 C   sl   t |s|t| d S |j}||jd  | t| ||t |j d |d || |d d S )N(r
   ))rI   r;   repr	__class__r'   r:   listr]   )r   r   r   r   rP   rQ   rR   clsr   r   r   _pprint_ordered_dict   s      z"PrettyPrinter._pprint_ordered_dictc                 C   s0   | d | ||||d || | d d S )N[r
   ])r;   _format_itemsr   r   r   r   rP   rQ   rR   r   r   r   _pprint_list   s    
 zPrettyPrinter._pprint_listc                 C   sH   | d t|dkrdnd}| ||||t| || | | d S )Nr_   r
   z,)r`   )r;   rI   rh   )r   r   r   r   rP   rQ   rR   endcharr   r   r   _pprint_tuple   s    
 zPrettyPrinter._pprint_tuplec           	      C   s   t |s|t| d S |j}|tkr8|d d}n&||jd  d}|t |jd 7 }t|td}| ||||t | || || d S )NrW   r[   ({})r
   rY   )	rI   r;   ra   rb   setr'   r\   r   rh   )	r   r   r   r   rP   rQ   rR   typrk   r   r   r   _pprint_set   s     
 zPrettyPrinter._pprint_setc                 C   s  |j }t|s|t| d S g }|d}	|dkrD|d7 }|d7 }| j|  }
}t|	D ]\}}t|}|t|	d kr|
|8 }
t||
kr|| qZtd|}|st	|d rt	|
  |}d}t|D ]l\}}|| }|t|d kr|t|	d kr||8 }tt||kr:|r4|t| |}q|}q|rZ|t| qZt|dkrn|| d S |dkr|d t|D ],\}}|dkr|dd	|   || q|dkr|d
 d S )NTr
   z\S*\s* r_   r   r9   rX   r`   )r;   rI   ra   
splitlinesr2   	enumerateappendrefindallAssertionErrorpop)r   r   r   r   rP   rQ   rR   r;   ZchunkslinesZ
max_width1rU   ilinerT   partsZ
max_width2currentjpart	candidater   r   r   _pprint_str   sX    
$


zPrettyPrinter._pprint_strc                 C   s   |j }t|dkr"|t| d S |dk}|rF|d7 }|d7 }|d d}	t|| j| |D ]$}
||	 ||
 |	s\dd|  }	q\|r|d d S )N   r
   r_   rs   r9   rX   r`   )r;   rI   ra   _wrap_bytes_reprr2   )r   r   r   r   rP   rQ   rR   r;   ZparensdelimrT   r   r   r   _pprint_bytes3  s"    zPrettyPrinter._pprint_bytesc              	   C   s>   |j }|d | t|||d |d ||d  |d d S )Nz
bytearray(
   r
   r`   )r;   r   bytes)r   r   r   r   rP   rQ   rR   r;   r   r   r   _pprint_bytearrayH  s      zPrettyPrinter._pprint_bytearrayc                 C   s8   | d | | ||d |d || | d d S )Nzmappingproxy(   r
   r`   )r;   r:   copyri   r   r   r   _pprint_mappingproxyQ  s    
 z"PrettyPrinter._pprint_mappingproxyc              	   C   s   |j }|| j7 }dd|  }t|d }	t|D ]f\}
\}}|
|	k}| |||}|| |d | |||t| d |r|nd|| |s0|| q0d S )N,
rX   r
   z: r   )r;   r1   rI   ru   rH   r:   )r   r]   r   r   rP   rQ   rR   r;   delimnlZ
last_indexr|   rZ   entlastrT   r   r   r   r^   Y  s     

 z PrettyPrinter._format_dict_itemsc                 C   sL  |j }|| j7 }| jdkr,|| jd d  dd|  }d}	| j| d  }
}t|}zt|}W n tk
rx   Y d S X d}|sH|}z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|| q~d S )	Nr
   rX   r   rs   FTr   , )
r;   r1   r2   iternextStopIterationr7   rH   rI   r:   )r   r]   r   r   rP   rQ   rR   r;   r   r   r   rU   itZnext_entr   r   rT   wr   r   r   rh   j  sR    



 zPrettyPrinter._format_itemsc                 C   s4   |  || | j|\}}}|s&d| _|r0d| _|S )NFT)rA   r   r0   rG   rF   )r   r   rQ   rR   ra   rC   rD   r   r   r   rH     s     
zPrettyPrinter._reprc                 C   s   t ||||| jS )zFormat object for a specific context, returning a string
        and flags indicating whether the representation is 'readable'
        and whether the object represents a recursive construct.
        )r   r8   )r   r   rQ   	maxlevelsrR   r   r   r   rA     s    zPrettyPrinter.formatc           	      C   s   t |s|t| d S | |j||}|j}|t |jd 7 }|d|j|d| f  | ||||d || |d d S )Nr
   z	%s(%s,
%srX   r`   )rI   r;   ra   rH   default_factoryrb   r'   rO   )	r   r   r   r   rP   rQ   rR   Zrdfrd   r   r   r   _pprint_default_dict  s    z"PrettyPrinter._pprint_default_dictc           	      C   s   t |s|t| d S |j}||jd  | jdkrN|| jd d  | }| |||t |j d |d || |d d S )Nrm   r
   rX   r   rn   )rI   r;   ra   rb   r'   r1   most_commonr^   )	r   r   r   r   rP   rQ   rR   rd   r]   r   r   r   _pprint_counter  s    
  zPrettyPrinter._pprint_counterc           
   	   C   s   t |js|t| d S |j}||jd  |t |jd 7 }t|jD ]d\}}	|t |jd kr| |	|||d || |d qN| |	||d|| |dd|   qNd S )Nr_   r
   r`   r   rX   )rI   mapsr;   ra   rb   r'   ru   r:   )
r   r   r   r   rP   rQ   rR   rd   r|   mr   r   r   _pprint_chain_map  s    
zPrettyPrinter._pprint_chain_mapc           	      C   s   t |s|t| d S |j}||jd  |t |jd 7 }|d |jd krz| ||||d || |d n:| |||d|| | |j||}|dd| |f  d S )Nr_   r
   rf   r   z])z],
%smaxlen=%s)rX   )rI   r;   ra   rb   r'   maxlenrh   rH   )	r   r   r   r   rP   rQ   rR   rd   Zrmlr   r   r   _pprint_deque  s&    

  zPrettyPrinter._pprint_dequec              	   C   s   |  |j|||||d  d S Nr
   r:   datari   r   r   r   _pprint_user_dict  s    zPrettyPrinter._pprint_user_dictc              	   C   s   |  |j|||||d  d S r   r   ri   r   r   r   _pprint_user_list  s    zPrettyPrinter._pprint_user_listc              	   C   s   |  |j|||||d  d S r   r   ri   r   r   r   _pprint_user_string  s    z!PrettyPrinter._pprint_user_string)r
   r   NN)2r'   r(   r)   r    r   r   r   r   r:   rJ   rO   rN   rL   re   _collectionsOrderedDictrj   rc   rl   tuplerq   ro   	frozensetr   r"   r   r   r   	bytearrayr   _typesMappingProxyTyper^   rh   rH   rA   r   defaultdictr   Counterr   ChainMapr   dequer   UserDictr   UserListr   
UserStringr   r   r   r   r   g   s^    +




1


)	c                 C   s  t | }|tkrt| ddfS t|dd }t|trD|tjkrD| sJdS t| }|rl||krldd||kfS ||krt| ddfS d||< d}d}	g }
|
j	}|d7 }|rt
|  td}n|  }|D ]b\}}t|||||\}}}t|||||\}}}|d||f  |o|o|}|s$|rd}	q||= d	d
|
 ||	fS t|tr\|tjkstt|trn|tjkrnt|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 }| D ]8}t|||||\}}}|| |sFd}|rd}	q||= |d
|
 ||	fS t| }||o|d dfS )NTFrL   )z{}TFz{...}r
   rY   z%s: %sz{%s}r   )z[]TFz[%s]z(%s,))z()TFz(%s)z...<)r#   _builtin_scalarsra   getattr
issubclassrN   rL   r$   rE   rv   r\   r]   r-   r   joinrc   r   rI   
startswith)r   rQ   r   rR   r   rp   rrS   rC   rD   Z
componentsrv   r]   kvZkreprZ	kreadableZkrecurZvreprZ	vreadableZvrecurrA   oZoreprZ	oreadableZorecurrT   r   r   r   r     s    

r   c                 C   s   dt | jt| f S )Nz<Recursion on %s with id=%s>)r#   r'   r$   r   r   r   r   rE   ?  s    rE   c                 C   s   dd l }| d kr,ddddgdddfgd	 } t }| }t| i d dd
 | }||  | }td||  td||  d S )Nr   string)r
   r      r         )      i Tz_safe_repr:zpformat:)timer   perf_counterr   r   print)r   r   rV   t1t2Zt3r   r   r   
_perfcheckD  s    
r   c                 c   s   d}t | d d }tdt | dD ]T}| ||d  }|| }||krP||8 }t t||krt|rnt|V  |}q$|}q$|rt|V  d S )N    r   r   )rI   rangera   )r   r   rP   r   r   r|   r   r   r   r   r   r   Q  s    
r   __main__)Nr
   r   N)r
   r   N)N)$r*   collectionsr   rw   sysr4   typesr   ior   r>   __all__r   r   r	   r   r   r   r   r-   r   r   r   r"   r   r   r.   floatcomplexr6   r#   r   rE   r   r   r'   r   r   r   r   <module>   sJ   
      F 

