B /g^Qc @sdZddlZddlZddlZddlZddlm Z dddddd gZ d!d d ddZ d"d d ddZ ddZddZddZGdddZddZGdd d ZddZeeeeeeeeedhZddZd#ddZ ddZ!e"d kre dS)$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)StringIOpprintpformat isreadable isrecursivesaferepr PrettyPrinterPFcompactcCs t|||||d}||dS)zAPretty-print a Python object to a stream [default is sys.stdout].)streamindentwidthdepthr N)rr)objectr r rrr Zprinterr/usr/lib64/python3.7/pprint.pyr/scCst||||d|S)z= 0Nzdepth must be > 0zwidth must be != 0) intZ ValueError_depth_indent_per_level_width_stream_sysZstdoutbool_compact)rr rrr r rrrrcszPrettyPrinter.__init__cCs&|||jddid|jddS)Nr )_formatr,writerrrrrrzPrettyPrinter.pprintcCs"t}|||ddid|SNr) _StringIOr1Zgetvalue)rrZsiorrrrszPrettyPrinter.pformatcCs||idddS)Nrrformatr3rrrrrzPrettyPrinter.isrecursivecCs ||idd\}}}|o| Sr5r7)rrZsreadable recursiverrrrr4zPrettyPrinter.isreadablec Cst|}||kr.|t|d|_d|_dS||||}|j||} t|| kr|j t |j d} | dk rd||<| |||||||d||=dSt |t rd||<|||||||d||=dS||dS)NTFr)r!r2 _recursion _recursive _readable_reprr+len _dispatchZgetr __repr__Z isinstancedict _pprint_dict) rrr r allowancecontextlevelobjidrep max_widthprrrr1s,    zPrettyPrinter._formatc Csj|j}|d|jdkr*||jddt|}|r^t|td} || |||d|||ddS)N{r key})r2r*r?sorteditemsr'_format_dict_items) rrr r rDrErFr2ZlengthrQrrrrCs zPrettyPrinter._pprint_dictcCslt|s|t|dS|j}||jd|t|||t|jd|d|||ddS)N(r))r?r2repr __class__r#r1listrQ)rrr r rDrErFclsrrr_pprint_ordered_dictsz"PrettyPrinter._pprint_ordered_dictcCs0|d|||||d|||ddS)N[rz])r2 _format_itemsrrr r rDrErFrrr _pprint_lists zPrettyPrinter._pprint_listcCsH|dt|dkrdnd}|||||t|||||dS)NrSrz,)rT)r2r?r[)rrr r rDrErFendcharrrr _pprint_tuples  zPrettyPrinter._pprint_tuplec Cst|s|t|dS|j}|tkr8|dd}n&||jdd}|t|jd7}t|td}|||||t|||||dS)NrKrO({})rrM) r?r2rUrVsetr#rPrr[) rrr r rDrErFtypr^rrr _pprint_sets  zPrettyPrinter._pprint_setcCs|j}t|s|t|dSg}|d} |dkrD|d7}|d7}|j|} } xt| D]\} } t| }| t| dkr| |8} t|| kr||q\td| }| | }d}xtt|D]h\}}||}|t|dkr| t| dkr||8}tt||kr&|r |t||}q|}qW|r\|t|q\Wt|dkr^||dS|dkrp|dx8t|D],\} }| dkr|dd|||qzW|dkr|d dS) NTrz\S*\s*rSrr0rLrT) r2r?rUZ splitlinesr+ enumerateappendreZfindallZpop)rrr r rDrErFr2ZchunksZlinesZ max_width1rIiZlinerHZpartsZ max_width2currentZjpart candidaterrr _pprint_strsT         zPrettyPrinter._pprint_strc Cs|j}t|dkr"|t|dS|dk}|rF|d7}|d7}|dd} x:t||j||D]$} || || | s^dd|} q^W|r|ddS)NrrSrer0rLrT)r2r?rU_wrap_bytes_reprr+) rrr r rDrErFr2ZparensdelimrHrrr _pprint_bytes's"  zPrettyPrinter._pprint_bytesc Cs>|j}|d|t|||d|d||d|ddS)Nz bytearray(i rrT)r2rqbytes)rrr r rDrErFr2rrr_pprint_bytearray<s zPrettyPrinter._pprint_bytearraycCs8|d||||d|d|||ddS)Nz mappingproxy(i rrT)r2r1copyr\rrr_pprint_mappingproxyEs z"PrettyPrinter._pprint_mappingproxyc Cs|j}||j7}dd|}t|d} xrt|D]f\} \} } | | k} || ||}|||d|| ||t|d| r|nd||| s2||q2WdS)N, rLrz: r)r2r*r?rfr>r1)rrQr r rDrErFr2delimnlZ last_indexrirNentlastrHrrrrRMs    z PrettyPrinter._format_dict_itemscCsN|j}||j7}|jdkr,||jdddd|}d} |j|d} } t|} y t| } Wntk rvdSXd}x|sH| }y t| } Wn(tk rd}| |8} | |8} YnX|jr||||}t|d}| |kr| } | r|} | |kr| |8} || d} ||q~|| |} | ||||r<|nd||q~WdS) NrrLrvreFTr, ) r2r*r+ZiterZnextZ StopIterationr/r>r?r1)rrQr r rDrErFr2rwrprrIZitZnext_entryrxrHZwrrrr[^sN         zPrettyPrinter._format_itemscCs4||||j|\}}}|s&d|_|r0d|_|S)NFT)r8rtr)r=r<)rrrErFrUr9r:rrrr>s zPrettyPrinter._reprcCst||||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)rrrE maxlevelsrFrrrr8szPrettyPrinter.formatc Cst|s|t|dS||j||}|j}|t|jd7}|d|j|d|f|||||d|||ddS)Nrz %s(%s, %srLrT)r?r2rUr>Zdefault_factoryrVr#rC) rrr r rDrErFZrdfrXrrr_pprint_default_dictsz"PrettyPrinter._pprint_default_dictc Cst|s|t|dS|j}||jd|jdkrN||jdd|}||||t|jd|d|||ddS)Nr`rrLrra)r?r2rUrVr#r*Z most_commonrR) rrr r rDrErFrXrQrrr_pprint_counters zPrettyPrinter._pprint_counterc Cst|js|t|dS|j}||jd|t|jd7}xrt|jD]d\}} |t|jdkr|| |||d|||dqP|| ||d|||dd|qPWdS)NrSrrTrvrL)r?Zmapsr2rUrVr#rfr1) rrr r rDrErFrXriZmrrr_pprint_chain_maps  zPrettyPrinter._pprint_chain_mapc Cst|s|t|dS|j}||jd|t|jd7}|d|jdkrz|||||d|||dn:||||d||||j||}|dd||fdS)NrSrrZrz])z], %smaxlen=%s)rL)r?r2rUrVr#Zmaxlenr[r>) rrr r rDrErFrXZrmlrrr _pprint_deques    zPrettyPrinter._pprint_dequec C||j|||||ddSNrr1Zdatar\rrr_pprint_user_dictrzPrettyPrinter._pprint_user_dictc Crrrr\rrr_pprint_user_listrzPrettyPrinter._pprint_user_listc Crrrr\rrr_pprint_user_stringrz!PrettyPrinter._pprint_user_string)rr NN)2r#r$r%rrrrrr1r@rCrBrArY _collectionsZ OrderedDictr]rWr_tuplerdrb frozensetrmrrqrrrs bytearrayru_typesZMappingProxyTyperRr[r>r8r|Z defaultdictr}ZCounterr~ZChainMaprZdequerZUserDictrZUserListrZ UserStringrrrrrbsZ&      1    )       cCst|}|tkrt|ddfSt|dd}t|tr:|tjkr:|sJdSt|}|rl||krldd||kfS||krt|ddfSd||<d}d}g} | j } |d7}t } t | t d} xf| D]^\} }| | |||\}}}| ||||\}}}| d||f|o |o |}|s|rd}qW||=d d | ||fSt|trR|tjksjt|trf|tjkrft|tr|sd Sd }n"t|dkrd }n|sdSd}t|}|r||kr|dd||kfS||krt|ddfSd||<d}d}g} | j } |d7}x>|D]6}t ||||\}}}| ||s)r r#r!rrrrr;1sr;cCsddl}|dkr,ddddgdddfgd }t}|}t|idd|}|||}td ||td ||dS) NrZstring)rrirnii)iiiz _safe_repr:zpformat:)timerZ perf_counterrrZprint)rrrJZt1Zt2Zt3rrr _perfcheck6s rccsd}t|dd}xhtdt|dD]T}|||d}||}||krR||8}tt||krv|rpt|V|}q&|}q&W|rt|VdS)Nsrnr)r?ZrangerU)rrrDrjryrirkrlrrrroCs roZ__main__)Nrr N)rr Nr)#r&Z collectionsrrhZsysr-ZtypesrZiorr6Z__all__rrrrrrr'rrrrrrrr(ZfloatZcomplexr.r rr;rror#rrrrZ#s6   D