blob: 87a0cfd4c55fda5d6898a67a178001f50e3c7109 [file] [log] [blame]
Guido van Rossum16d27e31996-08-21 16:28:53 +00001# pprint.py
2#
3# Author: Fred L. Drake, Jr.
Guido van Rossum8206fb91996-08-26 00:33:29 +00004# fdrake@cnri.reston.va.us, fdrake@intr.net
Guido van Rossum16d27e31996-08-21 16:28:53 +00005#
6# This is a simple little module I wrote to make life easier. I didn't
7# see anything quite like it in the library, though I may have overlooked
8# something. I wrote this when I was trying to read some heavily nested
9# tuples with fairly non-descriptive content. This is modelled very much
10# after Lisp/Scheme - style pretty-printing of lists. If you find it
11# useful, thank small children who sleep at night.
Guido van Rossum16d27e31996-08-21 16:28:53 +000012
13"""Support to pretty-print lists, tuples, & dictionaries recursively.
Guido van Rossum8206fb91996-08-26 00:33:29 +000014Very simple, but useful, especially in debugging data structures.
Guido van Rossum16d27e31996-08-21 16:28:53 +000015
Guido van Rossum8206fb91996-08-26 00:33:29 +000016Constants
17---------
Guido van Rossum16d27e31996-08-21 16:28:53 +000018
Guido van Rossum8206fb91996-08-26 00:33:29 +000019INDENT_PER_LEVEL
20 Amount of indentation to use for each new recursive level. The
21 default is 1. This must be a non-negative integer, and may be set
22 by the caller before calling pprint().
Guido van Rossum16d27e31996-08-21 16:28:53 +000023
Guido van Rossum8206fb91996-08-26 00:33:29 +000024MAX_WIDTH
25 Maximum width of the display. This is only used if the
26 representation *can* be kept less than MAX_WIDTH characters wide.
Fred Drakeb5d20391997-04-15 14:15:23 +000027 May be set by the user before calling pprint() if needed.
Guido van Rossum16d27e31996-08-21 16:28:53 +000028
Guido van Rossum16d27e31996-08-21 16:28:53 +000029"""
30
Guido van Rossum16d27e31996-08-21 16:28:53 +000031INDENT_PER_LEVEL = 1
32
33MAX_WIDTH = 80
34
Guido van Rossum8206fb91996-08-26 00:33:29 +000035from types import DictType, ListType, TupleType
Guido van Rossum16d27e31996-08-21 16:28:53 +000036
37
Fred Drakeb5d20391997-04-15 14:15:23 +000038def pformat(seq):
39 """Format a Python object into a pretty-printed representation.
Guido van Rossum16d27e31996-08-21 16:28:53 +000040
Fred Drakeb5d20391997-04-15 14:15:23 +000041 The representation is returned with no trailing newline.
42
Guido van Rossum8206fb91996-08-26 00:33:29 +000043 """
Fred Drakeb5d20391997-04-15 14:15:23 +000044 import StringIO
45 sio = StringIO.StringIO()
46 pprint(seq, stream=sio)
47 str = sio.getvalue()
48 if str and str[-1] == '\n':
49 str = str[:-1]
50 return str
Guido van Rossum16d27e31996-08-21 16:28:53 +000051
52
Fred Drakeb5d20391997-04-15 14:15:23 +000053def pprint(seq, stream=None, indent=0, allowance=0):
Guido van Rossum16d27e31996-08-21 16:28:53 +000054 """Pretty-print a list, tuple, or dictionary.
55
Guido van Rossum8206fb91996-08-26 00:33:29 +000056 seq
57 List, tuple, or dictionary object to be pretty-printed. Other
58 object types are permitted by are not specially interpreted.
Guido van Rossum16d27e31996-08-21 16:28:53 +000059
Guido van Rossum8206fb91996-08-26 00:33:29 +000060 stream
61 Output stream. If not provided, `sys.stdout' is used. This
62 parameter must support the `write()' method with a single
63 parameter, which will always be a string. It may be a
64 `StringIO.StringIO' object if the result is needed as a
65 string.
66
67 Indentation is done according to `INDENT_PER_LEVEL', which may be
68 set to any non-negative integer before calling this function. The
69 output written on the stream is a perfectly valid representation
70 of the Python object passed in, with indentation to assist
71 human-readable interpretation. The output can be used as input
72 without error, given readable representations of all elements are
73 available via `repr()'. Output is restricted to `MAX_WIDTH'
74 columns where possible.
Fred Drakeb5d20391997-04-15 14:15:23 +000075
Guido van Rossum16d27e31996-08-21 16:28:53 +000076 """
Guido van Rossum16d27e31996-08-21 16:28:53 +000077 if stream is None:
78 import sys
79 stream = sys.stdout
80
Guido van Rossum16d27e31996-08-21 16:28:53 +000081 rep = `seq`
82 typ = type(seq)
83 sepLines = len(rep) > (MAX_WIDTH - 1 - indent - allowance)
84
85 if sepLines and (typ is ListType or typ is TupleType):
86 # Pretty-print the sequence.
87 stream.write(((typ is ListType) and '[') or '(')
88
89 length = len(seq)
90 if length:
91 indent = indent + INDENT_PER_LEVEL
92 pprint(seq[0], stream, indent, allowance + 1)
93
94 if len(seq) > 1:
95 for ent in seq[1:]:
Fred Drakeb5d20391997-04-15 14:15:23 +000096 stream.write(',\n' + ' '*indent)
Guido van Rossum16d27e31996-08-21 16:28:53 +000097 pprint(ent, stream, indent, allowance + 1)
98
99 indent = indent - INDENT_PER_LEVEL
100
101 stream.write(((typ is ListType) and ']') or ')')
102
103 elif typ is DictType and sepLines:
104 stream.write('{')
105
106 length = len(seq)
107 if length:
108 indent = indent + INDENT_PER_LEVEL
109 items = seq.items()
110 items.sort()
111 key, ent = items[0]
112 rep = `key` + ': '
113 stream.write(rep)
114 pprint(ent, stream, indent + len(rep), allowance + 1)
115
116 if len(items) > 1:
117 for key, ent in items[1:]:
118 rep = `key` + ': '
Fred Drakeb5d20391997-04-15 14:15:23 +0000119 stream.write(',\n' + ' '*indent + rep)
Guido van Rossum16d27e31996-08-21 16:28:53 +0000120 pprint(ent, stream, indent + len(rep), allowance + 1)
121
122 indent = indent - INDENT_PER_LEVEL
123
124 stream.write('}')
125
126 else:
127 stream.write(rep)
128
129 # Terminate the 'print' if we're not a recursive invocation.
130 if not indent:
131 stream.write('\n')
132
133
134#
Guido van Rossum8206fb91996-08-26 00:33:29 +0000135# end of file