Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{pprint} --- |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 2 | Data pretty printer} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 2a2f1fe | 1999-02-18 21:10:32 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{pprint} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Data pretty printer.} |
Fred Drake | 2a2f1fe | 1999-02-18 21:10:32 +0000 | [diff] [blame] | 6 | \moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 7 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 8 | |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 9 | |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 10 | The \module{pprint} module provides a capability to ``pretty-print'' |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 11 | arbitrary Python data structures in a form which can be used as input |
| 12 | to the interpreter. If the formatted structures include objects which |
| 13 | are not fundamental Python types, the representation may not be |
| 14 | loadable. This may be the case if objects such as files, sockets, |
| 15 | classes, or instances are included, as well as many other builtin |
| 16 | objects which are not representable as Python constants. |
| 17 | |
| 18 | The formatted representation keeps objects on a single line if it can, |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 19 | and breaks them onto multiple lines if they don't fit within the |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 20 | allowed width. Construct \class{PrettyPrinter} objects explicitly if |
| 21 | you need to adjust the width constraint. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 22 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 23 | \versionchanged[Dictionaries are sorted by key before the display is |
| 24 | computed; before 2.5, a dictionary was sorted only if its display |
| 25 | required more than one line, although that wasn't documented]{2.5} |
| 26 | |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 27 | The \module{pprint} module defines one class: |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 28 | |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 29 | |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 30 | % First the implementation class: |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 31 | |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 32 | \begin{classdesc}{PrettyPrinter}{...} |
| 33 | Construct a \class{PrettyPrinter} instance. This constructor |
| 34 | understands several keyword parameters. An output stream may be set |
| 35 | using the \var{stream} keyword; the only method used on the stream |
| 36 | object is the file protocol's \method{write()} method. If not |
| 37 | specified, the \class{PrettyPrinter} adopts \code{sys.stdout}. Three |
| 38 | additional parameters may be used to control the formatted |
| 39 | representation. The keywords are \var{indent}, \var{depth}, and |
| 40 | \var{width}. The amount of indentation added for each recursive level |
| 41 | is specified by \var{indent}; the default is one. Other values can |
| 42 | cause output to look a little odd, but can make nesting easier to |
| 43 | spot. The number of levels which may be printed is controlled by |
| 44 | \var{depth}; if the data structure being printed is too deep, the next |
| 45 | contained level is replaced by \samp{...}. By default, there is no |
| 46 | constraint on the depth of the objects being formatted. The desired |
| 47 | output width is constrained using the \var{width} parameter; the |
| 48 | default is eighty characters. If a structure cannot be formatted |
| 49 | within the constrained width, a best effort will be made. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 50 | |
| 51 | \begin{verbatim} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 52 | >>> import pprint, sys |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 53 | >>> stuff = sys.path[:] |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 54 | >>> stuff.insert(0, stuff[:]) |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 55 | >>> pp = pprint.PrettyPrinter(indent=4) |
| 56 | >>> pp.pprint(stuff) |
| 57 | [ [ '', |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 58 | '/usr/local/lib/python1.5', |
| 59 | '/usr/local/lib/python1.5/test', |
| 60 | '/usr/local/lib/python1.5/sunos5', |
| 61 | '/usr/local/lib/python1.5/sharedmodules', |
| 62 | '/usr/local/lib/python1.5/tkinter'], |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 63 | '', |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 64 | '/usr/local/lib/python1.5', |
| 65 | '/usr/local/lib/python1.5/test', |
| 66 | '/usr/local/lib/python1.5/sunos5', |
| 67 | '/usr/local/lib/python1.5/sharedmodules', |
| 68 | '/usr/local/lib/python1.5/tkinter'] |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 69 | >>> |
| 70 | >>> import parser |
| 71 | >>> tup = parser.ast2tuple( |
| 72 | ... parser.suite(open('pprint.py').read()))[1][1][1] |
| 73 | >>> pp = pprint.PrettyPrinter(depth=6) |
| 74 | >>> pp.pprint(tup) |
| 75 | (266, (267, (307, (287, (288, (...)))))) |
| 76 | \end{verbatim} |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 77 | \end{classdesc} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | % Now the derivative functions: |
| 81 | |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 82 | The \class{PrettyPrinter} class supports several derivative functions: |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 83 | |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 84 | \begin{funcdesc}{pformat}{object\optional{, indent\optional{, |
| 85 | width\optional{, depth}}}} |
| 86 | Return the formatted representation of \var{object} as a string. \var{indent}, |
| 87 | \var{width} and \var{depth} will be passed to the \class{PrettyPrinter} |
| 88 | constructor as formatting parameters. |
| 89 | \versionchanged[The parameters \var{indent}, \var{width} and \var{depth} |
| 90 | were added]{2.4} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 91 | \end{funcdesc} |
| 92 | |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 93 | \begin{funcdesc}{pprint}{object\optional{, stream\optional{, |
| 94 | indent\optional{, width\optional{, depth}}}}} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 95 | Prints the formatted representation of \var{object} on \var{stream}, |
| 96 | followed by a newline. If \var{stream} is omitted, \code{sys.stdout} |
| 97 | is used. This may be used in the interactive interpreter instead of a |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 98 | \keyword{print} statement for inspecting values. \var{indent}, |
| 99 | \var{width} and \var{depth} will be passed to the \class{PrettyPrinter} |
| 100 | constructor as formatting parameters. |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 101 | |
| 102 | \begin{verbatim} |
| 103 | >>> stuff = sys.path[:] |
| 104 | >>> stuff.insert(0, stuff) |
| 105 | >>> pprint.pprint(stuff) |
| 106 | [<Recursion on list with id=869440>, |
| 107 | '', |
| 108 | '/usr/local/lib/python1.5', |
| 109 | '/usr/local/lib/python1.5/test', |
| 110 | '/usr/local/lib/python1.5/sunos5', |
| 111 | '/usr/local/lib/python1.5/sharedmodules', |
| 112 | '/usr/local/lib/python1.5/tkinter'] |
| 113 | \end{verbatim} |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 114 | \versionchanged[The parameters \var{indent}, \var{width} and \var{depth} |
| 115 | were added]{2.4} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{isreadable}{object} |
| 119 | Determine if the formatted representation of \var{object} is |
| 120 | ``readable,'' or can be used to reconstruct the value using |
Fred Drake | 14c198b | 1998-04-03 07:11:32 +0000 | [diff] [blame] | 121 | \function{eval()}\bifuncindex{eval}. This always returns false for |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 122 | recursive objects. |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 123 | |
| 124 | \begin{verbatim} |
| 125 | >>> pprint.isreadable(stuff) |
Martin v. Löwis | ccabed3 | 2003-11-27 19:48:03 +0000 | [diff] [blame] | 126 | False |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 127 | \end{verbatim} |
| 128 | \end{funcdesc} |
| 129 | |
| 130 | \begin{funcdesc}{isrecursive}{object} |
| 131 | Determine if \var{object} requires a recursive representation. |
| 132 | \end{funcdesc} |
| 133 | |
| 134 | |
| 135 | One more support function is also defined: |
| 136 | |
| 137 | \begin{funcdesc}{saferepr}{object} |
| 138 | Return a string representation of \var{object}, protected against |
| 139 | recursive data structures. If the representation of \var{object} |
| 140 | exposes a recursive entry, the recursive reference will be represented |
Fred Drake | 2303d31 | 1997-12-17 14:07:25 +0000 | [diff] [blame] | 141 | as \samp{<Recursion on \var{typename} with id=\var{number}>}. The |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 142 | representation is not otherwise formatted. |
Fred Drake | b55f9d3 | 1998-03-08 07:03:27 +0000 | [diff] [blame] | 143 | \end{funcdesc} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 144 | |
Fred Drake | b55f9d3 | 1998-03-08 07:03:27 +0000 | [diff] [blame] | 145 | % This example is outside the {funcdesc} to keep it from running over |
| 146 | % the right margin. |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 147 | \begin{verbatim} |
| 148 | >>> pprint.saferepr(stuff) |
Fred Drake | 14c198b | 1998-04-03 07:11:32 +0000 | [diff] [blame] | 149 | "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.5', '/usr/loca |
| 150 | l/lib/python1.5/test', '/usr/local/lib/python1.5/sunos5', '/usr/local/lib/python |
| 151 | 1.5/sharedmodules', '/usr/local/lib/python1.5/tkinter']" |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 152 | \end{verbatim} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | \subsection{PrettyPrinter Objects} |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 156 | \label{PrettyPrinter Objects} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 157 | |
Fred Drake | b55f9d3 | 1998-03-08 07:03:27 +0000 | [diff] [blame] | 158 | \class{PrettyPrinter} instances have the following methods: |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 159 | |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 160 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 161 | \begin{methoddesc}{pformat}{object} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 162 | Return the formatted representation of \var{object}. This takes into |
Fredrik Lundh | bb2bf2c | 2005-12-25 12:05:42 +0000 | [diff] [blame] | 163 | account the options passed to the \class{PrettyPrinter} constructor. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 164 | \end{methoddesc} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 165 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 166 | \begin{methoddesc}{pprint}{object} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 167 | Print the formatted representation of \var{object} on the configured |
| 168 | stream, followed by a newline. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 169 | \end{methoddesc} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 170 | |
| 171 | The following methods provide the implementations for the |
| 172 | corresponding functions of the same names. Using these methods on an |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 173 | instance is slightly more efficient since new \class{PrettyPrinter} |
| 174 | objects don't need to be created. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 175 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 176 | \begin{methoddesc}{isreadable}{object} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 177 | Determine if the formatted representation of the object is |
| 178 | ``readable,'' or can be used to reconstruct the value using |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 179 | \function{eval()}\bifuncindex{eval}. Note that this returns false for |
| 180 | recursive objects. If the \var{depth} parameter of the |
| 181 | \class{PrettyPrinter} is set and the object is deeper than allowed, |
| 182 | this returns false. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 183 | \end{methoddesc} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 184 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 185 | \begin{methoddesc}{isrecursive}{object} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 186 | Determine if the object requires a recursive representation. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 187 | \end{methoddesc} |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 188 | |
| 189 | This method is provided as a hook to allow subclasses to modify the |
| 190 | way objects are converted to strings. The default implementation uses |
| 191 | the internals of the \function{saferepr()} implementation. |
| 192 | |
| 193 | \begin{methoddesc}{format}{object, context, maxlevels, level} |
| 194 | Returns three values: the formatted version of \var{object} as a |
| 195 | string, a flag indicating whether the result is readable, and a flag |
| 196 | indicating whether recursion was detected. The first argument is the |
| 197 | object to be presented. The second is a dictionary which contains the |
| 198 | \function{id()} of objects that are part of the current presentation |
| 199 | context (direct and indirect containers for \var{object} that are |
| 200 | affecting the presentation) as the keys; if an object needs to be |
| 201 | presented which is already represented in \var{context}, the third |
| 202 | return value should be true. Recursive calls to the \method{format()} |
Raymond Hettinger | 6880431 | 2005-01-01 00:28:46 +0000 | [diff] [blame] | 203 | method should add additional entries for containers to this |
Fredrik Lundh | 428b413 | 2005-12-25 11:36:43 +0000 | [diff] [blame] | 204 | dictionary. The third argument, \var{maxlevels}, gives the requested |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 205 | limit to recursion; this will be \code{0} if there is no requested |
| 206 | limit. This argument should be passed unmodified to recursive calls. |
Fredrik Lundh | 428b413 | 2005-12-25 11:36:43 +0000 | [diff] [blame] | 207 | The fourth argument, \var{level}, gives the current level; recursive |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 208 | calls should be passed a value less than that of the current call. |
| 209 | \versionadded{2.3} |
| 210 | \end{methoddesc} |