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