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 | |
| 24 | The :mod:`pprint` module defines one class: |
| 25 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 26 | .. First the implementation class: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
| 28 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 29 | .. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
| 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 Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 45 | be made. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 47 | >>> import pprint |
| 48 | >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | >>> stuff.insert(0, stuff[:]) |
| 50 | >>> pp = pprint.PrettyPrinter(indent=4) |
| 51 | >>> pp.pprint(stuff) |
Georg Brandl | 3ccb787 | 2008-07-16 03:00:45 +0000 | [diff] [blame] | 52 | [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'], |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 53 | 'spam', |
| 54 | 'eggs', |
| 55 | 'lumberjack', |
| 56 | 'knights', |
| 57 | 'ni'] |
| 58 | >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', |
| 59 | ... ('parrot', ('fresh fruit',)))))))) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | >>> pp = pprint.PrettyPrinter(depth=6) |
| 61 | >>> pp.pprint(tup) |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 62 | ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...))))))) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 64 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | The :class:`PrettyPrinter` class supports several derivative functions: |
| 66 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 67 | .. function:: pformat(object, indent=1, width=80, depth=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 74 | .. function:: pprint(object, stream=None, indent=1, width=80, depth=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
| 76 | Prints the formatted representation of *object* on *stream*, followed by a |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 77 | 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] | 78 | 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 Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 81 | :class:`PrettyPrinter` constructor as formatting parameters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 83 | >>> import pprint |
| 84 | >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | >>> stuff.insert(0, stuff) |
| 86 | >>> pprint.pprint(stuff) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 87 | [<Recursion on list with id=...>, |
| 88 | 'spam', |
| 89 | 'eggs', |
| 90 | 'lumberjack', |
| 91 | 'knights', |
| 92 | 'ni'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
| 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 Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 101 | for recursive objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
| 103 | >>> pprint.isreadable(stuff) |
| 104 | False |
| 105 | |
| 106 | |
| 107 | .. function:: isrecursive(object) |
| 108 | |
| 109 | Determine if *object* requires a recursive representation. |
| 110 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 112 | One more support function is also defined: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 121 | >>> pprint.saferepr(stuff) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 122 | "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | .. _prettyprinter-objects: |
| 126 | |
| 127 | PrettyPrinter 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 | |
| 144 | The following methods provide the implementations for the corresponding |
| 145 | functions of the same names. Using these methods on an instance is slightly |
| 146 | more efficient since new :class:`PrettyPrinter` objects don't need to be |
| 147 | created. |
| 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 | |
| 165 | This method is provided as a hook to allow subclasses to modify the way objects |
| 166 | are 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 Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 185 | |
| 186 | |
| 187 | .. _pprint-example: |
| 188 | |
| 189 | pprint Example |
| 190 | -------------- |
| 191 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 192 | This example demonstrates several uses of the :func:`pprint` function and its |
| 193 | parameters. |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 194 | |
| 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 Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 209 | ('spam', ('eggs', (...))), |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 210 | ['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 | |