o w[e|_ @sdZddlZddlZddlZddlZddlZ ddl m Z gdZ d$dddd d d Zd%dddd d d ZddddZddZddZddZGdddZddZGdddZeeeeeeeedhZddZ d&dd Z!d!d"Z"e#d#kr|e!dSdS)'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_dictsunderscore_numbersc Cs$t|||||||d}||dS)zAPretty-print a Python object to a stream [default is sys.stdout].)streamindentwidthdepthr rrN)rr) objectrrrrr rrprinterr/usr/lib/python3.10/pprint.pyr0s rcCst||||||d|S)zZ2e2e ej3j <d?d@Z4e4e ej5j <dAdBZ6dS)Drr r NFTr cCst|}t|}|dkrtd|dur|dkrtd|s"td||_||_||_|dur3||_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_underscore_numbers)r#rrrrr rrrrrr$ks"  zPrettyPrinter.__init__cCs&|||jddid|jddS)Nr )_formatr7writer#rrrrrszPrettyPrinter.pprintcCs"t}|||ddid|SNr) _StringIOr?getvalue)r#rsiorrrrszPrettyPrinter.pformatcCs||idddS)NrrformatrArrrrszPrettyPrinter.isrecursivecCs ||idd\}}}|o| SrBrF)r#rsreadable recursiverrrrs zPrettyPrinter.isreadablec Cs t|}||vr|t|d|_d|_dS||||}|j||} t|| kr~|j t |j d} | durNd||<| |||||||d||=dSt |r~t|t s~|jjr~t|j dr~d|j jjvr~d||<|||||||d||=dS||dS)NTFr __wrapped__ __create_fn__)r(r@ _recursion _recursive _readable_reprr6len _dispatchgetr'__repr__ _dataclasses is_dataclass isinstance__dataclass_params__reprhasattrrKr-_pprint_dataclass) r#rrr allowancecontextlevelobjidrep max_widthprrrr?s8   zPrettyPrinter._formatc s`jj}|t|d7}fddtD}||d||||||||ddS)Nr cs$g|]}|jr|jt|jfqSr)rYnamegetattr).0frrr s$z3PrettyPrinter._pprint_dataclass..()) __class__r+rQrUfieldsr@_format_namespace_items r#rrrr\r]r^cls_nameitemsrrrr[s zPrettyPrinter._pprint_dataclassc Csz|j}|d|jdkr||jddt|}|r7|jr't|td} n|} || |||d|||ddS)N{r  key})r@r5rQr<sortedror1_format_dict_items) r#rrrr\r]r^r@lengthrorrr _pprint_dicts  zPrettyPrinter._pprint_dictcCslt|s |t|dS|j}||jd|t|||t|jd|d|||ddS)Nrhr ri)rQr@rYrjr+r?listro)r#rrrr\r]r^clsrrr_pprint_ordered_dictsz"PrettyPrinter._pprint_ordered_dictcCs0|d|||||d|||ddS)N[r ])r@ _format_itemsr#rrrr\r]r^rrr _pprint_lists zPrettyPrinter._pprint_listcCsH|dt|dkr dnd}|||||t|||||dS)Nrhr z,)ri)r@rQr~)r#rrrr\r]r^endcharrrr _pprint_tuples zPrettyPrinter._pprint_tuplec Cst|s |t|dS|j}|tur|dd}n||jdd}|t|jd7}t|td}|||||t|||||dS)Nrprt({})r rr) rQr@rYrjsetr+rur r~) r#rrrr\r]r^typrrrr _pprint_sets  zPrettyPrinter._pprint_setcCs|j}t|s|t|dSg}|d} |dkr"|d7}|d7}|j|} } t| D]x\} } t| }| t| dkrA| |8} t|| krM||q-td| }|sWJ|dr]J| | }d}t|D]2\}}||}|t|dkr| t| dkr||8}tt||kr|r|t||}qi|}qi|r|t|q-t|dkr||dS|dkr|dt|D]\} }| dkr|dd |||q|dkr|d dSdS) NTr z\S*\s*rhrr>rqri) r@rQrY splitlinesr6 enumerateappendrefindallpop)r#rrrr\r]r^r@chunkslines max_width1railiner`parts max_width2currentjpart candidaterrr _pprint_strs\          zPrettyPrinter._pprint_strc Cs|j}t|dkr|t|dS|dk}|r#|d7}|d7}|dd} t||j||D]} || || | s@dd|} q.|rI|ddSdS)Nr rhrr>rqri)r@rQrY_wrap_bytes_reprr6) r#rrrr\r]r^r@parensdelimr`rrr _pprint_bytesCs&    zPrettyPrinter._pprint_bytesc Cs>|j}|d|t|||d|d||d|ddS)Nz bytearray( r ri)r@rbytes)r#rrrr\r]r^r@rrr_pprint_bytearrayXs  zPrettyPrinter._pprint_bytearraycCs8|d||||d|d|||ddS)Nz mappingproxy( r ri)r@r?copyrrrr_pprint_mappingproxyas z"PrettyPrinter._pprint_mappingproxyc Csft|tjur d}n|jj}|t|d7}|j}||d| |||||||ddS)N namespacer rhri) r'_typesSimpleNamespacerjr+rQ__dict__ror@rlrmrrr_pprint_simplenamespaceis z%PrettyPrinter._pprint_simplenamespacec Cs|j}||j7}dd|}t|d} t|D]3\} \} } | | k} || ||}|||d|| ||t|d| r@|nd||| sK||qdS)N, rqr z: r)r@r5rQrrPr?)r#rorrr\r]r^r@delimnl last_indexrrsentlastr`rrrrvxs"    z PrettyPrinter._format_dict_itemsc Cs|j}dd|}t|d} t|D]7\} \} } | | k} || |dt| |vr0|dn|| ||t| d| r?|nd||| sJ||qdS)Nrrqr =...)r@rQrr(r?)r#rorrr\r]r^r@rrrrsrrrrrrls"     z%PrettyPrinter._format_namespace_itemscCsF|j}||j7}|jdkr||jdddd|}d} |j|d} } t|} zt| } Wn ty;YdSwd}|s| }zt| } Wnty[d}| |8} | |8} Ynw|jr||||}t|d}| |krv| } | rv|} | |kr| |8} || d} ||q>|| |} | ||||r|nd|||r@dSdS) Nr rqrrFTr, ) r@r5r6iternext StopIterationr;rPrQr?)r#rorrr\r]r^r@rrrraitnext_entrrr`wrrrr~sV            zPrettyPrinter._format_itemscCs4||||j|\}}}|sd|_|rd|_|S)NFT)rGrr4rOrN)r#rr]r^rYrIrJrrrrPs  zPrettyPrinter._reprcCs|||||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)r#rr] maxlevelsr^rrrrGszPrettyPrinter.formatc Cst|s |t|dS||j||}|j}|t|jd7}|d|j|d|f|||||d|||ddS)Nr z %s(%s, %srqri)rQr@rYrPdefault_factoryrjr+rx) r#rrrr\r]r^rdfrzrrr_pprint_default_dictsz"PrettyPrinter._pprint_default_dictc Cst|s |t|dS|j}||jd|jdkr'||jdd|}||||t|jd|d|||ddS)Nrr rqrr)rQr@rYrjr+r5 most_commonrv) r#rrrr\r]r^rzrorrr_pprint_counters zPrettyPrinter._pprint_counterc Cst|js|t|dS|j}||jd|t|jd7}t|jD]2\}} |t|jdkrF|| |||d|||dq'|| ||d|||dd|q'dS)Nrhr rirrq)rQmapsr@rYrjr+rr?) r#rrrr\r]r^rzrmrrr_pprint_chain_maps  zPrettyPrinter._pprint_chain_mapc Cst|s |t|dS|j}||jd|t|jd7}|d|jdur>|||||d|||ddS||||d||||j||}|dd||fdS)Nrhr r|rz])z], %smaxlen=%s)rq)rQr@rYrjr+maxlenr~rP) r#rrrr\r]r^rzrmlrrr _pprint_deques"   zPrettyPrinter._pprint_dequec C||j|||||ddSNr r?datarrrr_pprint_user_dictzPrettyPrinter._pprint_user_dictc Crrrrrrr_pprint_user_listrzPrettyPrinter._pprint_user_listc Crrrrrrr_pprint_user_string!rz!PrettyPrinter._pprint_user_stringcCst|}|tvrt|ddfSt|dd}t|tr0|tjur0|jr)|dddfSt|ddfSt|tr|tjur|s>dSt |}|rO||krOdd||vfS||vrZt |ddfSd||<d}d} g} | j } |d7}|j rwt |td} n|} | D].\} }|| |||\}}}|||||\}}}| d ||f|o|o|}|s|rd} q}||=d d | || fSt|tr|tjust|trF|tjurFt|tr|sd Sd }nt|dkrd}n|sdSd}t |}|r||kr|dd||vfS||vr t |ddfSd||<d}d} g} | j } |d7}|D]}|||||\}}}| ||s2d}|r7d} q||=|d | || fSt|}||oS|d dfS)NTFrT_d)z{}TFz{...}r rrz%s: %sz{%s}r)z[]TFz[%s]z(%s,))z()TFz(%s)r<)r'_builtin_scalarsrYrd issubclassr2rTr=dictr(rMrr<ruror1rGjoinrytuplerQ startswith)r#rr]rr^rrr_rIrJ componentsrrokvkrepr kreadablekrecurvrepr vreadablevrecurrGoorepr oreadableorecurr`rrrr&s            zPrettyPrinter._safe_repr)r r NN)7r+r,r-r$rrrrr?r[rRrxrrTr{ _collections OrderedDictrryrrrr frozensetrr&rrr bytearrayrrMappingProxyTyperrrvrlr~rPrGr defaultdictrCounterrChainMaprdequerUserDictrUserListr UserStringrrrrrrjsh ,       1    )         rcCsdt|jt|fS)Nz)r'r+r(rrrrrMzsrMcCsddl}|durddddgdddfgd }t}|}||iddd |}|||}td ||td ||dS) Nrstring)r rr)iTz _safe_repr:zpformat:)timer perf_counterrrprint)rrrbt1t2t3rrr _perfchecks rccsd}t|dd}tdt|dD]*}|||d}||}||kr)||8}tt||kr;|r8t|V|}q|}q|rGt|VdSdS)Nrr)rQrangerY)rrr\rrrrrrrrrs  r__main__)Nr r N)r r Nr")$r. collectionsr dataclassesrUrsysr8typesriorrC__all__rrr rrrr r1rrr&rrfloatcomplexr:r'rrMrrr+rrrrsD