<s> """<STR_LIT>""" <EOL> import sys as _sys <EOL> from collections import OrderedDict as _OrderedDict <EOL> from io import StringIO as _StringIO <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> _commajoin = "<STR_LIT:U+002CU+0020>" . join <EOL> _id = id <EOL> _len = len <EOL> _type = type <EOL> def pprint ( object , stream = None , indent = <NUM_LIT:1> , width = <NUM_LIT> , depth = None ) : <EOL> """<STR_LIT>""" <EOL> printer = PrettyPrinter ( <EOL> stream = stream , indent = indent , width = width , depth = depth ) <EOL> printer . pprint ( object ) <EOL> def pformat ( object , indent = <NUM_LIT:1> , width = <NUM_LIT> , depth = None ) : <EOL> """<STR_LIT>""" <EOL> return PrettyPrinter ( indent = indent , width = width , depth = depth ) . pformat ( object ) <EOL> def saferepr ( object ) : <EOL> """<STR_LIT>""" <EOL> return _safe_repr ( object , { } , None , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> def isreadable ( object ) : <EOL> """<STR_LIT>""" <EOL> return _safe_repr ( object , { } , None , <NUM_LIT:0> ) [ <NUM_LIT:1> ] <EOL> def isrecursive ( object ) : <EOL> """<STR_LIT>""" <EOL> return _safe_repr ( object , { } , None , <NUM_LIT:0> ) [ <NUM_LIT:2> ] <EOL> class _safe_key : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def __init__ ( self , obj ) : <EOL> self . obj = obj <EOL> def __lt__ ( self , other ) : <EOL> try : <EOL> rv = self . obj . __lt__ ( other . obj ) <EOL> except TypeError : <EOL> rv = NotImplemented <EOL> if rv is NotImplemented : <EOL> rv = ( str ( type ( self . obj ) ) , id ( self . obj ) ) < ( str ( type ( other . obj ) ) , id ( other . obj ) ) <EOL> return rv <EOL> def _safe_tuple ( t ) : <EOL> "<STR_LIT>" <EOL> return _safe_key ( t [ <NUM_LIT:0> ] ) , _safe_key ( t [ <NUM_LIT:1> ] ) <EOL> class PrettyPrinter : <EOL> def __init__ ( self , indent = <NUM_LIT:1> , width = <NUM_LIT> , depth = None , stream = None ) : <EOL> """<STR_LIT>""" <EOL> indent = int ( indent ) <EOL> width = int ( width ) <EOL> assert indent >= <NUM_LIT:0> , "<STR_LIT>" <EOL> assert depth is None or depth > <NUM_LIT:0> , "<STR_LIT>" <EOL> assert width , "<STR_LIT>" <EOL> self . _depth = depth <EOL> self . _indent_per_level = indent <EOL> self . _width = width <EOL> if stream is not None : <EOL> self . _stream = stream <EOL> else : <EOL> self . _stream = _sys . stdout <EOL> def pprint ( self , object ) : <EOL> self . _format ( object , self . _stream , <NUM_LIT:0> , <NUM_LIT:0> , { } , <NUM_LIT:0> ) <EOL> self . _stream . write ( "<STR_LIT:\n>" ) <EOL> def pformat ( self , object ) : <EOL> sio = _StringIO ( ) <EOL> self . _format ( object , sio , <NUM_LIT:0> , <NUM_LIT:0> , { } , <NUM_LIT:0> ) <EOL> return sio . getvalue ( ) <EOL> def isrecursive ( self , object ) : <EOL> return self . format ( object , { } , <NUM_LIT:0> , <NUM_LIT:0> ) [ <NUM_LIT:2> ] <EOL> def isreadable ( self , object ) : <EOL> s , readable , recursive = self . format ( object , { } , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> return readable and not recursive <EOL> def _format ( self , object , stream , indent , allowance , context , level ) : <EOL> level = level + <NUM_LIT:1> <EOL> import sys <EOL> sys . stderr . write ( str ( object ) ) <EOL> objid = _id ( object ) <EOL> if objid in context : <EOL> stream . write ( _recursion ( object ) ) <EOL> self . _recursive = True <EOL> self . _readable = False <EOL> return <EOL> rep = self . _repr ( object , context , level - <NUM_LIT:1> ) <EOL> typ = _type ( object ) <EOL> sepLines = _len ( rep ) > ( self . _width - <NUM_LIT:1> - indent - allowance ) <EOL> write = stream . write <EOL> if self . _depth and level > self . _depth : <EOL> write ( rep ) <EOL> return <EOL> if sepLines : <EOL> r = getattr ( typ , "<STR_LIT>" , None ) <EOL> if issubclass ( typ , dict ) : <EOL> write ( '<STR_LIT:{>' ) <EOL> if self . _indent_per_level > <NUM_LIT:1> : <EOL> write ( ( self . _indent_per_level - <NUM_LIT:1> ) * '<STR_LIT:U+0020>' ) <EOL> length = _len ( object ) <EOL> if length : <EOL> context [ objid ] = <NUM_LIT:1> <EOL> indent = indent + self . _indent_per_level <EOL> if issubclass ( typ , _OrderedDict ) : <EOL> items = list ( object . items ( ) ) <EOL> else : <EOL> items = sorted ( object . items ( ) , key = _safe_tuple ) <EOL> key , ent = items [ <NUM_LIT:0> ] <EOL> rep = self . _repr ( key , context , level ) <EOL> write ( rep ) <EOL> write ( '<STR_LIT>' ) <EOL> self . _format ( ent , stream , indent + _len ( rep ) + <NUM_LIT:2> , <EOL> allowance + <NUM_LIT:1> , context , level ) <EOL> if length > <NUM_LIT:1> : <EOL> for key , ent in items [ <NUM_LIT:1> : ] : <EOL> rep = self . _repr ( key , context , level ) <EOL> write ( '<STR_LIT>' % ( '<STR_LIT:U+0020>' * indent , rep ) ) <EOL> self . _format ( ent , stream , indent + _len ( rep ) + <NUM_LIT:2> , <EOL> allowance + <NUM_LIT:1> , context , level ) <EOL> indent = indent - self . _indent_per_level <EOL> del context [ objid ] <EOL> write ( '<STR_LIT:}>' ) <EOL> return <EOL> if ( ( issubclass ( typ , list ) and r is list . __repr__ ) or <EOL> ( issubclass ( typ , tuple ) and r is tuple . __repr__ ) or <EOL> ( issubclass ( typ , set ) and r is set . __repr__ ) or <EOL> ( issubclass ( typ , frozenset ) and r is frozenset . __repr__ ) <EOL> ) : <EOL> length = _len ( object ) <EOL> if issubclass ( typ , list ) : <EOL> write ( '<STR_LIT:[>' ) <EOL> endchar = '<STR_LIT:]>' <EOL> elif issubclass ( typ , tuple ) : <EOL> write ( '<STR_LIT:(>' ) <EOL> endchar = '<STR_LIT:)>' <EOL> else : <EOL> if not length : <EOL> write ( rep ) <EOL> return <EOL> if typ is set : <EOL> write ( '<STR_LIT:{>' ) <EOL> endchar = '<STR_LIT:}>' <EOL> else : <EOL> write ( typ . __name__ ) <EOL> write ( '<STR_LIT>' ) <EOL> endchar = '<STR_LIT>' <EOL> indent += len ( typ . __name__ ) + <NUM_LIT:1> <EOL> object = sorted ( object , key = _safe_key ) <EOL> if self . _indent_per_level > <NUM_LIT:1> : <EOL> write ( ( self . _indent_per_level - <NUM_LIT:1> ) * '<STR_LIT:U+0020>' ) <EOL> if length : <EOL> context [ objid ] = <NUM_LIT:1> <EOL> indent = indent + self . _indent_per_level <EOL> self . _format ( object [ <NUM_LIT:0> ] , stream , indent , allowance + <NUM_LIT:1> , <EOL> context , level ) <EOL> if length > <NUM_LIT:1> : <EOL> for ent in object [ <NUM_LIT:1> : ] : <EOL> write ( '<STR_LIT>' + '<STR_LIT:U+0020>' * indent ) <EOL> self . _format ( ent , stream , indent , <EOL> allowance + <NUM_LIT:1> , context , level ) <EOL> indent = indent - self . _indent_per_level <EOL> del context [ objid ] <EOL> if issubclass ( typ , tuple ) and length == <NUM_LIT:1> : <EOL> write ( '<STR_LIT:U+002C>' ) <EOL> write ( endchar ) <EOL> return <EOL> write ( rep ) <EOL> def _repr ( self , object , context , level ) : <EOL> repr , readable , recursive = self . format ( object , context . copy ( ) , <EOL> self . _depth , level ) <EOL> if not readable : <EOL> self . _readable = False <EOL> if recursive : <EOL> self . _recursive = True <EOL> return repr <EOL> def format ( self , object , context , maxlevels , level ) : <EOL> """<STR_LIT>""" <EOL> return _safe_repr ( object , context , maxlevels , level ) <EOL> def _safe_repr ( object , context , maxlevels , level ) : <EOL> typ = _type ( object ) <EOL> if typ is str : <EOL> if '<STR_LIT>' not in _sys . modules : <EOL> return repr ( object ) , True , False <EOL> if "<STR_LIT:'>" in object and '<STR_LIT:">' not in object : <EOL> closure = '<STR_LIT:">' <EOL> quotes = { '<STR_LIT:">' : '<STR_LIT>' } <EOL> else : <EOL> closure = "<STR_LIT:'>" <EOL> quotes = { "<STR_LIT:'>" : "<STR_LIT>" } <EOL> qget = quotes . get <EOL> sio = _StringIO ( ) <EOL> write = sio . write <EOL> for char in object : <EOL> if char . isalpha ( ) : <EOL> write ( char ) <EOL> else : <EOL> write ( qget ( char , repr ( char ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) ) <EOL> return ( "<STR_LIT>" % ( closure , sio . getvalue ( ) , closure ) ) , True , False <EOL> r = getattr ( typ , "<STR_LIT>" , None ) <EOL> if issubclass ( typ , dict ) and r is dict . __repr__ : <EOL> if not object : <EOL> return "<STR_LIT:{}>" , True , False <EOL> objid = _id ( object ) <EOL> if maxlevels and level >= maxlevels : <EOL> return "<STR_LIT>" , False , objid in context <EOL> if objid in context : <EOL> return _recursion ( object ) , False , True <EOL> context [ objid ] = <NUM_LIT:1> <EOL> readable = True <EOL> recursive = False <EOL> components = [ ] <EOL> append = components . append <EOL> level += <NUM_LIT:1> <EOL> saferepr = _safe_repr <EOL> items = sorted ( object . items ( ) , key = _safe_tuple ) <EOL> for k , v in items : <EOL> krepr , kreadable , krecur = saferepr ( k , context , maxlevels , level ) <EOL> vrepr , vreadable , vrecur = saferepr ( v , context , maxlevels , level ) <EOL> append ( "<STR_LIT>" % ( krepr , vrepr ) ) <EOL> readable = readable and kreadable and vreadable <EOL> if krecur or vrecur : <EOL> recursive = True <EOL> del context [ objid ] <EOL> return "<STR_LIT>" % _commajoin ( components ) , readable , recursive <EOL> if ( issubclass ( typ , list ) and r is list . __repr__ ) or ( issubclass ( typ , tuple ) and r is tuple . __repr__ ) : <EOL> if issubclass ( typ , list ) : <EOL> if not object : <EOL> return "<STR_LIT>" , True , False <EOL> format = "<STR_LIT>" <EOL> elif _len ( object ) == <NUM_LIT:1> : <EOL> format = "<STR_LIT>" <EOL> else : <EOL> if not object : <EOL> return "<STR_LIT>" , True , False <EOL> format = "<STR_LIT>" <EOL> objid = _id ( object ) <EOL> if maxlevels and level >= maxlevels : <EOL> return format % "<STR_LIT>" , False , objid in context <EOL> if objid in context : <EOL> return _recursion ( object ) , False , True <EOL> context [ objid ] = <NUM_LIT:1> <EOL> readable = True <EOL> recursive = False <EOL> components = [ ] <EOL> append = components . append <EOL> level += <NUM_LIT:1> <EOL> for o in object : <EOL> orepr , oreadable , orecur = _safe_repr ( o , context , maxlevels , level ) <EOL> append ( orepr ) <EOL> if not oreadable : <EOL> readable = False <EOL> if orecur : <EOL> recursive = True <EOL> del context [ objid ] <EOL> return format % _commajoin ( components ) , readable , recursive <EOL> rep = repr ( object ) <EOL> return rep , ( rep and not rep . startswith ( '<STR_LIT:<>' ) ) , False <EOL> def _recursion ( object ) : <EOL> return ( "<STR_LIT>" <EOL> % ( _type ( object ) . __name__ , _id ( object ) ) ) <EOL> def _perfcheck ( object = None ) : <EOL> import time <EOL> if object is None : <EOL> object = [ ( "<STR_LIT:string>" , ( <NUM_LIT:1> , <NUM_LIT:2> ) , [ <NUM_LIT:3> , <NUM_LIT:4> ] , { <NUM_LIT:5> : <NUM_LIT:6> , <NUM_LIT:7> : <NUM_LIT:8> } ) ] * <NUM_LIT> <EOL> p = PrettyPrinter ( ) <EOL> t1 = time . time ( ) <EOL> _safe_repr ( object , { } , None , <NUM_LIT:0> ) <EOL> t2 = time . time ( ) <EOL> p . pformat ( object ) <EOL> t3 = time . time ( ) <EOL> print ( "<STR_LIT>" , t2 - t1 ) <EOL> print ( "<STR_LIT>" , t3 - t2 ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> _perfcheck ( ) </s>
