blob: d1d1baeeab704a7fe89cf3827a2ca841ca98c393 [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
Georg Brandlc5605df2009-08-13 08:26:44 +000016other built-in objects which are not representable as Python constants.
Georg Brandl116aa622007-08-15 14:28:22 +000017
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
25The :mod:`pprint` module defines one class:
26
Christian Heimes5b5e81c2007-12-31 16:14:33 +000027.. First the implementation class:
Georg Brandl116aa622007-08-15 14:28:22 +000028
29
30.. class:: PrettyPrinter(...)
31
32 Construct a :class:`PrettyPrinter` instance. This constructor understands
33 several keyword parameters. An output stream may be set using the *stream*
34 keyword; the only method used on the stream object is the file protocol's
35 :meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts
36 ``sys.stdout``. Three additional parameters may be used to control the
37 formatted representation. The keywords are *indent*, *depth*, and *width*. The
38 amount of indentation added for each recursive level is specified by *indent*;
39 the default is one. Other values can cause output to look a little odd, but can
40 make nesting easier to spot. The number of levels which may be printed is
41 controlled by *depth*; if the data structure being printed is too deep, the next
42 contained level is replaced by ``...``. By default, there is no constraint on
43 the depth of the objects being formatted. The desired output width is
44 constrained using the *width* parameter; the default is 80 characters. If a
45 structure cannot be formatted within the constrained width, a best effort will
Christian Heimesfe337bf2008-03-23 21:54:12 +000046 be made.
Georg Brandl116aa622007-08-15 14:28:22 +000047
Christian Heimesb9eccbf2007-12-05 20:18:38 +000048 >>> import pprint
49 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000050 >>> stuff.insert(0, stuff[:])
51 >>> pp = pprint.PrettyPrinter(indent=4)
52 >>> pp.pprint(stuff)
Georg Brandl3ccb7872008-07-16 03:00:45 +000053 [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
Christian Heimesb9eccbf2007-12-05 20:18:38 +000054 'spam',
55 'eggs',
56 'lumberjack',
57 'knights',
58 'ni']
59 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
60 ... ('parrot', ('fresh fruit',))))))))
Georg Brandl116aa622007-08-15 14:28:22 +000061 >>> pp = pprint.PrettyPrinter(depth=6)
62 >>> pp.pprint(tup)
Alexandre Vassalottieca20b62008-05-16 02:54:33 +000063 ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
Georg Brandl116aa622007-08-15 14:28:22 +000064
65The :class:`PrettyPrinter` class supports several derivative functions:
66
Christian Heimes5b5e81c2007-12-31 16:14:33 +000067.. Now the derivative functions:
Georg Brandl116aa622007-08-15 14:28:22 +000068
69.. function:: pformat(object[, indent[, width[, depth]]])
70
71 Return the formatted representation of *object* as a string. *indent*, *width*
72 and *depth* will be passed to the :class:`PrettyPrinter` constructor as
73 formatting parameters.
74
Georg Brandl116aa622007-08-15 14:28:22 +000075
76.. function:: pprint(object[, stream[, indent[, width[, depth]]]])
77
78 Prints the formatted representation of *object* on *stream*, followed by a
Georg Brandl6911e3c2007-09-04 07:15:32 +000079 newline. If *stream* is omitted, ``sys.stdout`` is used. This may be used
80 in the interactive interpreter instead of the :func:`print` function for
81 inspecting values (you can even reassign ``print = pprint.pprint`` for use
82 within a scope). *indent*, *width* and *depth* will be passed to the
Christian Heimesfe337bf2008-03-23 21:54:12 +000083 :class:`PrettyPrinter` constructor as formatting parameters.
Georg Brandl116aa622007-08-15 14:28:22 +000084
Christian Heimesb9eccbf2007-12-05 20:18:38 +000085 >>> import pprint
86 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000087 >>> stuff.insert(0, stuff)
88 >>> pprint.pprint(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +000089 [<Recursion on list with id=...>,
90 'spam',
91 'eggs',
92 'lumberjack',
93 'knights',
94 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000095
Georg Brandl116aa622007-08-15 14:28:22 +000096
97.. function:: isreadable(object)
98
99 .. index:: builtin: eval
100
101 Determine if the formatted representation of *object* is "readable," or can be
102 used to reconstruct the value using :func:`eval`. This always returns ``False``
Christian Heimesfe337bf2008-03-23 21:54:12 +0000103 for recursive objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105 >>> pprint.isreadable(stuff)
106 False
107
108
109.. function:: isrecursive(object)
110
111 Determine if *object* requires a recursive representation.
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Christian Heimesfe337bf2008-03-23 21:54:12 +0000114One more support function is also defined:
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116.. function:: saferepr(object)
117
118 Return a string representation of *object*, protected against recursive data
119 structures. If the representation of *object* exposes a recursive entry, the
120 recursive reference will be represented as ``<Recursion on typename with
121 id=number>``. The representation is not otherwise formatted.
122
Georg Brandl116aa622007-08-15 14:28:22 +0000123 >>> pprint.saferepr(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000124 "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126
127.. _prettyprinter-objects:
128
129PrettyPrinter Objects
130---------------------
131
132:class:`PrettyPrinter` instances have the following methods:
133
134
135.. method:: PrettyPrinter.pformat(object)
136
137 Return the formatted representation of *object*. This takes into account the
138 options passed to the :class:`PrettyPrinter` constructor.
139
140
141.. method:: PrettyPrinter.pprint(object)
142
143 Print the formatted representation of *object* on the configured stream,
144 followed by a newline.
145
146The following methods provide the implementations for the corresponding
147functions of the same names. Using these methods on an instance is slightly
148more efficient since new :class:`PrettyPrinter` objects don't need to be
149created.
150
151
152.. method:: PrettyPrinter.isreadable(object)
153
154 .. index:: builtin: eval
155
156 Determine if the formatted representation of the object is "readable," or can be
157 used to reconstruct the value using :func:`eval`. Note that this returns
158 ``False`` for recursive objects. If the *depth* parameter of the
159 :class:`PrettyPrinter` is set and the object is deeper than allowed, this
160 returns ``False``.
161
162
163.. method:: PrettyPrinter.isrecursive(object)
164
165 Determine if the object requires a recursive representation.
166
167This method is provided as a hook to allow subclasses to modify the way objects
168are converted to strings. The default implementation uses the internals of the
169:func:`saferepr` implementation.
170
171
172.. method:: PrettyPrinter.format(object, context, maxlevels, level)
173
174 Returns three values: the formatted version of *object* as a string, a flag
175 indicating whether the result is readable, and a flag indicating whether
176 recursion was detected. The first argument is the object to be presented. The
177 second is a dictionary which contains the :func:`id` of objects that are part of
178 the current presentation context (direct and indirect containers for *object*
179 that are affecting the presentation) as the keys; if an object needs to be
180 presented which is already represented in *context*, the third return value
181 should be ``True``. Recursive calls to the :meth:`format` method should add
182 additional entries for containers to this dictionary. The third argument,
183 *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
184 is no requested limit. This argument should be passed unmodified to recursive
185 calls. The fourth argument, *level*, gives the current level; recursive calls
186 should be passed a value less than that of the current call.
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000187
188
189.. _pprint-example:
190
191pprint Example
192--------------
193
194This example demonstrates several uses of the :func:`pprint` function and its parameters.
195
196 >>> import pprint
197 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
198 ... ('parrot', ('fresh fruit',))))))))
199 >>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
200 >>> pprint.pprint(stuff)
201 ['aaaaaaaaaa',
202 ('spam',
203 ('eggs',
204 ('lumberjack',
205 ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
206 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
207 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
208 >>> pprint.pprint(stuff, depth=3)
209 ['aaaaaaaaaa',
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000210 ('spam', ('eggs', (...))),
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000211 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
212 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
213 >>> pprint.pprint(stuff, width=60)
214 ['aaaaaaaaaa',
215 ('spam',
216 ('eggs',
217 ('lumberjack',
218 ('knights',
219 ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
220 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
221 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
222 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
223