Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
| 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 | |
| 28 | The :mod:`pprint` module defines one class: |
| 29 | |
| 30 | .. % First the implementation class: |
| 31 | |
| 32 | |
| 33 | .. class:: PrettyPrinter(...) |
| 34 | |
| 35 | Construct a :class:`PrettyPrinter` instance. This constructor understands |
| 36 | several keyword parameters. An output stream may be set using the *stream* |
| 37 | keyword; the only method used on the stream object is the file protocol's |
| 38 | :meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts |
| 39 | ``sys.stdout``. Three additional parameters may be used to control the |
| 40 | formatted representation. The keywords are *indent*, *depth*, and *width*. The |
| 41 | amount of indentation added for each recursive level is specified by *indent*; |
| 42 | the default is one. Other values can cause output to look a little odd, but can |
| 43 | make nesting easier to spot. The number of levels which may be printed is |
| 44 | controlled by *depth*; if the data structure being printed is too deep, the next |
| 45 | contained level is replaced by ``...``. By default, there is no constraint on |
| 46 | the depth of the objects being formatted. The desired output width is |
| 47 | constrained using the *width* parameter; the default is 80 characters. If a |
| 48 | structure cannot be formatted within the constrained width, a best effort will |
| 49 | be made. :: |
| 50 | |
| 51 | >>> import pprint, sys |
| 52 | >>> stuff = sys.path[:] |
| 53 | >>> stuff.insert(0, stuff[:]) |
| 54 | >>> pp = pprint.PrettyPrinter(indent=4) |
| 55 | >>> pp.pprint(stuff) |
| 56 | [ [ '', |
| 57 | '/usr/local/lib/python1.5', |
| 58 | '/usr/local/lib/python1.5/test', |
| 59 | '/usr/local/lib/python1.5/sunos5', |
| 60 | '/usr/local/lib/python1.5/sharedmodules', |
| 61 | '/usr/local/lib/python1.5/tkinter'], |
| 62 | '', |
| 63 | '/usr/local/lib/python1.5', |
| 64 | '/usr/local/lib/python1.5/test', |
| 65 | '/usr/local/lib/python1.5/sunos5', |
| 66 | '/usr/local/lib/python1.5/sharedmodules', |
| 67 | '/usr/local/lib/python1.5/tkinter'] |
| 68 | >>> |
| 69 | >>> import parser |
| 70 | >>> tup = parser.ast2tuple( |
| 71 | ... parser.suite(open('pprint.py').read()))[1][1][1] |
| 72 | >>> pp = pprint.PrettyPrinter(depth=6) |
| 73 | >>> pp.pprint(tup) |
| 74 | (266, (267, (307, (287, (288, (...)))))) |
| 75 | |
| 76 | The :class:`PrettyPrinter` class supports several derivative functions: |
| 77 | |
| 78 | .. % Now the derivative functions: |
| 79 | |
| 80 | |
| 81 | .. function:: pformat(object[, indent[, width[, depth]]]) |
| 82 | |
| 83 | Return the formatted representation of *object* as a string. *indent*, *width* |
| 84 | and *depth* will be passed to the :class:`PrettyPrinter` constructor as |
| 85 | formatting parameters. |
| 86 | |
| 87 | .. versionchanged:: 2.4 |
| 88 | The parameters *indent*, *width* and *depth* were added. |
| 89 | |
| 90 | |
| 91 | .. function:: pprint(object[, stream[, indent[, width[, depth]]]]) |
| 92 | |
| 93 | Prints the formatted representation of *object* on *stream*, followed by a |
| 94 | newline. If *stream* is omitted, ``sys.stdout`` is used. This may be used in |
| 95 | the interactive interpreter instead of a :keyword:`print` statement for |
| 96 | inspecting values. *indent*, *width* and *depth* will be passed to the |
| 97 | :class:`PrettyPrinter` constructor as formatting parameters. :: |
| 98 | |
| 99 | >>> stuff = sys.path[:] |
| 100 | >>> stuff.insert(0, stuff) |
| 101 | >>> pprint.pprint(stuff) |
| 102 | [<Recursion on list with id=869440>, |
| 103 | '', |
| 104 | '/usr/local/lib/python1.5', |
| 105 | '/usr/local/lib/python1.5/test', |
| 106 | '/usr/local/lib/python1.5/sunos5', |
| 107 | '/usr/local/lib/python1.5/sharedmodules', |
| 108 | '/usr/local/lib/python1.5/tkinter'] |
| 109 | |
| 110 | .. versionchanged:: 2.4 |
| 111 | The parameters *indent*, *width* and *depth* were added. |
| 112 | |
| 113 | |
| 114 | .. function:: isreadable(object) |
| 115 | |
| 116 | .. index:: builtin: eval |
| 117 | |
| 118 | Determine if the formatted representation of *object* is "readable," or can be |
| 119 | used to reconstruct the value using :func:`eval`. This always returns ``False`` |
| 120 | for recursive objects. :: |
| 121 | |
| 122 | >>> pprint.isreadable(stuff) |
| 123 | False |
| 124 | |
| 125 | |
| 126 | .. function:: isrecursive(object) |
| 127 | |
| 128 | Determine if *object* requires a recursive representation. |
| 129 | |
| 130 | One more support function is also defined: |
| 131 | |
| 132 | |
| 133 | .. function:: saferepr(object) |
| 134 | |
| 135 | Return a string representation of *object*, protected against recursive data |
| 136 | structures. If the representation of *object* exposes a recursive entry, the |
| 137 | recursive reference will be represented as ``<Recursion on typename with |
| 138 | id=number>``. The representation is not otherwise formatted. |
| 139 | |
| 140 | .. % This example is outside the {funcdesc} to keep it from running over |
| 141 | .. % the right margin. |
| 142 | |
| 143 | :: |
| 144 | |
| 145 | >>> pprint.saferepr(stuff) |
| 146 | "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.5', '/usr/loca |
| 147 | l/lib/python1.5/test', '/usr/local/lib/python1.5/sunos5', '/usr/local/lib/python |
| 148 | 1.5/sharedmodules', '/usr/local/lib/python1.5/tkinter']" |
| 149 | |
| 150 | |
| 151 | .. _prettyprinter-objects: |
| 152 | |
| 153 | PrettyPrinter Objects |
| 154 | --------------------- |
| 155 | |
| 156 | :class:`PrettyPrinter` instances have the following methods: |
| 157 | |
| 158 | |
| 159 | .. method:: PrettyPrinter.pformat(object) |
| 160 | |
| 161 | Return the formatted representation of *object*. This takes into account the |
| 162 | options passed to the :class:`PrettyPrinter` constructor. |
| 163 | |
| 164 | |
| 165 | .. method:: PrettyPrinter.pprint(object) |
| 166 | |
| 167 | Print the formatted representation of *object* on the configured stream, |
| 168 | followed by a newline. |
| 169 | |
| 170 | The following methods provide the implementations for the corresponding |
| 171 | functions of the same names. Using these methods on an instance is slightly |
| 172 | more efficient since new :class:`PrettyPrinter` objects don't need to be |
| 173 | created. |
| 174 | |
| 175 | |
| 176 | .. method:: PrettyPrinter.isreadable(object) |
| 177 | |
| 178 | .. index:: builtin: eval |
| 179 | |
| 180 | Determine if the formatted representation of the object is "readable," or can be |
| 181 | used to reconstruct the value using :func:`eval`. Note that this returns |
| 182 | ``False`` for recursive objects. If the *depth* parameter of the |
| 183 | :class:`PrettyPrinter` is set and the object is deeper than allowed, this |
| 184 | returns ``False``. |
| 185 | |
| 186 | |
| 187 | .. method:: PrettyPrinter.isrecursive(object) |
| 188 | |
| 189 | Determine if the object requires a recursive representation. |
| 190 | |
| 191 | This method is provided as a hook to allow subclasses to modify the way objects |
| 192 | are converted to strings. The default implementation uses the internals of the |
| 193 | :func:`saferepr` implementation. |
| 194 | |
| 195 | |
| 196 | .. method:: PrettyPrinter.format(object, context, maxlevels, level) |
| 197 | |
| 198 | Returns three values: the formatted version of *object* as a string, a flag |
| 199 | indicating whether the result is readable, and a flag indicating whether |
| 200 | recursion was detected. The first argument is the object to be presented. The |
| 201 | second is a dictionary which contains the :func:`id` of objects that are part of |
| 202 | the current presentation context (direct and indirect containers for *object* |
| 203 | that are affecting the presentation) as the keys; if an object needs to be |
| 204 | presented which is already represented in *context*, the third return value |
| 205 | should be ``True``. Recursive calls to the :meth:`format` method should add |
| 206 | additional entries for containers to this dictionary. The third argument, |
| 207 | *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there |
| 208 | is no requested limit. This argument should be passed unmodified to recursive |
| 209 | calls. The fourth argument, *level*, gives the current level; recursive calls |
| 210 | should be passed a value less than that of the current call. |
| 211 | |
| 212 | .. versionadded:: 2.3 |
| 213 | |