blob: 3e2e8926f5d41b270086570c535b0d54be534e64 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`pprint` --- Data pretty printer
3=====================================
4
5.. module:: pprint
6 :synopsis: Data pretty printer.
7.. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
10
11The :mod:`pprint` module provides a capability to "pretty-print" arbitrary
12Python data structures in a form which can be used as input to the interpreter.
13If the formatted structures include objects which are not fundamental Python
14types, the representation may not be loadable. This may be the case if objects
15such as files, sockets, classes, or instances are included, as well as many
16other builtin objects which are not representable as Python constants.
17
18The formatted representation keeps objects on a single line if it can, and
19breaks them onto multiple lines if they don't fit within the allowed width.
20Construct :class:`PrettyPrinter` objects explicitly if you need to adjust the
21width constraint.
22
Georg Brandl55ac8f02007-09-01 13:51:09 +000023Dictionaries are sorted by key before the display is computed.
Georg Brandl116aa622007-08-15 14:28:22 +000024
Christian Heimes1af737c2008-01-23 08:24:23 +000025.. versionchanged:: 2.6
26 Added support for :class:`set` and :class:`frozenset`.
27
Georg Brandl116aa622007-08-15 14:28:22 +000028The :mod:`pprint` module defines one class:
29
Christian Heimes5b5e81c2007-12-31 16:14:33 +000030.. First the implementation class:
Georg Brandl116aa622007-08-15 14:28:22 +000031
32
33.. class:: PrettyPrinter(...)
34
35 Construct a :class:`PrettyPrinter` instance. This constructor understands
36 several keyword parameters. An output stream may be set using the *stream*
37 keyword; the only method used on the stream object is the file protocol's
38 :meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts
39 ``sys.stdout``. Three additional parameters may be used to control the
40 formatted representation. The keywords are *indent*, *depth*, and *width*. The
41 amount of indentation added for each recursive level is specified by *indent*;
42 the default is one. Other values can cause output to look a little odd, but can
43 make nesting easier to spot. The number of levels which may be printed is
44 controlled by *depth*; if the data structure being printed is too deep, the next
45 contained level is replaced by ``...``. By default, there is no constraint on
46 the depth of the objects being formatted. The desired output width is
47 constrained using the *width* parameter; the default is 80 characters. If a
48 structure cannot be formatted within the constrained width, a best effort will
Christian Heimesfe337bf2008-03-23 21:54:12 +000049 be made.
Georg Brandl116aa622007-08-15 14:28:22 +000050
Christian Heimesb9eccbf2007-12-05 20:18:38 +000051 >>> import pprint
52 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000053 >>> stuff.insert(0, stuff[:])
54 >>> pp = pprint.PrettyPrinter(indent=4)
55 >>> pp.pprint(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +000056 [ [ 'spam', 'eggs', 'lumberjack', 'knights', 'ni'],
Christian Heimesb9eccbf2007-12-05 20:18:38 +000057 'spam',
58 'eggs',
59 'lumberjack',
60 'knights',
61 'ni']
62 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
63 ... ('parrot', ('fresh fruit',))))))))
Georg Brandl116aa622007-08-15 14:28:22 +000064 >>> pp = pprint.PrettyPrinter(depth=6)
65 >>> pp.pprint(tup)
Christian Heimesb9eccbf2007-12-05 20:18:38 +000066 ('spam',
67 ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', (...,))))))))
Georg Brandl116aa622007-08-15 14:28:22 +000068
69The :class:`PrettyPrinter` class supports several derivative functions:
70
Christian Heimes5b5e81c2007-12-31 16:14:33 +000071.. Now the derivative functions:
Georg Brandl116aa622007-08-15 14:28:22 +000072
73.. function:: pformat(object[, indent[, width[, depth]]])
74
75 Return the formatted representation of *object* as a string. *indent*, *width*
76 and *depth* will be passed to the :class:`PrettyPrinter` constructor as
77 formatting parameters.
78
Georg Brandl116aa622007-08-15 14:28:22 +000079
80.. function:: pprint(object[, stream[, indent[, width[, depth]]]])
81
82 Prints the formatted representation of *object* on *stream*, followed by a
Georg Brandl6911e3c2007-09-04 07:15:32 +000083 newline. If *stream* is omitted, ``sys.stdout`` is used. This may be used
84 in the interactive interpreter instead of the :func:`print` function for
85 inspecting values (you can even reassign ``print = pprint.pprint`` for use
86 within a scope). *indent*, *width* and *depth* will be passed to the
Christian Heimesfe337bf2008-03-23 21:54:12 +000087 :class:`PrettyPrinter` constructor as formatting parameters.
Georg Brandl116aa622007-08-15 14:28:22 +000088
Christian Heimesb9eccbf2007-12-05 20:18:38 +000089 >>> import pprint
90 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000091 >>> stuff.insert(0, stuff)
92 >>> pprint.pprint(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +000093 [<Recursion on list with id=...>,
94 'spam',
95 'eggs',
96 'lumberjack',
97 'knights',
98 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000099
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101.. function:: isreadable(object)
102
103 .. index:: builtin: eval
104
105 Determine if the formatted representation of *object* is "readable," or can be
106 used to reconstruct the value using :func:`eval`. This always returns ``False``
Christian Heimesfe337bf2008-03-23 21:54:12 +0000107 for recursive objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109 >>> pprint.isreadable(stuff)
110 False
111
112
113.. function:: isrecursive(object)
114
115 Determine if *object* requires a recursive representation.
116
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Christian Heimesfe337bf2008-03-23 21:54:12 +0000118One more support function is also defined:
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120.. function:: saferepr(object)
121
122 Return a string representation of *object*, protected against recursive data
123 structures. If the representation of *object* exposes a recursive entry, the
124 recursive reference will be represented as ``<Recursion on typename with
125 id=number>``. The representation is not otherwise formatted.
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127 >>> pprint.saferepr(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000128 "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130
131.. _prettyprinter-objects:
132
133PrettyPrinter Objects
134---------------------
135
136:class:`PrettyPrinter` instances have the following methods:
137
138
139.. method:: PrettyPrinter.pformat(object)
140
141 Return the formatted representation of *object*. This takes into account the
142 options passed to the :class:`PrettyPrinter` constructor.
143
144
145.. method:: PrettyPrinter.pprint(object)
146
147 Print the formatted representation of *object* on the configured stream,
148 followed by a newline.
149
150The following methods provide the implementations for the corresponding
151functions of the same names. Using these methods on an instance is slightly
152more efficient since new :class:`PrettyPrinter` objects don't need to be
153created.
154
155
156.. method:: PrettyPrinter.isreadable(object)
157
158 .. index:: builtin: eval
159
160 Determine if the formatted representation of the object is "readable," or can be
161 used to reconstruct the value using :func:`eval`. Note that this returns
162 ``False`` for recursive objects. If the *depth* parameter of the
163 :class:`PrettyPrinter` is set and the object is deeper than allowed, this
164 returns ``False``.
165
166
167.. method:: PrettyPrinter.isrecursive(object)
168
169 Determine if the object requires a recursive representation.
170
171This method is provided as a hook to allow subclasses to modify the way objects
172are converted to strings. The default implementation uses the internals of the
173:func:`saferepr` implementation.
174
175
176.. method:: PrettyPrinter.format(object, context, maxlevels, level)
177
178 Returns three values: the formatted version of *object* as a string, a flag
179 indicating whether the result is readable, and a flag indicating whether
180 recursion was detected. The first argument is the object to be presented. The
181 second is a dictionary which contains the :func:`id` of objects that are part of
182 the current presentation context (direct and indirect containers for *object*
183 that are affecting the presentation) as the keys; if an object needs to be
184 presented which is already represented in *context*, the third return value
185 should be ``True``. Recursive calls to the :meth:`format` method should add
186 additional entries for containers to this dictionary. The third argument,
187 *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
188 is no requested limit. This argument should be passed unmodified to recursive
189 calls. The fourth argument, *level*, gives the current level; recursive calls
190 should be passed a value less than that of the current call.
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000191
192
193.. _pprint-example:
194
195pprint Example
196--------------
197
198This example demonstrates several uses of the :func:`pprint` function and its parameters.
199
200 >>> import pprint
201 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
202 ... ('parrot', ('fresh fruit',))))))))
203 >>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
204 >>> pprint.pprint(stuff)
205 ['aaaaaaaaaa',
206 ('spam',
207 ('eggs',
208 ('lumberjack',
209 ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
210 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
211 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
212 >>> pprint.pprint(stuff, depth=3)
213 ['aaaaaaaaaa',
214 ('spam', ('eggs', ('lumberjack', (...)))),
215 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
216 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
217 >>> pprint.pprint(stuff, width=60)
218 ['aaaaaaaaaa',
219 ('spam',
220 ('eggs',
221 ('lumberjack',
222 ('knights',
223 ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
224 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
225 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
226 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
227