4949_type = type
5050
5151
52- def pprint (object , stream = None , indent = 1 , width = 80 , depth = None ):
52+ def pprint (object , stream = None , indent = 1 , width = 80 , depth = None , * ,
53+ compact = False ):
5354 """Pretty-print a Python object to a stream [default is sys.stdout]."""
5455 printer = PrettyPrinter (
55- stream = stream , indent = indent , width = width , depth = depth )
56+ stream = stream , indent = indent , width = width , depth = depth ,
57+ compact = compact )
5658 printer .pprint (object )
5759
58- def pformat (object , indent = 1 , width = 80 , depth = None ):
60+ def pformat (object , indent = 1 , width = 80 , depth = None , * , compact = False ):
5961 """Format a Python object into a pretty-printed representation."""
60- return PrettyPrinter (indent = indent , width = width , depth = depth ).pformat (object )
62+ return PrettyPrinter (indent = indent , width = width , depth = depth ,
63+ compact = compact ).pformat (object )
6164
6265def saferepr (object ):
6366 """Version of repr() which can handle recursive data structures."""
@@ -102,7 +105,8 @@ def _safe_tuple(t):
102105 return _safe_key (t [0 ]), _safe_key (t [1 ])
103106
104107class PrettyPrinter :
105- def __init__ (self , indent = 1 , width = 80 , depth = None , stream = None ):
108+ def __init__ (self , indent = 1 , width = 80 , depth = None , stream = None , * ,
109+ compact = False ):
106110 """Handle pretty printing operations onto a stream using a set of
107111 configured parameters.
108112
@@ -119,6 +123,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None):
119123 The desired output stream. If omitted (or false), the standard
120124 output stream available at construction will be used.
121125
126+ compact
127+ If true, several items will be combined in one line.
128+
122129 """
123130 indent = int (indent )
124131 width = int (width )
@@ -132,6 +139,7 @@ def __init__(self, indent=1, width=80, depth=None, stream=None):
132139 self ._stream = stream
133140 else :
134141 self ._stream = _sys .stdout
142+ self ._compact = bool (compact )
135143
136144 def pprint (self , object ):
137145 self ._format (object , self ._stream , 0 , 0 , {}, 0 )
@@ -223,15 +231,9 @@ def _format(self, object, stream, indent, allowance, context, level):
223231 write ((self ._indent_per_level - 1 ) * ' ' )
224232 if length :
225233 context [objid ] = 1
226- indent = indent + self ._indent_per_level
227- self ._format (object [0 ], stream , indent , allowance + 1 ,
228- context , level )
229- if length > 1 :
230- for ent in object [1 :]:
231- write (',\n ' + ' ' * indent )
232- self ._format (ent , stream , indent ,
233- allowance + 1 , context , level )
234- indent = indent - self ._indent_per_level
234+ self ._format_items (object , stream ,
235+ indent + self ._indent_per_level ,
236+ allowance + 1 , context , level )
235237 del context [objid ]
236238 if issubclass (typ , tuple ) and length == 1 :
237239 write (',' )
@@ -271,6 +273,29 @@ def _str_parts(s):
271273 return
272274 write (rep )
273275
276+ def _format_items (self , items , stream , indent , allowance , context , level ):
277+ write = stream .write
278+ delimnl = ',\n ' + ' ' * indent
279+ delim = ''
280+ width = max_width = self ._width - indent - allowance + 2
281+ for ent in items :
282+ if self ._compact :
283+ rep = self ._repr (ent , context , level )
284+ w = _len (rep ) + 2
285+ if width < w :
286+ width = max_width
287+ if delim :
288+ delim = delimnl
289+ if width >= w :
290+ width -= w
291+ write (delim )
292+ delim = ', '
293+ write (rep )
294+ continue
295+ write (delim )
296+ delim = delimnl
297+ self ._format (ent , stream , indent , allowance , context , level )
298+
274299 def _repr (self , object , context , level ):
275300 repr , readable , recursive = self .format (object , context .copy (),
276301 self ._depth , level )
0 commit comments