blob: 8c28879c938c2dbe7c9aefba04ff33e1186af552 [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
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)
Christian Heimesfe337bf2008-03-23 21:54:12 +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)
Christian Heimesb9eccbf2007-12-05 20:18:38 +000063 ('spam',
64 ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', (...,))))))))
Georg Brandl116aa622007-08-15 14:28:22 +000065
66The :class:`PrettyPrinter` class supports several derivative functions:
67
Christian Heimes5b5e81c2007-12-31 16:14:33 +000068.. Now the derivative functions:
Georg Brandl116aa622007-08-15 14:28:22 +000069
70.. function:: pformat(object[, indent[, width[, depth]]])
71
72 Return the formatted representation of *object* as a string. *indent*, *width*
73 and *depth* will be passed to the :class:`PrettyPrinter` constructor as
74 formatting parameters.
75
Georg Brandl116aa622007-08-15 14:28:22 +000076
77.. function:: pprint(object[, stream[, indent[, width[, depth]]]])
78
79 Prints the formatted representation of *object* on *stream*, followed by a
Georg Brandl6911e3c2007-09-04 07:15:32 +000080 newline. If *stream* is omitted, ``sys.stdout`` is used. This may be used
81 in the interactive interpreter instead of the :func:`print` function for
82 inspecting values (you can even reassign ``print = pprint.pprint`` for use
83 within a scope). *indent*, *width* and *depth* will be passed to the
Christian Heimesfe337bf2008-03-23 21:54:12 +000084 :class:`PrettyPrinter` constructor as formatting parameters.
Georg Brandl116aa622007-08-15 14:28:22 +000085
Christian Heimesb9eccbf2007-12-05 20:18:38 +000086 >>> import pprint
87 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000088 >>> stuff.insert(0, stuff)
89 >>> pprint.pprint(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +000090 [<Recursion on list with id=...>,
91 'spam',
92 'eggs',
93 'lumberjack',
94 'knights',
95 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000096
Georg Brandl116aa622007-08-15 14:28:22 +000097
98.. function:: isreadable(object)
99
100 .. index:: builtin: eval
101
102 Determine if the formatted representation of *object* is "readable," or can be
103 used to reconstruct the value using :func:`eval`. This always returns ``False``
Christian Heimesfe337bf2008-03-23 21:54:12 +0000104 for recursive objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106 >>> pprint.isreadable(stuff)
107 False
108
109
110.. function:: isrecursive(object)
111
112 Determine if *object* requires a recursive representation.
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Christian Heimesfe337bf2008-03-23 21:54:12 +0000115One more support function is also defined:
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117.. function:: saferepr(object)
118
119 Return a string representation of *object*, protected against recursive data
120 structures. If the representation of *object* exposes a recursive entry, the
121 recursive reference will be represented as ``<Recursion on typename with
122 id=number>``. The representation is not otherwise formatted.
123
Georg Brandl116aa622007-08-15 14:28:22 +0000124 >>> pprint.saferepr(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000125 "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
128.. _prettyprinter-objects:
129
130PrettyPrinter Objects
131---------------------
132
133:class:`PrettyPrinter` instances have the following methods:
134
135
136.. method:: PrettyPrinter.pformat(object)
137
138 Return the formatted representation of *object*. This takes into account the
139 options passed to the :class:`PrettyPrinter` constructor.
140
141
142.. method:: PrettyPrinter.pprint(object)
143
144 Print the formatted representation of *object* on the configured stream,
145 followed by a newline.
146
147The following methods provide the implementations for the corresponding
148functions of the same names. Using these methods on an instance is slightly
149more efficient since new :class:`PrettyPrinter` objects don't need to be
150created.
151
152
153.. method:: PrettyPrinter.isreadable(object)
154
155 .. index:: builtin: eval
156
157 Determine if the formatted representation of the object is "readable," or can be
158 used to reconstruct the value using :func:`eval`. Note that this returns
159 ``False`` for recursive objects. If the *depth* parameter of the
160 :class:`PrettyPrinter` is set and the object is deeper than allowed, this
161 returns ``False``.
162
163
164.. method:: PrettyPrinter.isrecursive(object)
165
166 Determine if the object requires a recursive representation.
167
168This method is provided as a hook to allow subclasses to modify the way objects
169are converted to strings. The default implementation uses the internals of the
170:func:`saferepr` implementation.
171
172
173.. method:: PrettyPrinter.format(object, context, maxlevels, level)
174
175 Returns three values: the formatted version of *object* as a string, a flag
176 indicating whether the result is readable, and a flag indicating whether
177 recursion was detected. The first argument is the object to be presented. The
178 second is a dictionary which contains the :func:`id` of objects that are part of
179 the current presentation context (direct and indirect containers for *object*
180 that are affecting the presentation) as the keys; if an object needs to be
181 presented which is already represented in *context*, the third return value
182 should be ``True``. Recursive calls to the :meth:`format` method should add
183 additional entries for containers to this dictionary. The third argument,
184 *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
185 is no requested limit. This argument should be passed unmodified to recursive
186 calls. The fourth argument, *level*, gives the current level; recursive calls
187 should be passed a value less than that of the current call.
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000188
189
190.. _pprint-example:
191
192pprint Example
193--------------
194
195This example demonstrates several uses of the :func:`pprint` function and its parameters.
196
197 >>> import pprint
198 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
199 ... ('parrot', ('fresh fruit',))))))))
200 >>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
201 >>> pprint.pprint(stuff)
202 ['aaaaaaaaaa',
203 ('spam',
204 ('eggs',
205 ('lumberjack',
206 ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
207 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
208 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
209 >>> pprint.pprint(stuff, depth=3)
210 ['aaaaaaaaaa',
211 ('spam', ('eggs', ('lumberjack', (...)))),
212 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
213 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
214 >>> pprint.pprint(stuff, width=60)
215 ['aaaaaaaaaa',
216 ('spam',
217 ('eggs',
218 ('lumberjack',
219 ('knights',
220 ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
221 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
222 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
223 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
224