41
41
from io import StringIO as _StringIO
42
42
43
43
__all__ = ["pprint" ,"pformat" ,"isreadable" ,"isrecursive" ,"saferepr" ,
44
- "PrettyPrinter" ]
44
+ "PrettyPrinter" , "pp" ]
45
45
46
46
47
47
def pprint (object , stream = None , indent = 1 , width = 80 , depth = None , * ,
48
- compact = False ):
48
+ compact = False , sort_dicts = True ):
49
49
"""Pretty-print a Python object to a stream [default is sys.stdout]."""
50
50
printer = PrettyPrinter (
51
51
stream = stream , indent = indent , width = width , depth = depth ,
52
- compact = compact )
52
+ compact = compact , sort_dicts = sort_dicts )
53
53
printer .pprint (object )
54
54
55
- def pformat (object , indent = 1 , width = 80 , depth = None , * , compact = False ):
55
+ def pformat (object , indent = 1 , width = 80 , depth = None , * ,
56
+ compact = False , sort_dicts = True ):
56
57
"""Format a Python object into a pretty-printed representation."""
57
58
return PrettyPrinter (indent = indent , width = width , depth = depth ,
58
- compact = compact ).pformat (object )
59
+ compact = compact , sort_dicts = sort_dicts ).pformat (object )
60
+
61
+ def pp (object , * args , sort_dicts = False , ** kwargs ):
62
+ """Pretty-print a Python object"""
63
+ pprint (object , * args , sort_dicts = sort_dicts , ** kwargs )
59
64
60
65
def saferepr (object ):
61
66
"""Version of repr() which can handle recursive data structures."""
62
- return _safe_repr (object , {}, None , 0 )[0 ]
67
+ return _safe_repr (object , {}, None , 0 , True )[0 ]
63
68
64
69
def isreadable (object ):
65
70
"""Determine if saferepr(object) is readable by eval()."""
66
- return _safe_repr (object , {}, None , 0 )[1 ]
71
+ return _safe_repr (object , {}, None , 0 , True )[1 ]
67
72
68
73
def isrecursive (object ):
69
74
"""Determine if object requires a recursive representation."""
70
- return _safe_repr (object , {}, None , 0 )[2 ]
75
+ return _safe_repr (object , {}, None , 0 , True )[2 ]
71
76
72
77
class _safe_key :
73
78
"""Helper function for key functions when sorting unorderable objects.
@@ -97,7 +102,7 @@ def _safe_tuple(t):
97
102
98
103
class PrettyPrinter :
99
104
def __init__ (self , indent = 1 , width = 80 , depth = None , stream = None , * ,
100
- compact = False ):
105
+ compact = False , sort_dicts = True ):
101
106
"""Handle pretty printing operations onto a stream using a set of
102
107
configured parameters.
103
108
@@ -117,6 +122,9 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
117
122
compact
118
123
If true, several items will be combined in one line.
119
124
125
+ sort_dicts
126
+ If true, dict keys are sorted.
127
+
120
128
"""
121
129
indent = int (indent )
122
130
width = int (width )
@@ -134,6 +142,7 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
134
142
else :
135
143
self ._stream = _sys .stdout
136
144
self ._compact = bool (compact )
145
+ self ._sort_dicts = sort_dicts
137
146
138
147
def pprint (self , object ):
139
148
self ._format (object , self ._stream , 0 , 0 , {}, 0 )
@@ -184,7 +193,10 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level):
184
193
write ((self ._indent_per_level - 1 ) * ' ' )
185
194
length = len (object )
186
195
if length :
187
- items = sorted (object .items (), key = _safe_tuple )
196
+ if self ._sort_dicts :
197
+ items = sorted (object .items (), key = _safe_tuple )
198
+ else :
199
+ items = object .items ()
188
200
self ._format_dict_items (items , stream , indent , allowance + 1 ,
189
201
context , level )
190
202
write ('}' )
@@ -402,7 +414,7 @@ def format(self, object, context, maxlevels, level):
402
414
and flags indicating whether the representation is 'readable'
403
415
and whether the object represents a recursive construct.
404
416
"""
405
- return _safe_repr (object , context , maxlevels , level )
417
+ return _safe_repr (object , context , maxlevels , level , self . _sort_dicts )
406
418
407
419
def _pprint_default_dict (self , object , stream , indent , allowance , context , level ):
408
420
if not len (object ):
@@ -487,7 +499,7 @@ def _pprint_user_string(self, object, stream, indent, allowance, context, level)
487
499
488
500
# Return triple (repr_string, isreadable, isrecursive).
489
501
490
- def _safe_repr (object , context , maxlevels , level ):
502
+ def _safe_repr (object , context , maxlevels , level , sort_dicts ):
491
503
typ = type (object )
492
504
if typ in _builtin_scalars :
493
505
return repr (object ), True , False
@@ -507,11 +519,13 @@ def _safe_repr(object, context, maxlevels, level):
507
519
components = []
508
520
append = components .append
509
521
level += 1
510
- saferepr = _safe_repr
511
- items = sorted (object .items (), key = _safe_tuple )
522
+ if sort_dicts :
523
+ items = sorted (object .items (), key = _safe_tuple )
524
+ else :
525
+ items = object .items ()
512
526
for k , v in items :
513
- krepr , kreadable , krecur = saferepr (k , context , maxlevels , level )
514
- vrepr , vreadable , vrecur = saferepr (v , context , maxlevels , level )
527
+ krepr , kreadable , krecur = _safe_repr (k , context , maxlevels , level , sort_dicts )
528
+ vrepr , vreadable , vrecur = _safe_repr (v , context , maxlevels , level , sort_dicts )
515
529
append ("%s: %s" % (krepr , vrepr ))
516
530
readable = readable and kreadable and vreadable
517
531
if krecur or vrecur :
@@ -543,7 +557,7 @@ def _safe_repr(object, context, maxlevels, level):
543
557
append = components .append
544
558
level += 1
545
559
for o in object :
546
- orepr , oreadable , orecur = _safe_repr (o , context , maxlevels , level )
560
+ orepr , oreadable , orecur = _safe_repr (o , context , maxlevels , level , sort_dicts )
547
561
append (orepr )
548
562
if not oreadable :
549
563
readable = False
@@ -569,7 +583,7 @@ def _perfcheck(object=None):
569
583
object = [("string" , (1 , 2 ), [3 , 4 ], {5 : 6 , 7 : 8 })] * 100000
570
584
p = PrettyPrinter ()
571
585
t1 = time .perf_counter ()
572
- _safe_repr (object , {}, None , 0 )
586
+ _safe_repr (object , {}, None , 0 , True )
573
587
t2 = time .perf_counter ()
574
588
p .pformat (object )
575
589
t3 = time .perf_counter ()
0 commit comments