a dW @sdZddlZddlZddlZddlZddlm Z gdZ d&ddd d d Z d'ddd d d Z ddddZddZddZddZGdddZddZGdddZddZeeeeeeeeedhZdd Z d(d!d"Z!d#d$Z"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)StringIO)pprintpformat isreadable isrecursivesaferepr PrettyPrinterppPFTcompact sort_dictscCs"t||||||d}||dS)zAPretty-print a Python object to a stream [default is sys.stdout].)streamindentwidthdepthr rN)rr)objectrrrrr rprinterr/usr/lib64/python3.9/pprint.pyr/s rcCst|||||d|S)zrr r NFTr cCst|}t|}|dkr td|dur8|dkr8td|sDtd||_||_||_|durf||_ntj|_t||_ ||_ dS)aHandle 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. rzindent must be >= 0Nzdepth must be > 0zwidth must be != 0) int ValueError_depth_indent_per_level_width_stream_sysstdoutbool_compact _sort_dicts)r rrrrr rrrrr!hs  zPrettyPrinter.__init__cCs&|||jddid|jddS)Nr )_formatr4writer rrrrrszPrettyPrinter.pprintcCs"t}|||ddid|SNr) _StringIOr;getvalue)r rsiorrrrszPrettyPrinter.pformatcCs||idddS)Nrrformatr=rrrrszPrettyPrinter.isrecursivecCs ||idd\}}}|o| Sr>rB)r rsreadable recursiverrrrszPrettyPrinter.isreadablec Cst|}||vr.|t|d|_d|_dS||||}|j||} t|| kr|j t |j d} | durd||<| |||||||d||=dSt |t rd||<|||||||d||=dS||dS)NTFr )r%r< _recursion _recursive _readable_reprr3len _dispatchgetr$__repr__ isinstancedict _pprint_dict) r rrr allowancecontextlevelobjidrep max_widthprrrr;s.   zPrettyPrinter._formatc Csz|j}|d|jdkr*||jddt|}|rn|jrNt|td} n|} || |||d|||ddS)N{r  key})r<r2rKr9sorteditemsr._format_dict_items) r rrrrRrSrTr<lengthr_rrrrQs zPrettyPrinter._pprint_dictcCslt|s|t|dS|j}||jd|t|||t|jd|d|||ddS)N(r ))rKr<repr __class__r(r;listr_)r rrrrRrSrTclsrrr_pprint_ordered_dictsz"PrettyPrinter._pprint_ordered_dictcCs0|d|||||d|||ddS)N[r ])r< _format_itemsr rrrrRrSrTrrr _pprint_lists  zPrettyPrinter._pprint_listcCsH|dt|dkrdnd}|||||t|||||dS)Nrbr z,)rc)r<rKrk)r rrrrRrSrTendcharrrr _pprint_tuples  zPrettyPrinter._pprint_tuplec Cst|s|t|dS|j}|tur8|dd}n&||jdd}|t|jd7}t|td}|||||t|||||dS)NrYr]({})r r[) rKr<rdresetr(r^rrk) r rrrrRrSrTtyprnrrr _pprint_sets  zPrettyPrinter._pprint_setcCs|j}t|s|t|dSg}|d} |dkrD|d7}|d7}|j|} } t| D]\} } t| }| t| dkr| |8} t|| kr||qZtd| }|sJ|drJ| | }d}t|D]l\}}||}|t|dkr| t| dkr||8}tt||kr:|r4|t||}q|}q|rZ|t|qZt|dkrn||dS|dkr|dt|D],\} }| dkr|dd |||q|dkr|d dS) NTr z\S*\s*rbrr:rZrc) r<rKrd splitlinesr3 enumerateappendrefindallpop)r rrrrRrSrTr<chunkslines max_width1rWilinerVparts max_width2currentjpart candidaterrr _pprint_strsX      $    zPrettyPrinter._pprint_strc Cs|j}t|dkr"|t|dS|dk}|rF|d7}|d7}|dd} t||j||D]$} || || | s\dd|} q\|r|ddS)Nr rbrvr:rZrc)r<rKrd_wrap_bytes_reprr3) r rrrrRrSrTr<parensdelimrVrrr _pprint_bytes3s"  zPrettyPrinter._pprint_bytesc Cs>|j}|d|t|||d|d||d|ddS)Nz bytearray( r rc)r<rbytes)r rrrrRrSrTr<rrr_pprint_bytearrayHs zPrettyPrinter._pprint_bytearraycCs8|d||||d|d|||ddS)Nz mappingproxy( r rc)r<r;copyrlrrr_pprint_mappingproxyQs  z"PrettyPrinter._pprint_mappingproxyc Cst|tjurd}n|jj}|t|d7}dd|}|j} t| d} ||dt | D]^\} \} } || |d| | k}| | ||t| d|r|nd|||sd||qd|ddS)N namespacer , rZrb=rc) r$_typesSimpleNamespacerer(rK__dict__r_r<rxr;)r rrrrRrSrTcls_namedelimnlr_ last_indexrr\entlastrrr_pprint_simplenamespaceYs&       z%PrettyPrinter._pprint_simplenamespacec Cs|j}||j7}dd|}t|d} t|D]f\} \} } | | k} || ||}|||d|| ||t|d| r|nd||| s0||q0dS)NrrZr z: r)r<r2rKrxrJr;)r r_rrrRrSrTr<rrrr\rrrVrrrr`ts    z PrettyPrinter._format_dict_itemscCsH|j}||j7}|jdkr,||jdddd|}d} |j|d} } t|} z t| } WntyvYdS0d}|sD| }z t| } Wn&tyd}| |8} | |8} Yn0|jr||||}t|d}| |kr| } | r|} | |kr| |8} || d} ||q||| |} | ||||r8|nd||q|dS) Nr rZrrvFTr, ) r<r2r3iternext StopIterationr8rJrKr;)r r_rrrRrSrTr<rrrrWitnext_entrrrVwrrrrksP           zPrettyPrinter._format_itemscCs4||||j|\}}}|s&d|_|r0d|_|S)NFT)rCrr1rIrH)r rrSrTrdrErFrrrrJs  zPrettyPrinter._reprcCst|||||jS)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. )rr9)r rrS maxlevelsrTrrrrCszPrettyPrinter.formatc Cst|s|t|dS||j||}|j}|t|jd7}|d|j|d|f|||||d|||ddS)Nr z %s(%s, %srZrc)rKr<rdrJdefault_factoryrer(rQ) r rrrrRrSrTrdfrgrrr_pprint_default_dictsz"PrettyPrinter._pprint_default_dictc Cst|s|t|dS|j}||jd|jdkrN||jdd|}||||t|jd|d|||ddS)Nrpr rZrrq)rKr<rdrer(r2 most_commonr`) r rrrrRrSrTrgr_rrr_pprint_counters zPrettyPrinter._pprint_counterc Cst|js|t|dS|j}||jd|t|jd7}t|jD]d\}} |t|jdkr|| |||d|||dqN|| ||d|||dd|qNdS)Nrbr rcrrZ)rKmapsr<rdrer(rxr;) r rrrrRrSrTrgrmrrr_pprint_chain_maps  zPrettyPrinter._pprint_chain_mapc Cst|s|t|dS|j}||jd|t|jd7}|d|jdurz|||||d|||dn:||||d||||j||}|dd||fdS)Nrbr rirz])z], %smaxlen=%s)rZ)rKr<rdrer(maxlenrkrJ) r rrrrRrSrTrgrmlrrr _pprint_deques"    zPrettyPrinter._pprint_dequec Cs||j|||||ddSNr r;datarlrrr_pprint_user_dictszPrettyPrinter._pprint_user_dictc Cs||j|||||ddSrrrlrrr_pprint_user_listszPrettyPrinter._pprint_user_listc Cs||j|||||ddSrrrlrrr_pprint_user_string sz!PrettyPrinter._pprint_user_string)r r NN)4r(r)r*r!rrrrr;rLrQrPrNrh _collections OrderedDictrmrfrotuplertrr frozensetrr#rrr bytearrayrrMappingProxyTyperrr`rkrJrCr defaultdictrCounterrChainMaprdequerUserDictrUserListr UserStringrrrrrgs` +      1     )       rcCst|}|tvrt|ddfSt|dd}t|trD|tjurD|sJdSt|}|rl||krldd||vfS||vrt|ddfSd||<d}d} g} | j } |d7}|rt | t d} n| } | D]b\} }t | ||||\}}}t |||||\}}}| d||f|o|o|}|s$|rd} q||=d d | || fSt|tr\|tjustt|trn|tjurnt|tr|sd Sd }n"t|dkrd }n|sdSd}t|}|r||kr|dd||vfS||vrt|ddfSd||<d}d} g} | j } |d7}|D]8}t |||||\}}}| ||sFd}|rd} q||=|d | || fSt|}||o|d dfS)NTFrN)z{}TFz{...}r r[z%s: %sz{%s}r)z[]TFz[%s]z(%s,))z()TFz(%s)z...<)r$_builtin_scalarsrdgetattr issubclassrPrNr%rGryr^r_r.rjoinrfrrK startswith)rrSrrTrrsrrUrErF componentsryr_kvkrepr kreadablekrecurvrepr vreadablevrecurrCoorepr oreadableorecurrVrrrrs      rcCsdt|jt|fS)Nz)r$r(r%rrrrrGZsrGcCsddl}|dur,ddddgdddfgd }t}|}t|iddd |}|||}td ||td ||dS) Nrstring)r rr)iTz _safe_repr:zpformat:)timer perf_counterrrprint)rrrXt1t2t3rrr _perfcheck_s rccsd}t|dd}tdt|dD]T}|||d}||}||krP||8}tt||krt|rnt|V|}q$|}q$|rt|VdS)Nrr)rKrangerd)rrrRrrrrrrrrrls r__main__)Nr r N)r r N)N)$r+ collectionsrrzsysr5typesriorr?__all__rrr rrrrr.rrrr#rrr/floatcomplexr7r$rrGrrr(rrrr s>   -F