blob: 82648a1e91e3647f3a2beaeff16010a11b0d983e [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
9
10The :mod:`pprint` module provides a capability to "pretty-print" arbitrary
11Python data structures in a form which can be used as input to the interpreter.
12If the formatted structures include objects which are not fundamental Python
13types, the representation may not be loadable. This may be the case if objects
14such as files, sockets, classes, or instances are included, as well as many
Georg Brandl22b34312009-07-26 14:54:51 +000015other built-in objects which are not representable as Python constants.
Georg Brandl116aa622007-08-15 14:28:22 +000016
17The formatted representation keeps objects on a single line if it can, and
18breaks them onto multiple lines if they don't fit within the allowed width.
19Construct :class:`PrettyPrinter` objects explicitly if you need to adjust the
20width constraint.
21
Georg Brandl55ac8f02007-09-01 13:51:09 +000022Dictionaries are sorted by key before the display is computed.
Georg Brandl116aa622007-08-15 14:28:22 +000023
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000024.. seealso::
25
26 Latest version of the :source:`pprint module Python source code
27 <Lib/pprint.py>`
28
Georg Brandl116aa622007-08-15 14:28:22 +000029The :mod:`pprint` module defines one class:
30
Christian Heimes5b5e81c2007-12-31 16:14:33 +000031.. First the implementation class:
Georg Brandl116aa622007-08-15 14:28:22 +000032
33
Georg Brandl18244152009-09-02 20:34:52 +000034.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None)
Georg Brandl116aa622007-08-15 14:28:22 +000035
36 Construct a :class:`PrettyPrinter` instance. This constructor understands
37 several keyword parameters. An output stream may be set using the *stream*
38 keyword; the only method used on the stream object is the file protocol's
39 :meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts
40 ``sys.stdout``. Three additional parameters may be used to control the
41 formatted representation. The keywords are *indent*, *depth*, and *width*. The
42 amount of indentation added for each recursive level is specified by *indent*;
43 the default is one. Other values can cause output to look a little odd, but can
44 make nesting easier to spot. The number of levels which may be printed is
45 controlled by *depth*; if the data structure being printed is too deep, the next
46 contained level is replaced by ``...``. By default, there is no constraint on
47 the depth of the objects being formatted. The desired output width is
48 constrained using the *width* parameter; the default is 80 characters. If a
49 structure cannot be formatted within the constrained width, a best effort will
Christian Heimesfe337bf2008-03-23 21:54:12 +000050 be made.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Christian Heimesb9eccbf2007-12-05 20:18:38 +000052 >>> import pprint
53 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000054 >>> stuff.insert(0, stuff[:])
55 >>> pp = pprint.PrettyPrinter(indent=4)
56 >>> pp.pprint(stuff)
Georg Brandl3ccb7872008-07-16 03:00:45 +000057 [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
Christian Heimesb9eccbf2007-12-05 20:18:38 +000058 'spam',
59 'eggs',
60 'lumberjack',
61 'knights',
62 'ni']
63 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
64 ... ('parrot', ('fresh fruit',))))))))
Georg Brandl116aa622007-08-15 14:28:22 +000065 >>> pp = pprint.PrettyPrinter(depth=6)
66 >>> pp.pprint(tup)
Alexandre Vassalottieca20b62008-05-16 02:54:33 +000067 ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
Georg Brandl116aa622007-08-15 14:28:22 +000068
Georg Brandl18244152009-09-02 20:34:52 +000069
Georg Brandl116aa622007-08-15 14:28:22 +000070The :class:`PrettyPrinter` class supports several derivative functions:
71
Georg Brandl18244152009-09-02 20:34:52 +000072.. function:: pformat(object, indent=1, width=80, depth=None)
Georg Brandl116aa622007-08-15 14:28:22 +000073
74 Return the formatted representation of *object* as a string. *indent*, *width*
75 and *depth* will be passed to the :class:`PrettyPrinter` constructor as
76 formatting parameters.
77
Georg Brandl116aa622007-08-15 14:28:22 +000078
Georg Brandl18244152009-09-02 20:34:52 +000079.. function:: pprint(object, stream=None, indent=1, width=80, depth=None)
Georg Brandl116aa622007-08-15 14:28:22 +000080
81 Prints the formatted representation of *object* on *stream*, followed by a
Georg Brandl18244152009-09-02 20:34:52 +000082 newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used
Georg Brandl6911e3c2007-09-04 07:15:32 +000083 in the interactive interpreter instead of the :func:`print` function for
84 inspecting values (you can even reassign ``print = pprint.pprint`` for use
85 within a scope). *indent*, *width* and *depth* will be passed to the
Christian Heimesfe337bf2008-03-23 21:54:12 +000086 :class:`PrettyPrinter` constructor as formatting parameters.
Georg Brandl116aa622007-08-15 14:28:22 +000087
Christian Heimesb9eccbf2007-12-05 20:18:38 +000088 >>> import pprint
89 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000090 >>> stuff.insert(0, stuff)
91 >>> pprint.pprint(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +000092 [<Recursion on list with id=...>,
93 'spam',
94 'eggs',
95 'lumberjack',
96 'knights',
97 'ni']
Georg Brandl116aa622007-08-15 14:28:22 +000098
Georg Brandl116aa622007-08-15 14:28:22 +000099
100.. function:: isreadable(object)
101
102 .. index:: builtin: eval
103
104 Determine if the formatted representation of *object* is "readable," or can be
105 used to reconstruct the value using :func:`eval`. This always returns ``False``
Christian Heimesfe337bf2008-03-23 21:54:12 +0000106 for recursive objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108 >>> pprint.isreadable(stuff)
109 False
110
111
112.. function:: isrecursive(object)
113
114 Determine if *object* requires a recursive representation.
115
Georg Brandl116aa622007-08-15 14:28:22 +0000116
Christian Heimesfe337bf2008-03-23 21:54:12 +0000117One more support function is also defined:
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119.. function:: saferepr(object)
120
121 Return a string representation of *object*, protected against recursive data
122 structures. If the representation of *object* exposes a recursive entry, the
123 recursive reference will be represented as ``<Recursion on typename with
124 id=number>``. The representation is not otherwise formatted.
125
Georg Brandl116aa622007-08-15 14:28:22 +0000126 >>> pprint.saferepr(stuff)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000127 "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129
130.. _prettyprinter-objects:
131
132PrettyPrinter Objects
133---------------------
134
135:class:`PrettyPrinter` instances have the following methods:
136
137
138.. method:: PrettyPrinter.pformat(object)
139
140 Return the formatted representation of *object*. This takes into account the
141 options passed to the :class:`PrettyPrinter` constructor.
142
143
144.. method:: PrettyPrinter.pprint(object)
145
146 Print the formatted representation of *object* on the configured stream,
147 followed by a newline.
148
149The following methods provide the implementations for the corresponding
150functions of the same names. Using these methods on an instance is slightly
151more efficient since new :class:`PrettyPrinter` objects don't need to be
152created.
153
154
155.. method:: PrettyPrinter.isreadable(object)
156
157 .. index:: builtin: eval
158
159 Determine if the formatted representation of the object is "readable," or can be
160 used to reconstruct the value using :func:`eval`. Note that this returns
161 ``False`` for recursive objects. If the *depth* parameter of the
162 :class:`PrettyPrinter` is set and the object is deeper than allowed, this
163 returns ``False``.
164
165
166.. method:: PrettyPrinter.isrecursive(object)
167
168 Determine if the object requires a recursive representation.
169
170This method is provided as a hook to allow subclasses to modify the way objects
171are converted to strings. The default implementation uses the internals of the
172:func:`saferepr` implementation.
173
174
175.. method:: PrettyPrinter.format(object, context, maxlevels, level)
176
177 Returns three values: the formatted version of *object* as a string, a flag
178 indicating whether the result is readable, and a flag indicating whether
179 recursion was detected. The first argument is the object to be presented. The
180 second is a dictionary which contains the :func:`id` of objects that are part of
181 the current presentation context (direct and indirect containers for *object*
182 that are affecting the presentation) as the keys; if an object needs to be
183 presented which is already represented in *context*, the third return value
184 should be ``True``. Recursive calls to the :meth:`format` method should add
185 additional entries for containers to this dictionary. The third argument,
186 *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
187 is no requested limit. This argument should be passed unmodified to recursive
188 calls. The fourth argument, *level*, gives the current level; recursive calls
189 should be passed a value less than that of the current call.
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000190
191
192.. _pprint-example:
193
194pprint Example
195--------------
196
Georg Brandl18244152009-09-02 20:34:52 +0000197This example demonstrates several uses of the :func:`pprint` function and its
198parameters.
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000199
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',
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000214 ('spam', ('eggs', (...))),
Christian Heimesb9eccbf2007-12-05 20:18:38 +0000215 ['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