[G:@s&dZddlZddlZddlmZddlmZ ddddd d gZ dd d dd dddZ d d dd dddZ dd Z ddZddZGdddZddZGdd d ZddZddZdddZedkr"endS) 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) OrderedDict)StringIOpprintpformat isreadable isrecursivesaferepr PrettyPrinterPcompactFc Cs8td|d|d|d|d|}|j|dS)zAPretty-print a Python object to a stream [default is sys.stdout].streamindentwidthdepthr N)r r)objectr rrrr Zprinterr/usr/lib/python3.4/pprint.pyr.s c Cs(td|d|d|d|j|S)z= 0Nzdepth must be > 0zwidth must be != 0) intAssertionError_depth_indent_per_level_width_stream_sysstdoutbool_compact)rrrrr r rrrrfs  $      zPrettyPrinter.__init__cCs3|j||jddid|jjddS)Nr )_formatr.write)rrrrrrszPrettyPrinter.pprintcCs/t}|j||ddid|jS)Nr) _StringIOr4getvalue)rrsiorrrrs zPrettyPrinter.pformatcCs|j|idddS)Nrr)format)rrrrrrszPrettyPrinter.isrecursivecCs,|j|idd\}}}|o+| S)Nr)r9)rrsreadable recursiverrrrs!zPrettyPrinter.isreadablecCsv|d}t|}||krK|jt|d|_d|_dS|j|||d}t|} |jd||} t|| k} |j} | rht | dd} t | t rt| d|j dkr| |j ddnt|}|rfd||<||j }t | t rEt|j}nt|jdt}|d\}}|j|||}| || d |j|||t|d |d|||dkrOx||ddD]g\}}|j|||}| d d||f|j|||t|d |d||qWn||j }||=n| d dSt | tr| tjkst | tr| tjkst | tr| tjkst | trS| tjkrSt|}t | tr| d d}nt | tr<| dd}ny|sP| |dS| tkro| dd }n4| | j| dd}|t| jd7}t|dt}|j dkr| |j ddn|rd||<|j||||j |d||||=nt | trE|dkrE| dn| |dSt | trht|dkrh| tjkrhg}|jd}|dkr|d7}| d 8} nx t|D]\}}t|}t|| kr|j|qtj d|dg}d}xt!dt|d D]i}||||d}||}tt|| kr|r|jt|n|}q:|}q:W|r|jt|qqWt|dkr| |dS|dkr| dnxEt|D]7\}}|dkr:| dd|n| |q W|dkra| dndSn| |dS)Nr TF__repr__{ keyrz: rz, %s%s: }[]()z({z}),z(\s+)r3)"rr5 _recursion _recursive _readable_reprrr-lengetattr issubclassdictr, _OrderedDictlistitemssortedr(r4r=tupleset frozensetr"r _format_itemsr splitlines enumeratereprappendresplitrange)rrr r allowancecontextlevelobjidreptyp max_widthZsepLinesr5rZlengthrRr@entZendcharZchunkslinesilinepartsZcurrentpart candidaterrrr4s                                  0             zPrettyPrinter._formatcCs|j}dd|}d} |j||d} } x|D]} |jr|j| ||} t| d}| |kr| } | r|} qn| |kr| |8} || d} || q=qn|| |} |j| |||||q=WdS)Nz, r?rGrz, )r5r-r2rKrLr4)rrRr rr_r`rar5ZdelimnlZdelimrrergrcwrrrrWs*           zPrettyPrinter._format_itemscCsR|j||j|j|\}}}|s<d|_n|rNd|_n|S)NFT)r9copyr+rJrI)rrr`rarZr;r<rrrrK+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)rrr` maxlevelsrarrrr94szPrettyPrinter.format) r"r#r$rrrrrr4rWrKr9rrrrr es #     z  c!Cs6t|}|tkrdtjkr:t|ddfSd|krhd|krhd}idd6}nd}idd6}|j}t}|j} xJ|D]B} | jr| | q| || t| ddqWd ||j |fddfSt |d d} t |t r| t j kr|s=dSt|} |rn||krnd d| |kfS| |krt|ddfSd|| s         '                    rcCsdt|jt|fS)Nz)rr"r)rrrrrHsrHcCsddl}|dkrHddddgidd6d d 6fgd }nt}|j}t|idd|j}|j||j}td ||td ||dS)Nrstringr riz _safe_repr:zpformat:)r r)timer rrprint)rrpZt1Zt2Zt3rrr _perfchecks  0     r__main__)r%r\sysr/ collectionsrrPiorr6__all__rrrrrrr(r rrHrr"rrrr#s(        U