blob: 7f13029153949a94e8b66383495ee36c19745cc6 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Brandld7d4fd72009-07-26 14:37:28 +000016other built-in objects which are not representable as Python constants.
Georg Brandl8ec7f652007-08-15 14:28:01 +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
23.. versionchanged:: 2.5
24 Dictionaries are sorted by key before the display is computed; before 2.5, a
25 dictionary was sorted only if its display required more than one line, although
26 that wasn't documented.
27
Raymond Hettingerc226c312008-01-23 00:04:40 +000028.. versionchanged:: 2.6
29 Added support for :class:`set` and :class:`frozenset`.
30
Raymond Hettingere0e08222010-11-06 07:10:31 +000031.. seealso::
32
33 Latest version of the `pprint module Python source code
34 <http://svn.python.org/view/python/branches/release27-maint/Lib/pprint.py?view=markup>`_
35
Georg Brandl8ec7f652007-08-15 14:28:01 +000036The :mod:`pprint` module defines one class:
37
Georg Brandlb19be572007-12-29 10:57:00 +000038.. First the implementation class:
Georg Brandl8ec7f652007-08-15 14:28:01 +000039
40
41.. class:: PrettyPrinter(...)
42
43 Construct a :class:`PrettyPrinter` instance. This constructor understands
44 several keyword parameters. An output stream may be set using the *stream*
45 keyword; the only method used on the stream object is the file protocol's
46 :meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts
47 ``sys.stdout``. Three additional parameters may be used to control the
48 formatted representation. The keywords are *indent*, *depth*, and *width*. The
49 amount of indentation added for each recursive level is specified by *indent*;
50 the default is one. Other values can cause output to look a little odd, but can
51 make nesting easier to spot. The number of levels which may be printed is
52 controlled by *depth*; if the data structure being printed is too deep, the next
53 contained level is replaced by ``...``. By default, there is no constraint on
54 the depth of the objects being formatted. The desired output width is
55 constrained using the *width* parameter; the default is 80 characters. If a
56 structure cannot be formatted within the constrained width, a best effort will
Georg Brandl473f1642008-03-22 12:59:37 +000057 be made.
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Georg Brandl722e1012007-12-05 17:56:50 +000059 >>> import pprint
60 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl8ec7f652007-08-15 14:28:01 +000061 >>> stuff.insert(0, stuff[:])
62 >>> pp = pprint.PrettyPrinter(indent=4)
63 >>> pp.pprint(stuff)
Facundo Batista2da91c32008-06-21 17:43:56 +000064 [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
Georg Brandl722e1012007-12-05 17:56:50 +000065 'spam',
66 'eggs',
67 'lumberjack',
68 'knights',
69 'ni']
70 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
71 ... ('parrot', ('fresh fruit',))))))))
Georg Brandl8ec7f652007-08-15 14:28:01 +000072 >>> pp = pprint.PrettyPrinter(depth=6)
73 >>> pp.pprint(tup)
Georg Brandl23da6e62008-05-12 16:26:52 +000074 ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
Georg Brandl8ec7f652007-08-15 14:28:01 +000075
76The :class:`PrettyPrinter` class supports several derivative functions:
77
Georg Brandlb19be572007-12-29 10:57:00 +000078.. Now the derivative functions:
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80.. function:: pformat(object[, indent[, width[, depth]]])
81
82 Return the formatted representation of *object* as a string. *indent*, *width*
83 and *depth* will be passed to the :class:`PrettyPrinter` constructor as
84 formatting parameters.
85
86 .. versionchanged:: 2.4
87 The parameters *indent*, *width* and *depth* were added.
88
89
90.. function:: pprint(object[, stream[, indent[, width[, depth]]]])
91
92 Prints the formatted representation of *object* on *stream*, followed by a
93 newline. If *stream* is omitted, ``sys.stdout`` is used. This may be used in
94 the interactive interpreter instead of a :keyword:`print` statement for
95 inspecting values. *indent*, *width* and *depth* will be passed to the
Georg Brandl473f1642008-03-22 12:59:37 +000096 :class:`PrettyPrinter` constructor as formatting parameters.
Georg Brandl8ec7f652007-08-15 14:28:01 +000097
Georg Brandl722e1012007-12-05 17:56:50 +000098 >>> import pprint
99 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100 >>> stuff.insert(0, stuff)
101 >>> pprint.pprint(stuff)
Georg Brandl473f1642008-03-22 12:59:37 +0000102 [<Recursion on list with id=...>,
103 'spam',
104 'eggs',
105 'lumberjack',
106 'knights',
107 'ni']
Georg Brandl8ec7f652007-08-15 14:28:01 +0000108
109 .. versionchanged:: 2.4
110 The parameters *indent*, *width* and *depth* were added.
111
112
113.. function:: isreadable(object)
114
115 .. index:: builtin: eval
116
117 Determine if the formatted representation of *object* is "readable," or can be
118 used to reconstruct the value using :func:`eval`. This always returns ``False``
Georg Brandl473f1642008-03-22 12:59:37 +0000119 for recursive objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
121 >>> pprint.isreadable(stuff)
122 False
123
124
125.. function:: isrecursive(object)
126
127 Determine if *object* requires a recursive representation.
128
Georg Brandl8ec7f652007-08-15 14:28:01 +0000129
Georg Brandl473f1642008-03-22 12:59:37 +0000130One more support function is also defined:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000131
132.. function:: saferepr(object)
133
134 Return a string representation of *object*, protected against recursive data
135 structures. If the representation of *object* exposes a recursive entry, the
136 recursive reference will be represented as ``<Recursion on typename with
137 id=number>``. The representation is not otherwise formatted.
138
Georg Brandl8ec7f652007-08-15 14:28:01 +0000139 >>> pprint.saferepr(stuff)
Georg Brandl473f1642008-03-22 12:59:37 +0000140 "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141
142
143.. _prettyprinter-objects:
144
145PrettyPrinter Objects
146---------------------
147
148:class:`PrettyPrinter` instances have the following methods:
149
150
151.. method:: PrettyPrinter.pformat(object)
152
153 Return the formatted representation of *object*. This takes into account the
154 options passed to the :class:`PrettyPrinter` constructor.
155
156
157.. method:: PrettyPrinter.pprint(object)
158
159 Print the formatted representation of *object* on the configured stream,
160 followed by a newline.
161
162The following methods provide the implementations for the corresponding
163functions of the same names. Using these methods on an instance is slightly
164more efficient since new :class:`PrettyPrinter` objects don't need to be
165created.
166
167
168.. method:: PrettyPrinter.isreadable(object)
169
170 .. index:: builtin: eval
171
172 Determine if the formatted representation of the object is "readable," or can be
173 used to reconstruct the value using :func:`eval`. Note that this returns
174 ``False`` for recursive objects. If the *depth* parameter of the
175 :class:`PrettyPrinter` is set and the object is deeper than allowed, this
176 returns ``False``.
177
178
179.. method:: PrettyPrinter.isrecursive(object)
180
181 Determine if the object requires a recursive representation.
182
183This method is provided as a hook to allow subclasses to modify the way objects
184are converted to strings. The default implementation uses the internals of the
185:func:`saferepr` implementation.
186
187
188.. method:: PrettyPrinter.format(object, context, maxlevels, level)
189
190 Returns three values: the formatted version of *object* as a string, a flag
191 indicating whether the result is readable, and a flag indicating whether
192 recursion was detected. The first argument is the object to be presented. The
193 second is a dictionary which contains the :func:`id` of objects that are part of
194 the current presentation context (direct and indirect containers for *object*
195 that are affecting the presentation) as the keys; if an object needs to be
196 presented which is already represented in *context*, the third return value
197 should be ``True``. Recursive calls to the :meth:`format` method should add
198 additional entries for containers to this dictionary. The third argument,
199 *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
200 is no requested limit. This argument should be passed unmodified to recursive
201 calls. The fourth argument, *level*, gives the current level; recursive calls
202 should be passed a value less than that of the current call.
203
204 .. versionadded:: 2.3
205
Georg Brandl722e1012007-12-05 17:56:50 +0000206.. _pprint-example:
207
208pprint Example
209--------------
210
211This example demonstrates several uses of the :func:`pprint` function and its parameters.
212
213 >>> import pprint
214 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
215 ... ('parrot', ('fresh fruit',))))))))
216 >>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
217 >>> pprint.pprint(stuff)
218 ['aaaaaaaaaa',
219 ('spam',
220 ('eggs',
221 ('lumberjack',
222 ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
223 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
224 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
225 >>> pprint.pprint(stuff, depth=3)
226 ['aaaaaaaaaa',
Georg Brandl23da6e62008-05-12 16:26:52 +0000227 ('spam', ('eggs', (...))),
Georg Brandl722e1012007-12-05 17:56:50 +0000228 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
229 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
230 >>> pprint.pprint(stuff, width=60)
231 ['aaaaaaaaaa',
232 ('spam',
233 ('eggs',
234 ('lumberjack',
235 ('knights',
236 ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
237 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
238 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
239 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
240