o ?Og|_ @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 rrZprinterr-/opt/alt/python310/lib64/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 )_formatr6writer"rrrrrszPrettyPrinter.pprintcCs"t}|||ddid|SNr) _StringIOr>getvalue)r"rZsiorrrrszPrettyPrinter.pformatcCs||idddS)Nrrformatr@rrrrszPrettyPrinter.isrecursivecCs ||idd\}}}|o| SrArD)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__Z __create_fn__)r'r? _recursion _recursive _readable_reprr5len _dispatchgetr&__repr__ _dataclassesZ is_dataclass isinstanceZ__dataclass_params__reprhasattrrIr,_pprint_dataclass) r"rrr allowancecontextlevelobjidrep max_widthprrrr>s8   zPrettyPrinter._formatc s`jj}|t|d7}fddtD}||d||||||||ddS)Nr cs$g|]}|jr|jt|jfqSr)rTnamegetattr).0frrr s$z3PrettyPrinter._pprint_dataclass..()) __class__r*rNrRZfieldsr?_format_namespace_items r"rrrrWrXrYcls_nameitemsrrrrVs zPrettyPrinter._pprint_dataclassc Csz|j}|d|jdkr||jddt|}|r7|jr't|td} n|} || |||d|||ddS)N{r  key})r?r4rNr;sortedrir0_format_dict_items) r"rrrrWrXrYr?Zlengthrirrr _pprint_dicts  zPrettyPrinter._pprint_dictcCslt|s |t|dS|j}||jd|t|||t|jd|d|||ddS)Nrcr rd)rNr?rTrer*r>listri)r"rrrrWrXrYclsrrr_pprint_ordered_dictsz"PrettyPrinter._pprint_ordered_dictcCs0|d|||||d|||ddS)N[r ])r? _format_itemsr"rrrrWrXrYrrr _pprint_lists zPrettyPrinter._pprint_listcCsH|dt|dkr dnd}|||||t|||||dS)Nrcr z,)rd)r?rNrw)r"rrrrWrXrYendcharrrr _pprint_tuples zPrettyPrinter._pprint_tuplec Cst|s |t|dS|j}|tur|dd}n||jdd}|t|jd7}t|td}|||||t|||||dS)Nrjrn({})r rl) rNr?rTresetr*rorrw) r"rrrrWrXrYtyprzrrr _pprint_sets  zPrettyPrinter._pprint_setcCs|j}t|s|t|dSg}|d} |dkr"|d7}|d7}|j|} } t| D]n\} } t| }| t| dkrA| |8} t|| krM||q-td| }| | }d}t|D]2\}}||}|t|dkr{| t| dkr{||8}tt||kr|r|t||}q_|}q_|r|t|q-t|dkr||dS|dkr|dt|D]\} }| dkr|dd|||q|dkr|d dSdS) NTr z\S*\s*rcrr=rkrd) r?rNrT splitlinesr5 enumerateappendrefindallpop)r"rrrrWrXrYr?ZchunkslinesZ max_width1r\iliner[partsZ max_width2currentjpart candidaterrr _pprint_strsX         zPrettyPrinter._pprint_strc Cs|j}t|dkr|t|dS|dk}|r#|d7}|d7}|dd} t||j||D]} || || | s@dd|} q.|rI|ddSdS)Nr rcrr=rkrd)r?rNrT_wrap_bytes_reprr5) r"rrrrWrXrYr?Zparensdelimr[rrr _pprint_bytesCs&    zPrettyPrinter._pprint_bytesc Cs>|j}|d|t|||d|d||d|ddS)Nz bytearray( r rd)r?rbytes)r"rrrrWrXrYr?rrr_pprint_bytearrayXs  zPrettyPrinter._pprint_bytearraycCs8|d||||d|d|||ddS)Nz mappingproxy( r rd)r?r>copyrxrrr_pprint_mappingproxyas z"PrettyPrinter._pprint_mappingproxyc Csft|tjur d}n|jj}|t|d7}|j}||d| |||||||ddS)N namespacer rcrd) r&_typesSimpleNamespacerer*rN__dict__rir?rfrgrrr_pprint_simplenamespaceis z%PrettyPrinter._pprint_simplenamespacec Cs|j}||j7}dd|}t|d} t|D]3\} \} } | | k} || ||}|||d|| ||t|d| r@|nd||| sK||qdS)N, rkr z: r)r?r4rNrrMr>)r"rirrrWrXrYr?delimnl last_indexrrmentlastr[rrrrpxs"    z PrettyPrinter._format_dict_itemsc Cs|j}dd|}t|d} t|D]7\} \} } | | k} || |dt| |vr0|dn|| ||t| d| r?|nd||| sJ||qdS)Nrrkr =...)r?rNrr'r>)r"rirrrWrXrYr?rrrrmrrrrrrfs"     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 rkrrFTr, ) r?r4r5iternext StopIterationr:rMrNr>)r"rirrrWrXrYr?rrrr\itZnext_entrrr[wrrrrwsV            zPrettyPrinter._format_itemscCs4||||j|\}}}|sd|_|rd|_|S)NFT)rErr3rLrK)r"rrXrYrTrGrHrrrrMs  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"rrX maxlevelsrYrrrrEszPrettyPrinter.formatc Cst|s |t|dS||j||}|j}|t|jd7}|d|j|d|f|||||d|||ddS)Nr z %s(%s, %srkrd)rNr?rTrMdefault_factoryrer*rq) r"rrrrWrXrYZrdfrsrrr_pprint_default_dictsz"PrettyPrinter._pprint_default_dictc Cst|s |t|dS|j}||jd|jdkr'||jdd|}||||t|jd|d|||ddS)Nr|r rkrr})rNr?rTrer*r4 most_commonrp) r"rrrrWrXrYrsrirrr_pprint_counters zPrettyPrinter._pprint_counterc Cst|js|t|dS|j}||jd|t|jd7}t|jD]2\}} |t|jdkrF|| |||d|||dq'|| ||d|||dd|q'dS)Nrcr rdrrk)rNmapsr?rTrer*rr>) r"rrrrWrXrYrsrmrrr_pprint_chain_maps  zPrettyPrinter._pprint_chain_mapc Cst|s |t|dS|j}||jd|t|jd7}|d|jdur>|||||d|||ddS||||d||||j||}|dd||fdS)Nrcr rurz])z], %smaxlen=%s)rk)rNr?rTrer*maxlenrwrM) r"rrrrWrXrYrsZrmlrrr _pprint_deques"   zPrettyPrinter._pprint_dequec C||j|||||ddSNr r>datarxrrr_pprint_user_dictzPrettyPrinter._pprint_user_dictc Crrrrxrrr_pprint_user_listrzPrettyPrinter._pprint_user_listc Crrrrxrrr_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)NTFrQZ_d)z{}TFz{...}r rlz%s: %sz{%s}r)z[]TFz[%s]z(%s,))z()TFz(%s)r<)r&_builtin_scalarsrTr_ issubclassr1rQr<dictr'rJrr;rorir0rEjoinrrtuplerN startswith)r"rrXrrYrrrZrGrHZ componentsrrikvZkreprZ kreadableZkrecurZvreprZ vreadableZvrecurrEoZoreprZ oreadableZorecurr[rrrr&s            zPrettyPrinter._safe_repr)r r NN)7r*r+r,r#rrrrr>rVrOrqrrQrt _collections OrderedDictryrrr{rrr~ frozensetrr%rrr bytearrayrrMappingProxyTyperrrprfrwrMrEr defaultdictrCounterrChainMaprdequerUserDictrUserListr UserStringrrrrrrjsh ,       1    )         rcCsdt|jt|fS)Nz)r&r*r'rrrrrJzsrJcCsddl}|durddddgdddfgd }t}|}||iddd |}|||}td ||td ||dS) Nrstring)r rr)iTz _safe_repr:zpformat:)timer perf_counterrrprint)rrr]t1t2Zt3rrr _perfchecks rccsd}t|dd}tdt|dD]*}|||d}||}||kr)||8}tt||kr;|r8t|V|}q|}q|rGt|VdSdS)Nrr)rNrangerT)rrrWrrrrrrrrrs  r__main__)Nr r N)r r Nr!)$r- collectionsrZ dataclassesrRrsysr7typesriorrB__all__rrr rrrrr0rrr%rrfloatcomplexr9r&rrJrrr*rrrrsD