| 1 |
:mod:`pprint` --- Data pretty printer |
| 2 |
===================================== |
| 3 |
|
| 4 |
.. module:: pprint |
| 5 |
:synopsis: Data pretty printer. |
| 6 |
.. moduleauthor:: Fred L. Drake, Jr. <[email protected]> |
| 7 |
.. sectionauthor:: Fred L. Drake, Jr. <[email protected]> |
| 8 |
|
| 9 |
**Source code:** :source:`Lib/pprint.py` |
| 10 |
|
| 11 |
-------------- |
| 12 |
|
| 13 |
The :mod:`pprint` module provides a capability to "pretty-print" arbitrary |
| 14 |
Python data structures in a form which can be used as input to the interpreter. |
| 15 |
If the formatted structures include objects which are not fundamental Python |
| 16 |
types, the representation may not be loadable. This may be the case if objects |
| 17 |
such as files, sockets, classes, or instances are included, as well as many |
| 18 |
other built-in objects which are not representable as Python constants. |
| 19 |
|
| 20 |
The formatted representation keeps objects on a single line if it can, and |
| 21 |
breaks them onto multiple lines if they don't fit within the allowed width. |
| 22 |
Construct :class:`PrettyPrinter` objects explicitly if you need to adjust the |
| 23 |
width constraint. |
| 24 |
|
| 25 |
.. versionchanged:: 2.5 |
| 26 |
Dictionaries are sorted by key before the display is computed; before 2.5, a |
| 27 |
dictionary was sorted only if its display required more than one line, although |
| 28 |
that wasn't documented. |
| 29 |
|
| 30 |
.. versionchanged:: 2.6 |
| 31 |
Added support for :class:`set` and :class:`frozenset`. |
| 32 |
|
| 33 |
|
| 34 |
The :mod:`pprint` module defines one class: |
| 35 |
|
| 36 |
.. First the implementation class: |
| 37 |
|
| 38 |
|
| 39 |
.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None) |
| 40 |
|
| 41 |
Construct a :class:`PrettyPrinter` instance. This constructor understands |
| 42 |
several keyword parameters. An output stream may be set using the *stream* |
| 43 |
keyword; the only method used on the stream object is the file protocol's |
| 44 |
:meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts |
| 45 |
``sys.stdout``. Three additional parameters may be used to control the |
| 46 |
formatted representation. The keywords are *indent*, *depth*, and *width*. The |
| 47 |
amount of indentation added for each recursive level is specified by *indent*; |
| 48 |
the default is one. Other values can cause output to look a little odd, but can |
| 49 |
make nesting easier to spot. The number of levels which may be printed is |
| 50 |
controlled by *depth*; if the data structure being printed is too deep, the next |
| 51 |
contained level is replaced by ``...``. By default, there is no constraint on |
| 52 |
the depth of the objects being formatted. The desired output width is |
| 53 |
constrained using the *width* parameter; the default is 80 characters. If a |
| 54 |
structure cannot be formatted within the constrained width, a best effort will |
| 55 |
be made. |
| 56 |
|
| 57 |
>>> import pprint |
| 58 |
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] |
| 59 |
>>> stuff.insert(0, stuff[:]) |
| 60 |
>>> pp = pprint.PrettyPrinter(indent=4) |
| 61 |
>>> pp.pprint(stuff) |
| 62 |
[ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'], |
| 63 |
'spam', |
| 64 |
'eggs', |
| 65 |
'lumberjack', |
| 66 |
'knights', |
| 67 |
'ni'] |
| 68 |
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', |
| 69 |
... ('parrot', ('fresh fruit',)))))))) |
| 70 |
>>> pp = pprint.PrettyPrinter(depth=6) |
| 71 |
>>> pp.pprint(tup) |
| 72 |
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...))))))) |
| 73 |
|
| 74 |
The :class:`PrettyPrinter` class supports several derivative functions: |
| 75 |
|
| 76 |
.. function:: pformat(object, indent=1, width=80, depth=None) |
| 77 |
|
| 78 |
Return the formatted representation of *object* as a string. *indent*, *width* |
| 79 |
and *depth* will be passed to the :class:`PrettyPrinter` constructor as |
| 80 |
formatting parameters. |
| 81 |
|
| 82 |
.. versionchanged:: 2.4 |
| 83 |
The parameters *indent*, *width* and *depth* were added. |
| 84 |
|
| 85 |
|
| 86 |
.. function:: pprint(object, stream=None, indent=1, width=80, depth=None) |
| 87 |
|
| 88 |
Prints the formatted representation of *object* on *stream*, followed by a |
| 89 |
newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used in |
| 90 |
the interactive interpreter instead of a :keyword:`print` statement for |
| 91 |
inspecting values. *indent*, *width* and *depth* will be passed to the |
| 92 |
:class:`PrettyPrinter` constructor as formatting parameters. |
| 93 |
|
| 94 |
>>> import pprint |
| 95 |
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] |
| 96 |
>>> stuff.insert(0, stuff) |
| 97 |
>>> pprint.pprint(stuff) |
| 98 |
[<Recursion on list with id=...>, |
| 99 |
'spam', |
| 100 |
'eggs', |
| 101 |
'lumberjack', |
| 102 |
'knights', |
| 103 |
'ni'] |
| 104 |
|
| 105 |
.. versionchanged:: 2.4 |
| 106 |
The parameters *indent*, *width* and *depth* were added. |
| 107 |
|
| 108 |
|
| 109 |
.. function:: isreadable(object) |
| 110 |
|
| 111 |
.. index:: builtin: eval |
| 112 |
|
| 113 |
Determine if the formatted representation of *object* is "readable," or can be |
| 114 |
used to reconstruct the value using :func:`eval`. This always returns ``False`` |
| 115 |
for recursive objects. |
| 116 |
|
| 117 |
>>> pprint.isreadable(stuff) |
| 118 |
False |
| 119 |
|
| 120 |
|
| 121 |
.. function:: isrecursive(object) |
| 122 |
|
| 123 |
Determine if *object* requires a recursive representation. |
| 124 |
|
| 125 |
|
| 126 |
One more support function is also defined: |
| 127 |
|
| 128 |
.. function:: saferepr(object) |
| 129 |
|
| 130 |
Return a string representation of *object*, protected against recursive data |
| 131 |
structures. If the representation of *object* exposes a recursive entry, the |
| 132 |
recursive reference will be represented as ``<Recursion on typename with |
| 133 |
id=number>``. The representation is not otherwise formatted. |
| 134 |
|
| 135 |
>>> pprint.saferepr(stuff) |
| 136 |
"[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']" |
| 137 |
|
| 138 |
|
| 139 |
.. _prettyprinter-objects: |
| 140 |
|
| 141 |
PrettyPrinter Objects |
| 142 |
--------------------- |
| 143 |
|
| 144 |
:class:`PrettyPrinter` instances have the following methods: |
| 145 |
|
| 146 |
|
| 147 |
.. method:: PrettyPrinter.pformat(object) |
| 148 |
|
| 149 |
Return the formatted representation of *object*. This takes into account the |
| 150 |
options passed to the :class:`PrettyPrinter` constructor. |
| 151 |
|
| 152 |
|
| 153 |
.. method:: PrettyPrinter.pprint(object) |
| 154 |
|
| 155 |
Print the formatted representation of *object* on the configured stream, |
| 156 |
followed by a newline. |
| 157 |
|
| 158 |
The following methods provide the implementations for the corresponding |
| 159 |
functions of the same names. Using these methods on an instance is slightly |
| 160 |
more efficient since new :class:`PrettyPrinter` objects don't need to be |
| 161 |
created. |
| 162 |
|
| 163 |
|
| 164 |
.. method:: PrettyPrinter.isreadable(object) |
| 165 |
|
| 166 |
.. index:: builtin: eval |
| 167 |
|
| 168 |
Determine if the formatted representation of the object is "readable," or can be |
| 169 |
used to reconstruct the value using :func:`eval`. Note that this returns |
| 170 |
``False`` for recursive objects. If the *depth* parameter of the |
| 171 |
:class:`PrettyPrinter` is set and the object is deeper than allowed, this |
| 172 |
returns ``False``. |
| 173 |
|
| 174 |
|
| 175 |
.. method:: PrettyPrinter.isrecursive(object) |
| 176 |
|
| 177 |
Determine if the object requires a recursive representation. |
| 178 |
|
| 179 |
This method is provided as a hook to allow subclasses to modify the way objects |
| 180 |
are converted to strings. The default implementation uses the internals of the |
| 181 |
:func:`saferepr` implementation. |
| 182 |
|
| 183 |
|
| 184 |
.. method:: PrettyPrinter.format(object, context, maxlevels, level) |
| 185 |
|
| 186 |
Returns three values: the formatted version of *object* as a string, a flag |
| 187 |
indicating whether the result is readable, and a flag indicating whether |
| 188 |
recursion was detected. The first argument is the object to be presented. The |
| 189 |
second is a dictionary which contains the :func:`id` of objects that are part of |
| 190 |
the current presentation context (direct and indirect containers for *object* |
| 191 |
that are affecting the presentation) as the keys; if an object needs to be |
| 192 |
presented which is already represented in *context*, the third return value |
| 193 |
should be ``True``. Recursive calls to the :meth:`.format` method should add |
| 194 |
additional entries for containers to this dictionary. The third argument, |
| 195 |
*maxlevels*, gives the requested limit to recursion; this will be ``0`` if there |
| 196 |
is no requested limit. This argument should be passed unmodified to recursive |
| 197 |
calls. The fourth argument, *level*, gives the current level; recursive calls |
| 198 |
should be passed a value less than that of the current call. |
| 199 |
|
| 200 |
.. versionadded:: 2.3 |
| 201 |
|
| 202 |
.. _pprint-example: |
| 203 |
|
| 204 |
pprint Example |
| 205 |
-------------- |
| 206 |
|
| 207 |
This example demonstrates several uses of the :func:`pprint` function and its |
| 208 |
parameters. |
| 209 |
|
| 210 |
>>> import pprint |
| 211 |
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', |
| 212 |
... ('parrot', ('fresh fruit',)))))))) |
| 213 |
>>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]] |
| 214 |
>>> pprint.pprint(stuff) |
| 215 |
['aaaaaaaaaa', |
| 216 |
('spam', |
| 217 |
('eggs', |
| 218 |
('lumberjack', |
| 219 |
('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))), |
| 220 |
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'], |
| 221 |
['cccccccccccccccccccc', 'dddddddddddddddddddd']] |
| 222 |
>>> pprint.pprint(stuff, depth=3) |
| 223 |
['aaaaaaaaaa', |
| 224 |
('spam', ('eggs', (...))), |
| 225 |
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'], |
| 226 |
['cccccccccccccccccccc', 'dddddddddddddddddddd']] |
| 227 |
>>> pprint.pprint(stuff, width=60) |
| 228 |
['aaaaaaaaaa', |
| 229 |
('spam', |
| 230 |
('eggs', |
| 231 |
('lumberjack', |
| 232 |
('knights', |
| 233 |
('ni', ('dead', ('parrot', ('fresh fruit',)))))))), |
| 234 |
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', |
| 235 |
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'], |
| 236 |
['cccccccccccccccccccc', 'dddddddddddddddddddd']] |