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