blob: 58b6702fda78dcd6b5e398e1402387f1baf84e8f [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`pprint` --- Data pretty printer
2=====================================
3
4.. module:: pprint
5 :synopsis: Data pretty printer.
6.. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
Raymond Hettinger10480942011-01-10 03:26:08 +00009**Source code:** :source:`Lib/pprint.py`
Georg Brandl116aa622007-08-15 14:28:22 +000010
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 Brandl22b34312009-07-26 14:54:51 +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
Georg Brandl18244152009-09-02 20:34:52 +000030.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None)
Georg Brandl116aa622007-08-15 14:28:22 +000031
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
Georg Brandl18244152009-09-02 20:34:52 +000065
Georg Brandl116aa622007-08-15 14:28:22 +000066The :class:`PrettyPrinter` class supports several derivative functions:
67
Georg Brandl18244152009-09-02 20:34:52 +000068.. function:: pformat(object, indent=1, width=80, depth=None)
Georg Brandl116aa622007-08-15 14:28:22 +000069
70 Return the formatted representation of *object* as a string. *indent*, *width*
71 and *depth* will be passed to the :class:`PrettyPrinter` constructor as
72 formatting parameters.
73
Georg Brandl116aa622007-08-15 14:28:22 +000074
Georg Brandl18244152009-09-02 20:34:52 +000075.. function:: pprint(object, stream=None, indent=1, width=80, depth=None)
Georg Brandl116aa622007-08-15 14:28:22 +000076
77 Prints the formatted representation of *object* on *stream*, followed by a
Georg Brandl18244152009-09-02 20:34:52 +000078 newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used
Georg Brandl6911e3c2007-09-04 07:15:32 +000079 in the interactive interpreter instead of the :func:`print` function for
80 inspecting values (you can even reassign ``print = pprint.pprint`` for use
81 within a scope). *indent*, *width* and *depth* will be passed to the
Christian Heimesfe337bf2008-03-23 21:54:12 +000082 :class:`PrettyPrinter` constructor as formatting parameters.
Georg Brandl116aa622007-08-15 14:28:22 +000083
Christian Heimesb9eccbf2007-12-05 20:18:38 +000084 >>> import pprint
85 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000086 >>> stuff.insert(0, stuff)
87 >>> pprint.pprint(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +000088 [<Recursion on list with id=...>,
89 'spam',
90 'eggs',
91 'lumberjack',
92 'knights',
93 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000094
Georg Brandl116aa622007-08-15 14:28:22 +000095
96.. function:: isreadable(object)
97
98 .. index:: builtin: eval
99
100 Determine if the formatted representation of *object* is "readable," or can be
101 used to reconstruct the value using :func:`eval`. This always returns ``False``
Christian Heimesfe337bf2008-03-23 21:54:12 +0000102 for recursive objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104 >>> pprint.isreadable(stuff)
105 False
106
107
108.. function:: isrecursive(object)
109
110 Determine if *object* requires a recursive representation.
111
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Christian Heimesfe337bf2008-03-23 21:54:12 +0000113One more support function is also defined:
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115.. function:: saferepr(object)
116
117 Return a string representation of *object*, protected against recursive data
118 structures. If the representation of *object* exposes a recursive entry, the
119 recursive reference will be represented as ``<Recursion on typename with
120 id=number>``. The representation is not otherwise formatted.
121
Georg Brandl116aa622007-08-15 14:28:22 +0000122 >>> pprint.saferepr(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000123 "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. _prettyprinter-objects:
127
128PrettyPrinter Objects
129---------------------
130
131:class:`PrettyPrinter` instances have the following methods:
132
133
134.. method:: PrettyPrinter.pformat(object)
135
136 Return the formatted representation of *object*. This takes into account the
137 options passed to the :class:`PrettyPrinter` constructor.
138
139
140.. method:: PrettyPrinter.pprint(object)
141
142 Print the formatted representation of *object* on the configured stream,
143 followed by a newline.
144
145The following methods provide the implementations for the corresponding
146functions of the same names. Using these methods on an instance is slightly
147more efficient since new :class:`PrettyPrinter` objects don't need to be
148created.
149
150
151.. method:: PrettyPrinter.isreadable(object)
152
153 .. index:: builtin: eval
154
155 Determine if the formatted representation of the object is "readable," or can be
156 used to reconstruct the value using :func:`eval`. Note that this returns
157 ``False`` for recursive objects. If the *depth* parameter of the
158 :class:`PrettyPrinter` is set and the object is deeper than allowed, this
159 returns ``False``.
160
161
162.. method:: PrettyPrinter.isrecursive(object)
163
164 Determine if the object requires a recursive representation.
165
166This method is provided as a hook to allow subclasses to modify the way objects
167are converted to strings. The default implementation uses the internals of the
168:func:`saferepr` implementation.
169
170
171.. method:: PrettyPrinter.format(object, context, maxlevels, level)
172
173 Returns three values: the formatted version of *object* as a string, a flag
174 indicating whether the result is readable, and a flag indicating whether
175 recursion was detected. The first argument is the object to be presented. The
176 second is a dictionary which contains the :func:`id` of objects that are part of
177 the current presentation context (direct and indirect containers for *object*
178 that are affecting the presentation) as the keys; if an object needs to be
179 presented which is already represented in *context*, the third return value
180 should be ``True``. Recursive calls to the :meth:`format` method should add
181 additional entries for containers to this dictionary. The third argument,
182 *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
183 is no requested limit. This argument should be passed unmodified to recursive
184 calls. The fourth argument, *level*, gives the current level; recursive calls
185 should be passed a value less than that of the current call.
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000186
187
188.. _pprint-example:
189
190pprint Example
191--------------
192
Georg Brandl18244152009-09-02 20:34:52 +0000193This example demonstrates several uses of the :func:`pprint` function and its
194parameters.
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000195
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