3 \|Q @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 PrettyPrinterPF)compactcCs t|||||d}|j|dS)zAPretty-print a Python object to a stream [default is sys.stdout].)streamindentwidthdepthr N)rr)objectr r rrr Zprinterr/usr/lib64/python3.6/pprint.pyr/scCst||||dj|S)z= 0Nzdepth must be > 0zwidth must be != 0) int ValueError_depth_indent_per_level_width_stream_sysstdoutbool_compact)rr rrr r rrrrcszPrettyPrinter.__init__cCs&|j||jddid|jjddS)Nr )_formatr+write)rrrrrrszPrettyPrinter.pprintcCs"t}|j||ddid|jS)Nr) _StringIOr1getvalue)rrZsiorrrrszPrettyPrinter.pformatcCs|j|idddS)Nrr)format)rrrrrrszPrettyPrinter.isrecursivecCs |j|idd\}}}|o| S)Nr)r5)rrsreadable recursiverrrrszPrettyPrinter.isreadablec Cst|}||kr.|jt|d|_d|_dS|j|||}|j||} t|| kr|jj t |j d} | dk rd||<| |||||||d||=dSt |t rd||<|j||||||d||=dS|j|dS)NTFr )rr2 _recursion _recursive _readable_reprr*len _dispatchgetr__repr__ isinstancedict _pprint_dict) rrr r allowancecontextlevelobjidrep max_widthprrrr1s,    zPrettyPrinter._formatc Csj|j}|d|jdkr*||jddt|}|r^t|jtd} |j| |||d|||ddS)N{r  )key})r2r)r=sorteditemsr%_format_dict_items) rrr r rDrErFr2ZlengthrPrrrrCs zPrettyPrinter._pprint_dictcCslt|s|jt|dS|j}|j|jd|jt|j||t|jd|d|||jddS)N(r ))r=r2repr __class__rr1listrP)rrr r rDrErFclsrrr_pprint_ordered_dictsz"PrettyPrinter._pprint_ordered_dictcCs0|jd|j||||d|||jddS)N[r ])r2 _format_items)rrr r rDrErFrrr _pprint_lists zPrettyPrinter._pprint_listcCsH|jdt|dkrdnd}|j||||t||||j|dS)NrRr z,)rS)r2r=r[)rrr r rDrErFendcharrrr _pprint_tuples  zPrettyPrinter._pprint_tuplec Cst|s|jt|dS|j}|tkr8|jdd}n&|j|jdd}|t|jd7}t|td}|j||||t||||j|dS)NrKrNz({z})r )rM) r=r2rTrUsetrrOrr[) rrr r rDrErFtypr]rrr _pprint_sets  zPrettyPrinter._pprint_setcCs|j}t|s|t|dSg}|jd} |dkrD|d7}|d7}|j|} } x t| D]\} } t| }| t| dkr| |8} t|| kr|j|q^tjd| }|st |d  st |j | }d}xxt|D]l\}}||}|t|dkr| t| dkr||8}tt||krD|r>|jt||}q|}qW|r^|jt|q^Wt|dkr|||dS|dkr|dx8t|D],\} }| dkr|dd|||qW|dkr|d dS) NTr z\S*\s*rRrr0rLrS) r2r=rT splitlinesr* enumerateappendrefindallAssertionErrorpop)rrr r rDrErFr2ZchunkslinesZ max_width1rIilinerHpartsZ max_width2currentjpart candidaterrr _pprint_strsX     $   zPrettyPrinter._pprint_strc Cs|j}t|dkr"|t|dS|dk}|rF|d7}|d7}|dd} x:t||j||D]$} || || | s^dd|} q^W|r|ddS)Nr rRrbr0rLrS)r2r=rT_wrap_bytes_reprr*) rrr r rDrErFr2ZparensdelimrHrrr _pprint_bytes's"  zPrettyPrinter._pprint_bytescCs>|j}|d|jt|||d|d||d|ddS)Nz bytearray( r rS)r2rwbytes)rrr r rDrErFr2rrr_pprint_bytearray<s zPrettyPrinter._pprint_bytearraycCs8|jd|j|j||d|d|||jddS)Nz mappingproxy( r rS)r2r1copy)rrr r rDrErFrrr_pprint_mappingproxyEs z"PrettyPrinter._pprint_mappingproxycCs|j}||j7}dd|}t|d} xrt|D]f\} \} } | | k} |j| ||}|||d|j| ||t|d| r|nd||| s2||q2WdS)Nz, rLr z: r)r2r)r=rer<r1)rrPr r rDrErFr2delimnlZ last_indexrlrMentlastrHrrrrQMs    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|j|||}t|d}| |kr| } | r|} | |kr| |8} || d} ||q~|| |} |j ||||r<|nd||q~WdS) Nr rLz, rbFTrz, ) r2r)r*iternext StopIterationr/r<r=r1)rrPr r rDrErFr2r~rvrrIitZnext_entrrrHwrrrr[^sN         zPrettyPrinter._format_itemscCs4|j||j|j|\}}}|s&d|_|r0d|_|S)NFT)r5r|r(r;r:)rrrErFrTr7r8rrrr<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 maxlevelsrFrrrr5szPrettyPrinter.formatc Cst|s|jt|dS|j|j||}|j}|t|jd7}|jd|j|d|f|j||||d|||jddS)Nr z %s(%s, %srLrS)r=r2rTr<default_factoryrUrrC) rrr r rDrErFZrdfrWrrr_pprint_default_dictsz"PrettyPrinter._pprint_default_dictc Cst|s|jt|dS|j}|j|jd|jdkrN|j|jdd|j}|j|||t|jd|d|||jddS)Nz({r rLrz}))r=r2rTrUrr) most_commonrQ) rrr r rDrErFrWrPrrr_pprint_counters zPrettyPrinter._pprint_counterc Cst|js|jt|dS|j}|j|jd|t|jd7}xrt|jD]d\}} |t|jdkr|j| |||d|||jdqP|j| ||d|||jdd|qPWdS)NrRr rSz, rL)r=mapsr2rTrUrrer1) rrr r rDrErFrWrlmrrr_pprint_chain_maps  zPrettyPrinter._pprint_chain_mapc Cst|s|jt|dS|j}|j|jd|t|jd7}|jd|jdkrz|j||||d|||jdn:|j|||d|||j|j||}|jdd||fdS)NrRr rYrz])z], %smaxlen=%s)rL)r=r2rTrUrmaxlenr[r<) rrr r rDrErFrWZrmlrrr _pprint_deques    zPrettyPrinter._pprint_dequecCs|j|j|||||ddS)Nr )r1data)rrr r rDrErFrrr_pprint_user_dictszPrettyPrinter._pprint_user_dictcCs|j|j|||||ddS)Nr )r1r)rrr r rDrErFrrr_pprint_user_listszPrettyPrinter._pprint_user_listcCs|j|j|||||ddS)Nr )r1r)rrr r rDrErFrrr_pprint_user_stringsz!PrettyPrinter._pprint_user_string)r r NN)2rr r!rrrrrr1r>rCrBr@rX _collections OrderedDictr\rVr^tuplerar_ frozensetrsrrwryrz bytearrayr}_typesMappingProxyTyperQr[r<r5r defaultdictrCounterrChainMaprdequerUserDictrUserListr UserStringrrrrrbsZ&      1    )       cCst|}|tkrt|ddfSt|dd}t|to<|tjkr8|sHdSt|}|rj||krjdd||kfS||krt|ddfSd||<d}d}g} | j } |d7}t } t |j t d} xf| D]^\} }| | |||\}}}| ||||\}}}| d||f|o |o |}|s|rd}qW||=d d j| ||fSt|trP|tjksht|trd|tjkrdt|tr|s~dSd }n"t|dkrd }n|sdSd}t|}|r||kr|dd||kfS||krt|ddfSd||<d}d}g} | j } |d7}x>|D]6}t ||||\}}}| ||s:d}|rd}qW||=|d j| ||fSt|}||o~|jd dfS)NTFr@{}z{...}r )rMz%s: %sz{%s}z, []z[%s]z(%s,)()z(%s)z...<)rTF)rTF)rTF)r_builtin_scalarsrTgetattr issubclassrBr@rr9rfrrOrPr%joinrVrr= startswith)rrErrFr`rrGr7r8Z componentsrfrrPkvZkreprZ kreadableZkrecurZvreprZ vreadableZvrecurr5oZoreprZ oreadableZorecurrHrrrrs|       rcCsdt|jt|fS)Nz)rrr)rrrrr91sr9cCsddl}|dkr,dd ddgddd fgd }t}|j}t|idd|j}|j||j}td ||td ||dS)Nrstringr rrt)iz _safe_repr:zpformat:)r r)timerrrprint)rrrJZt1Zt2Zt3rrr _perfcheck6s rccsd}t|dd}xhtdt|dD]T}|||d}||}||krR||8}tt||krv|rpt|V|}q&|}q&W|rt|VdS)Nrtr)r=rangerT)rrrDrorrlrqrrrrrruCs ru__main__)Nr r N)r r N)N)#r" collectionsrrgsysr,typesriorr3__all__rrrrrrr%rrrrryrr&floatcomplexr.rrr9rrurrrrr#s6   D