o Ƌ|c|_ @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/conda/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 )_formatr6writer"rrrrrszPrettyPrinter.pprintcCs"t}|||ddid|SNr) _StringIOr>getvalue)r"rsiorrrrszPrettyPrinter.pformatcCs||idddS)Nrrformatr@rrrrszPrettyPrinter.isrecursivecCs ||idd\}}}|o| SrArE)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__reprhasattrrJr,_pprint_dataclass) r"rrr allowancecontextlevelobjidrep max_widthprrrr>s8   zPrettyPrinter._formatc s`jj}|t|d7}fddtD}||d||||||||ddS)Nr cs$g|]}|jr|jt|jfqSr)rUnamegetattr).0frrr s$z3PrettyPrinter._pprint_dataclass..()) __class__r*rOrSfieldsr?_format_namespace_items r"rrrrXrYrZcls_nameitemsrrrrWs zPrettyPrinter._pprint_dataclassc Csz|j}|d|jdkr||jddt|}|r7|jr't|td} n|} || |||d|||ddS)N{r  key})r?r4rOr;sortedrkr0_format_dict_items) r"rrrrXrYrZr?lengthrkrrr _pprint_dicts  zPrettyPrinter._pprint_dictcCslt|s |t|dS|j}||jd|t|||t|jd|d|||ddS)Nrdr re)rOr?rUrfr*r>listrk)r"rrrrXrYrZclsrrr_pprint_ordered_dictsz"PrettyPrinter._pprint_ordered_dictcCs0|d|||||d|||ddS)N[r ])r? _format_itemsr"rrrrXrYrZrrr _pprint_lists zPrettyPrinter._pprint_listcCsH|dt|dkr dnd}|||||t|||||dS)Nrdr z,)re)r?rOrz)r"rrrrXrYrZendcharrrr _pprint_tuples zPrettyPrinter._pprint_tuplec Cst|s |t|dS|j}|tur|dd}n||jdd}|t|jd7}t|td}|||||t|||||dS)Nrlrp({})r rn) rOr?rUrfsetr*rqrrz) r"rrrrXrYrZtypr}rrr _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*rdrr=rmre) r?rOrU splitlinesr5 enumerateappendrefindallpop)r"rrrrXrYrZr?chunkslinesZ max_width1r]iliner\partsZ 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 rdrr=rmre)r?rOrU_wrap_bytes_reprr5) r"rrrrXrYrZr?Zparensdelimr\rrr _pprint_bytesCs&    zPrettyPrinter._pprint_bytesc Cs>|j}|d|t|||d|d||d|ddS)Nz bytearray( r re)r?rbytes)r"rrrrXrYrZr?rrr_pprint_bytearrayXs  zPrettyPrinter._pprint_bytearraycCs8|d||||d|d|||ddS)Nz mappingproxy( r re)r?r>copyr{rrr_pprint_mappingproxyas z"PrettyPrinter._pprint_mappingproxyc Csft|tjur d}n|jj}|t|d7}|j}||d| |||||||ddS)N namespacer rdre) r&_typesSimpleNamespacerfr*rO__dict__rkr?rhrirrr_pprint_simplenamespaceis z%PrettyPrinter._pprint_simplenamespacec Cs|j}||j7}dd|}t|d} t|D]3\} \} } | | k} || ||}|||d|| ||t|d| r@|nd||| sK||qdS)N, rmr z: r)r?r4rOrrNr>)r"rkrrrXrYrZr?delimnl last_indexrroentlastr\rrrrrxs"    z PrettyPrinter._format_dict_itemsc Cs|j}dd|}t|d} t|D]7\} \} } | | k} || |dt| |vr0|dn|| ||t| d| r?|nd||| sJ||qdS)Nrrmr =...)r?rOrr'r>)r"rkrrrXrYrZr?rrrrorrrrrrhs"     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 rmrrFTr, ) r?r4r5iternext StopIterationr:rNrOr>)r"rkrrrXrYrZr?rrrr]itZnext_entrrr\wrrrrzsV            zPrettyPrinter._format_itemscCs4||||j|\}}}|sd|_|rd|_|S)NFT)rFrr3rMrL)r"rrYrZrUrHrIrrrrNs  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"rrY maxlevelsrZrrrrFszPrettyPrinter.formatc Cst|s |t|dS||j||}|j}|t|jd7}|d|j|d|f|||||d|||ddS)Nr z %s(%s, %srmre)rOr?rUrNdefault_factoryrfr*rt) r"rrrrXrYrZZrdfrvrrr_pprint_default_dictsz"PrettyPrinter._pprint_default_dictc Cst|s |t|dS|j}||jd|jdkr'||jdd|}||||t|jd|d|||ddS)Nrr rmrr)rOr?rUrfr*r4 most_commonrr) r"rrrrXrYrZrvrkrrr_pprint_counters zPrettyPrinter._pprint_counterc Cst|js|t|dS|j}||jd|t|jd7}t|jD]2\}} |t|jdkrF|| |||d|||dq'|| ||d|||dd|q'dS)Nrdr rerrm)rOmapsr?rUrfr*rr>) r"rrrrXrYrZrvrmrrr_pprint_chain_maps  zPrettyPrinter._pprint_chain_mapc Cst|s |t|dS|j}||jd|t|jd7}|d|jdur>|||||d|||ddS||||d||||j||}|dd||fdS)Nrdr rxrz])z], %smaxlen=%s)rm)rOr?rUrfr*maxlenrzrN) r"rrrrXrYrZrvZrmlrrr _pprint_deques"   zPrettyPrinter._pprint_dequec C||j|||||ddSNr r>datar{rrr_pprint_user_dictzPrettyPrinter._pprint_user_dictc Crrrr{rrr_pprint_user_listrzPrettyPrinter._pprint_user_listc Crrrr{rrr_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)NTFrRZ_d)z{}TFz{...}r rnz%s: %sz{%s}r)z[]TFz[%s]z(%s,))z()TFz(%s)r<)r&_builtin_scalarsrUr` issubclassr1rRr<dictr'rKrr;rqrkr0rFjoinrutuplerO startswith)r"rrYrrZrrr[rHrI componentsrrkkvZkreprZ kreadableZkrecurZvreprZ vreadableZvrecurrFoZoreprZ oreadableZorecurr\rrrr&s            zPrettyPrinter._safe_repr)r r NN)7r*r+r,r#rrrrr>rWrPrtrrRrw _collections OrderedDictr|rur~rrr frozensetrr%rrr bytearrayrrMappingProxyTyperrrrrhrzrNrFr defaultdictrCounterrChainMaprdequerUserDictrUserListr UserStringrrrrrrjsh ,       1    )         rcCsdt|jt|fS)Nz)r&r*r'rrrrrKzsrKcCsddl}|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)rOrangerU)rrrXrrrrrrrrrs  r__main__)Nr r N)r r Nr!)$r- collectionsrZ dataclassesrSrsysr7typesriorrB__all__rrr rrrrr0rrr%rrfloatcomplexr9r&rrKrrr*rrrrsD