4444
4545
4646def pprint (object , stream = None , indent = 1 , width = 80 , depth = None , * ,
47- block_style = False , compact = False , sort_dicts = True ,
47+ compact = False , expand = False , sort_dicts = True ,
4848 underscore_numbers = False ):
4949 """Pretty-print a Python object to a stream [default is sys.stdout]."""
5050 printer = PrettyPrinter (
5151 stream = stream , indent = indent , width = width , depth = depth ,
52- compact = compact , sort_dicts = sort_dicts ,
53- underscore_numbers = underscore_numbers , block_style = block_style )
52+ compact = compact , expand = expand , sort_dicts = sort_dicts ,
53+ underscore_numbers = underscore_numbers )
5454 printer .pprint (object )
5555
5656
5757def pformat (object , indent = 1 , width = 80 , depth = None , * ,
58- block_style = False , compact = False , sort_dicts = True ,
58+ compact = False , expand = False , sort_dicts = True ,
5959 underscore_numbers = False ):
6060 """Format a Python object into a pretty-printed representation."""
6161 return PrettyPrinter (indent = indent , width = width , depth = depth ,
62- compact = compact , sort_dicts = sort_dicts ,
63- underscore_numbers = underscore_numbers ,
64- block_style = block_style ).pformat (object )
62+ compact = compact , expand = expand , sort_dicts = sort_dicts ,
63+ underscore_numbers = underscore_numbers ).pformat (object )
6564
6665
6766def pp (object , * args , sort_dicts = False , ** kwargs ):
@@ -114,7 +113,7 @@ def _safe_tuple(t):
114113
115114class PrettyPrinter :
116115 def __init__ (self , indent = 1 , width = 80 , depth = None , stream = None , * ,
117- block_style = False , compact = False , sort_dicts = True ,
116+ compact = False , expand = False , sort_dicts = True ,
118117 underscore_numbers = False ):
119118 """Handle pretty printing operations onto a stream using a set of
120119 configured parameters.
@@ -134,18 +133,19 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
134133
135134 compact
136135 If true, several items will be combined in one line.
136+ Incompatible with expand mode.
137+
138+ expand
139+ If true, the output will be formatted in a block style similar to
140+ pretty-printed json.dumps() when ``indent`` is supplied.
141+ Incompatible with compact mode.
137142
138143 sort_dicts
139144 If true, dict keys are sorted.
140145
141146 underscore_numbers
142147 If true, digit groups are separated with underscores.
143148
144- block_style
145- If true, the output will be formatted in a block style similar to
146- pretty-printed json.dumps() when ``indent`` is supplied.
147- Incompatible with compact mode.
148-
149149 """
150150 indent = int (indent )
151151 width = int (width )
@@ -155,8 +155,8 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
155155 raise ValueError ('depth must be > 0' )
156156 if not width :
157157 raise ValueError ('width must be != 0' )
158- if compact and block_style :
159- raise ValueError ('compact and block_style are incompatible' )
158+ if compact and expand :
159+ raise ValueError ('compact and expand are incompatible' )
160160 self ._depth = depth
161161 self ._indent_per_level = indent
162162 self ._width = width
@@ -165,9 +165,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
165165 else :
166166 self ._stream = _sys .stdout
167167 self ._compact = bool (compact )
168+ self ._expand = bool (expand )
168169 self ._sort_dicts = sort_dicts
169170 self ._underscore_numbers = underscore_numbers
170- self ._block_style = bool (block_style )
171171
172172 def pprint (self , object ):
173173 if self ._stream is not None :
@@ -218,12 +218,12 @@ def _format(self, object, stream, indent, allowance, context, level):
218218 stream .write (rep )
219219
220220 def _format_block_start (self , start_str , indent ):
221- if self ._block_style :
221+ if self ._expand :
222222 return f"{ start_str } \n { ' ' * indent } "
223223 return start_str
224224
225225 def _format_block_end (self , end_str , indent ):
226- if self ._block_style :
226+ if self ._expand :
227227 return f"\n { ' ' * indent } { end_str } "
228228 return end_str
229229
@@ -232,7 +232,7 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
232232 from dataclasses import fields as dataclass_fields
233233
234234 cls_name = object .__class__ .__name__
235- if self ._block_style :
235+ if self ._expand :
236236 indent += self ._indent_per_level
237237 else :
238238 indent += len (cls_name ) + 1
@@ -246,9 +246,9 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
246246 def _pprint_dict (self , object , stream , indent , allowance , context , level ):
247247 write = stream .write
248248 write (self ._format_block_start ('{' , indent ))
249- if self ._indent_per_level > 1 and not self ._block_style :
249+ if self ._indent_per_level > 1 and not self ._expand :
250250 write ((self ._indent_per_level - 1 ) * ' ' )
251- if self ._indent_per_level > 0 and self ._block_style :
251+ if self ._indent_per_level > 0 and self ._expand :
252252 write (self ._indent_per_level * ' ' )
253253 length = len (object )
254254 if length :
@@ -268,7 +268,7 @@ def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level
268268 return
269269 cls = object .__class__
270270 stream .write (cls .__name__ + '(' )
271- if self ._block_style :
271+ if self ._expand :
272272 recursive_indent = indent
273273 else :
274274 recursive_indent = indent + len (cls .__name__ ) + 1
@@ -349,7 +349,7 @@ def _pprint_set(self, object, stream, indent, allowance, context, level):
349349 else :
350350 stream .write (self ._format_block_start (typ .__name__ + '({' , indent ))
351351 endchar = '})'
352- if not self ._block_style :
352+ if not self ._expand :
353353 indent += len (typ .__name__ ) + 1
354354 object = sorted (object , key = _safe_key )
355355 self ._format_items (object , stream , indent , allowance + len (endchar ),
@@ -420,7 +420,7 @@ def _pprint_bytes(self, object, stream, indent, allowance, context, level):
420420 return
421421 parens = level == 1
422422 if parens :
423- if self ._block_style :
423+ if self ._expand :
424424 indent += self ._indent_per_level
425425 else :
426426 indent += 1
@@ -440,7 +440,7 @@ def _pprint_bytes(self, object, stream, indent, allowance, context, level):
440440 def _pprint_bytearray (self , object , stream , indent , allowance , context , level ):
441441 write = stream .write
442442 write (self ._format_block_start ('bytearray(' , indent ))
443- if self ._block_style :
443+ if self ._expand :
444444 write (' ' * self ._indent_per_level )
445445 recursive_indent = indent + self ._indent_per_level
446446 else :
@@ -453,7 +453,7 @@ def _pprint_bytearray(self, object, stream, indent, allowance, context, level):
453453
454454 def _pprint_mappingproxy (self , object , stream , indent , allowance , context , level ):
455455 stream .write ('mappingproxy(' )
456- if self ._block_style :
456+ if self ._expand :
457457 recursive_indent = indent
458458 else :
459459 recursive_indent = indent + 13
@@ -470,7 +470,7 @@ def _pprint_simplenamespace(self, object, stream, indent, allowance, context, le
470470 cls_name = 'namespace'
471471 else :
472472 cls_name = object .__class__ .__name__
473- if self ._block_style :
473+ if self ._expand :
474474 indent += self ._indent_per_level
475475 else :
476476 indent += len (cls_name ) + 1
@@ -493,7 +493,7 @@ def _format_dict_items(self, items, stream, indent, allowance, context,
493493 rep = self ._repr (key , context , level )
494494 write (rep )
495495 write (': ' )
496- if self ._block_style :
496+ if self ._expand :
497497 recursive_indent = indent
498498 else :
499499 recursive_indent = indent + len (rep ) + 2
@@ -516,7 +516,7 @@ def _format_namespace_items(self, items, stream, indent, allowance, context, lev
516516 # recursive dataclass repr.
517517 write ("..." )
518518 else :
519- if self ._block_style :
519+ if self ._expand :
520520 recursive_indent = indent
521521 else :
522522 recursive_indent = indent + len (key ) + 1
@@ -529,9 +529,9 @@ def _format_namespace_items(self, items, stream, indent, allowance, context, lev
529529 def _format_items (self , items , stream , indent , allowance , context , level ):
530530 write = stream .write
531531 indent += self ._indent_per_level
532- if self ._indent_per_level > 1 and not self ._block_style :
532+ if self ._indent_per_level > 1 and not self ._expand :
533533 write ((self ._indent_per_level - 1 ) * ' ' )
534- if self ._indent_per_level > 0 and self ._block_style :
534+ if self ._indent_per_level > 0 and self ._expand :
535535 write (self ._indent_per_level * ' ' )
536536 delimnl = ',\n ' + ' ' * indent
537537 delim = ''
@@ -591,7 +591,7 @@ def _pprint_default_dict(self, object, stream, indent, allowance, context, level
591591 return
592592 rdf = self ._repr (object .default_factory , context , level )
593593 cls = object .__class__
594- if self ._block_style :
594+ if self ._expand :
595595 stream .write ('%s(%s, ' % (cls .__name__ , rdf ))
596596 else :
597597 indent += len (cls .__name__ ) + 1
@@ -608,12 +608,12 @@ def _pprint_counter(self, object, stream, indent, allowance, context, level):
608608 return
609609 cls = object .__class__
610610 stream .write (self ._format_block_start (cls .__name__ + '({' , indent ))
611- if self ._indent_per_level > 1 and not self ._block_style :
611+ if self ._indent_per_level > 1 and not self ._expand :
612612 stream .write ((self ._indent_per_level - 1 ) * ' ' )
613- if self ._indent_per_level > 0 and self ._block_style :
613+ if self ._indent_per_level > 0 and self ._expand :
614614 stream .write (self ._indent_per_level * ' ' )
615615 items = object .most_common ()
616- if self ._block_style :
616+ if self ._expand :
617617 recursive_indent = indent
618618 else :
619619 recursive_indent = indent + len (cls .__name__ ) + 1
@@ -630,7 +630,7 @@ def _pprint_chain_map(self, object, stream, indent, allowance, context, level):
630630 cls = object .__class__
631631 stream .write (self ._format_block_start (cls .__name__ + '(' ,
632632 indent + self ._indent_per_level ))
633- if self ._block_style :
633+ if self ._expand :
634634 indent += self ._indent_per_level
635635 else :
636636 indent += len (cls .__name__ ) + 1
@@ -650,7 +650,7 @@ def _pprint_deque(self, object, stream, indent, allowance, context, level):
650650 return
651651 cls = object .__class__
652652 stream .write (self ._format_block_start (cls .__name__ + '([' , indent ))
653- if not self ._block_style :
653+ if not self ._expand :
654654 indent += len (cls .__name__ ) + 1
655655 if object .maxlen is None :
656656 self ._format_items (object , stream , indent , allowance + 2 ,
@@ -660,7 +660,7 @@ def _pprint_deque(self, object, stream, indent, allowance, context, level):
660660 self ._format_items (object , stream , indent , 2 ,
661661 context , level )
662662 rml = self ._repr (object .maxlen , context , level )
663- if self ._block_style :
663+ if self ._expand :
664664 stream .write ('%s], maxlen=%s)' % ('\n ' + ' ' * indent , rml ))
665665 else :
666666 stream .write ('],\n %smaxlen=%s)' % (' ' * indent , rml ))
0 commit comments