B /g^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}||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) int ValueError_depth_indent_per_level_width_stream_sysstdoutbool_compact)rr rrr r rrrrcszPrettyPrinter.__init__cCs&|||jddid|jddS)Nr )_formatr+write)rrrrrrszPrettyPrinter.pprintcCs"t}|||ddid|S)Nr) _StringIOr1getvalue)rrZsiorrrrszPrettyPrinter.pformatcCs||idddS)Nrr)format)rrrrrrszPrettyPrinter.isrecursivecCs ||idd\}}}|o| S)Nr)r5)rrsreadable recursiverrrrszPrettyPrinter.isreadablec Cst|}||kr.|t|d|_d|_dS||||}|j||} t|| kr|j t |j d} | dk rd||<| |||||||d||=dSt |t rd||<|||||||d||=dS||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|td} || |||d|||ddS)N{r  )key})r2r)r=sorteditemsr%_format_dict_items) rrr r rDrErFr2lengthrPrrrrCs zPrettyPrinter._pprint_dictcCslt|s|t|dS|j}||jd|t|||t|jd|d|||ddS)N(r ))r=r2repr __class__rr1listrP)rrr r rDrErFclsrrr_pprint_ordered_dictsz"PrettyPrinter._pprint_ordered_dictcCs0|d|||||d|||ddS)N[r ])r2 _format_items)rrr r rDrErFrrr _pprint_lists zPrettyPrinter._pprint_listcCsH|dt|dkrdnd}|||||t|||||dS)NrSr z,)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)NrKrNz({z})r )rM) r=r2rUrVsetrrOrr\) 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) NTr z\S*\s*rSrr0rLrT) r2r=rU splitlinesr* enumerateappendrefindallpop)rrr r rDrErFr2ZchunkslinesZ max_width1rIilinerHpartsZ max_width2currentjpart 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)Nr rSrcr0rLrT)r2r=rU_wrap_bytes_reprr*) rrr r rDrErFr2ZparensdelimrHrrr _pprint_bytes's"  zPrettyPrinter._pprint_bytesc Cs>|j}|d|t|||d|d||d|ddS)Nz bytearray( r rT)r2rvbytes)rrr r rDrErFr2rrr_pprint_bytearray<s zPrettyPrinter._pprint_bytearraycCs8|d||||d|d|||ddS)Nz mappingproxy( r rT)r2r1copy)rrr r rDrErFrrr_pprint_mappingproxyEs z"PrettyPrinter._pprint_mappingproxyc Cs|j}||j7}dd|}t|d} xrt|D]f\} \} } | | k} || ||}|||d|| ||t|d| r|nd||| s2||q2WdS)Nz, rLr z: r)r2r)r=rer<r1)rrPr r rDrErFr2delimnlZ last_indexrkrMentlastrHrrrrQMs    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) Nr rLz, rcFTrz, ) r2r)r*iternext StopIterationr/r<r=r1)rrPr r rDrErFr2r}rurrIitZnext_entrr~rHwrrrr\^sN         zPrettyPrinter._format_itemscCs4||||j|\}}}|s&d|_|r0d|_|S)NFT)r5r{r(r;r:)rrrErFrUr7r8rrrr<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|t|dS||j||}|j}|t|jd7}|d|j|d|f|||||d|||ddS)Nr z %s(%s, %srLrT)r=r2rUr<default_factoryrVrrC) rrr r rDrErFZrdfrXrrr_pprint_default_dictsz"PrettyPrinter._pprint_default_dictc Cst|s|t|dS|j}||jd|jdkrN||jdd|}||||t|jd|d|||ddS)Nz({r rLrz}))r=r2rUrVrr) most_commonrQ) rrr r rDrErFrXrPrrr_pprint_counters zPrettyPrinter._pprint_counterc Cst|js|t|dS|j}||jd|t|jd7}xrt|jD]d\}} |t|jdkr|| |||d|||dqP|| ||d|||dd|qPWdS)NrSr rTz, rL)r=mapsr2rUrVrrer1) rrr r rDrErFrXrkmrrr_pprint_chain_maps  zPrettyPrinter._pprint_chain_mapc Cst|s|t|dS|j}||jd|t|jd7}|d|jdkrz|||||d|||dn:||||d||||j||}|dd||fdS)NrSr rZrz])z], %smaxlen=%s)rL)r=r2rUrVrmaxlenr\r<) rrr r rDrErFrXZrmlrrr _pprint_deques    zPrettyPrinter._pprint_dequec Cs||j|||||ddS)Nr )r1data)rrr r rDrErFrrr_pprint_user_dictszPrettyPrinter._pprint_user_dictc Cs||j|||||ddS)Nr )r1r)rrr r rDrErFrrr_pprint_user_listszPrettyPrinter._pprint_user_listc Cs||j|||||ddS)Nr )r1r)rrr r rDrErFrrr_pprint_user_stringsz!PrettyPrinter._pprint_user_string)r r NN)2rr r!rrrrrr1r>rCrBr@rY _collections OrderedDictr]rWr_tuplerbr` frozensetrrrrvrxry bytearrayr|_typesMappingProxyTyperQr\r<r5r defaultdictrCounterrChainMaprdequerUserDictrUserListr 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)rrr)rrrrr91sr9cCsddl}|dkr,ddddgdddfgd }t}|}t|idd|}|||}td ||td ||dS) Nrstring)r rrs)iz _safe_repr:zpformat:)timerZ perf_counterrrprint)rrrJZt1Zt2Zt3rrr _perfcheck6s rccsd}t|dd}xhtdt|dD]T}|||d}||}||krR||8}tt||krv|rpt|V|}q&|}q&W|rt|VdS)Nrsr)r=rangerU)rrrDrnrrkrprqrrrrtCs rt__main__)Nr r N)r r N)N)#r" collectionsrrgsysr,typesriorr3__all__rrrrrrr%rrrrrxrr&floatcomplexr.rrr9rrtrrrrr#s6   D