Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 | |
| 10 | The :mod:`pprint` module provides a capability to "pretty-print" arbitrary |
| 11 | Python data structures in a form which can be used as input to the interpreter. |
| 12 | If the formatted structures include objects which are not fundamental Python |
| 13 | types, the representation may not be loadable. This may be the case if objects |
| 14 | such as files, sockets, classes, or instances are included, as well as many |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 15 | other built-in objects which are not representable as Python constants. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
| 17 | The formatted representation keeps objects on a single line if it can, and |
| 18 | breaks them onto multiple lines if they don't fit within the allowed width. |
| 19 | Construct :class:`PrettyPrinter` objects explicitly if you need to adjust the |
| 20 | width constraint. |
| 21 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 22 | Dictionaries are sorted by key before the display is computed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 24 | .. seealso:: |
| 25 | |
| 26 | Latest version of the :source:`pprint module Python source code |
| 27 | <Lib/pprint.py>` |
| 28 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | The :mod:`pprint` module defines one class: |
| 30 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 31 | .. First the implementation class: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 34 | .. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
| 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 Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 50 | be made. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 52 | >>> import pprint |
| 53 | >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | >>> stuff.insert(0, stuff[:]) |
| 55 | >>> pp = pprint.PrettyPrinter(indent=4) |
| 56 | >>> pp.pprint(stuff) |
Georg Brandl | 3ccb787 | 2008-07-16 03:00:45 +0000 | [diff] [blame] | 57 | [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'], |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 58 | 'spam', |
| 59 | 'eggs', |
| 60 | 'lumberjack', |
| 61 | 'knights', |
| 62 | 'ni'] |
| 63 | >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', |
| 64 | ... ('parrot', ('fresh fruit',)))))))) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | >>> pp = pprint.PrettyPrinter(depth=6) |
| 66 | >>> pp.pprint(tup) |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 67 | ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...))))))) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 69 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | The :class:`PrettyPrinter` class supports several derivative functions: |
| 71 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 72 | .. function:: pformat(object, indent=1, width=80, depth=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 79 | .. function:: pprint(object, stream=None, indent=1, width=80, depth=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
| 81 | Prints the formatted representation of *object* on *stream*, followed by a |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 82 | newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 83 | 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 Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 86 | :class:`PrettyPrinter` constructor as formatting parameters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 88 | >>> import pprint |
| 89 | >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | >>> stuff.insert(0, stuff) |
| 91 | >>> pprint.pprint(stuff) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 92 | [<Recursion on list with id=...>, |
| 93 | 'spam', |
| 94 | 'eggs', |
| 95 | 'lumberjack', |
| 96 | 'knights', |
| 97 | 'ni'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
| 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 Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 106 | for recursive objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
| 108 | >>> pprint.isreadable(stuff) |
| 109 | False |
| 110 | |
| 111 | |
| 112 | .. function:: isrecursive(object) |
| 113 | |
| 114 | Determine if *object* requires a recursive representation. |
| 115 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 117 | One more support function is also defined: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 | >>> pprint.saferepr(stuff) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 127 | "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | .. _prettyprinter-objects: |
| 131 | |
| 132 | PrettyPrinter 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 | |
| 149 | The following methods provide the implementations for the corresponding |
| 150 | functions of the same names. Using these methods on an instance is slightly |
| 151 | more efficient since new :class:`PrettyPrinter` objects don't need to be |
| 152 | created. |
| 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 | |
| 170 | This method is provided as a hook to allow subclasses to modify the way objects |
| 171 | are 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 Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 190 | |
| 191 | |
| 192 | .. _pprint-example: |
| 193 | |
| 194 | pprint Example |
| 195 | -------------- |
| 196 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 197 | This example demonstrates several uses of the :func:`pprint` function and its |
| 198 | parameters. |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 199 | |
| 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 Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 214 | ('spam', ('eggs', (...))), |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 215 | ['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 | |