Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 1 | %% Author: Fred L. Drake, Jr. <fdrake@acm.org> |
| 2 | |
| 3 | \section{Standard module \sectcode{pprint}} |
| 4 | \stmodindex{pprint} |
| 5 | |
| 6 | The \code{pprint} module provides a capability to ``pretty-print'' |
| 7 | arbitrary Python data structures in a form which can be used as input |
| 8 | to the interpreter. If the formatted structures include objects which |
| 9 | are not fundamental Python types, the representation may not be |
| 10 | loadable. This may be the case if objects such as files, sockets, |
| 11 | classes, or instances are included, as well as many other builtin |
| 12 | objects which are not representable as Python constants. |
| 13 | |
| 14 | The formatted representation keeps objects on a single line if it can, |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 15 | and breaks them onto multiple lines if they don't fit within the |
| 16 | allowed width. Construct PrettyPrinter objects explicitly if you need |
| 17 | to adjust the width constraint. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 18 | |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 19 | The \code{pprint} module defines one class: |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 20 | |
| 21 | \renewcommand{\indexsubitem}{(in module pprint)} |
| 22 | |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 23 | % First the implementation class: |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 24 | |
| 25 | \begin{funcdesc}{PrettyPrinter}{...} |
| 26 | Construct a PrettyPrinter instance. This constructor understands |
| 27 | several keyword parameters. An output stream may be set using the |
| 28 | \var{stream} keyword; the only method used on the stream object is the |
| 29 | file protocol's \code{write()} method. If not specified, the |
| 30 | PrettyPrinter adopts \code{sys.stdout}. Three additional parameters |
| 31 | may be used to control the formatted representation. The keywords are |
| 32 | \var{indent}, \var{depth}, and \var{width}. The amount of indentation |
| 33 | added for each recursive level is specified by \var{indent}; the |
| 34 | default is one. Other values can cause output to look a little odd, |
| 35 | but can make nesting easier to spot. The number of levels which may |
| 36 | be printed is controlled by \var{depth}; if the data structure being |
| 37 | printed is too deep, the next contained level is replaced by |
| 38 | \samp{...}. By default, there is no constraint on the depth of the |
| 39 | objects being formatted. The desired output width is constrained |
| 40 | using the \var{width} parameter; the default is eighty characters. If |
| 41 | a structure cannot be formatted within the constrained width, a best |
| 42 | effort will be made. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 43 | |
| 44 | \begin{verbatim} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 45 | >>> import pprint, sys |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 46 | >>> stuff = sys.path[:] |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 47 | >>> stuff.insert(0, stuff[:]) |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 48 | >>> pp = pprint.PrettyPrinter(indent=4) |
| 49 | >>> pp.pprint(stuff) |
| 50 | [ [ '', |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 51 | '/usr/local/lib/python1.5', |
| 52 | '/usr/local/lib/python1.5/test', |
| 53 | '/usr/local/lib/python1.5/sunos5', |
| 54 | '/usr/local/lib/python1.5/sharedmodules', |
| 55 | '/usr/local/lib/python1.5/tkinter'], |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 56 | '', |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 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'] |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 62 | >>> |
| 63 | >>> import parser |
| 64 | >>> tup = parser.ast2tuple( |
| 65 | ... parser.suite(open('pprint.py').read()))[1][1][1] |
| 66 | >>> pp = pprint.PrettyPrinter(depth=6) |
| 67 | >>> pp.pprint(tup) |
| 68 | (266, (267, (307, (287, (288, (...)))))) |
| 69 | \end{verbatim} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 70 | \end{funcdesc} |
| 71 | |
| 72 | |
| 73 | % Now the derivative functions: |
| 74 | |
| 75 | The PrettyPrinter class supports several derivative functions: |
| 76 | |
| 77 | \begin{funcdesc}{pformat}{object} |
| 78 | Return the formatted representation of \var{object} as a string. The |
| 79 | default parameters for formatting are used. |
| 80 | \end{funcdesc} |
| 81 | |
| 82 | \begin{funcdesc}{pprint}{object\optional{, stream}} |
| 83 | Prints the formatted representation of \var{object} on \var{stream}, |
| 84 | followed by a newline. If \var{stream} is omitted, \code{sys.stdout} |
| 85 | is used. This may be used in the interactive interpreter instead of a |
| 86 | \code{print} command for inspecting values. The default parameters |
| 87 | for formatting are used. |
| 88 | |
| 89 | \begin{verbatim} |
| 90 | >>> stuff = sys.path[:] |
| 91 | >>> stuff.insert(0, stuff) |
| 92 | >>> pprint.pprint(stuff) |
| 93 | [<Recursion on list with id=869440>, |
| 94 | '', |
| 95 | '/usr/local/lib/python1.5', |
| 96 | '/usr/local/lib/python1.5/test', |
| 97 | '/usr/local/lib/python1.5/sunos5', |
| 98 | '/usr/local/lib/python1.5/sharedmodules', |
| 99 | '/usr/local/lib/python1.5/tkinter'] |
| 100 | \end{verbatim} |
| 101 | \end{funcdesc} |
| 102 | |
| 103 | \begin{funcdesc}{isreadable}{object} |
| 104 | Determine if the formatted representation of \var{object} is |
| 105 | ``readable,'' or can be used to reconstruct the value using |
| 106 | \code{eval()}. Note that this returns false for recursive objects. |
| 107 | |
| 108 | \begin{verbatim} |
| 109 | >>> pprint.isreadable(stuff) |
| 110 | 0 |
| 111 | \end{verbatim} |
| 112 | \end{funcdesc} |
| 113 | |
| 114 | \begin{funcdesc}{isrecursive}{object} |
| 115 | Determine if \var{object} requires a recursive representation. |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | |
| 119 | One more support function is also defined: |
| 120 | |
| 121 | \begin{funcdesc}{saferepr}{object} |
| 122 | Return a string representation of \var{object}, protected against |
| 123 | recursive data structures. If the representation of \var{object} |
| 124 | exposes a recursive entry, the recursive reference will be represented |
| 125 | as \samp{$<$Recursion on \var{typename} with id=\var{number}$>$}. The |
| 126 | representation is not otherwise formatted. |
| 127 | |
| 128 | \begin{verbatim} |
| 129 | >>> pprint.saferepr(stuff) |
| 130 | "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.4', '/usr/loca |
| 131 | l/lib/python1.4/test', '/usr/local/lib/python1.4/sunos5', '/usr/local/lib/python |
| 132 | 1.4/sharedmodules', '/usr/local/lib/python1.4/tkinter']" |
| 133 | \end{verbatim} |
| 134 | \end{funcdesc} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | \subsection{PrettyPrinter Objects} |
| 138 | |
| 139 | PrettyPrinter instances (returned by \code{PrettyPrinter()} above) |
| 140 | have the following methods. |
| 141 | |
| 142 | \renewcommand{\indexsubitem}{(PrettyPrinter method)} |
| 143 | |
| 144 | \begin{funcdesc}{pformat}{object} |
| 145 | Return the formatted representation of \var{object}. This takes into |
| 146 | account the options passed to the PrettyPrinter constructor. |
| 147 | \end{funcdesc} |
| 148 | |
| 149 | \begin{funcdesc}{pprint}{object} |
| 150 | Print the formatted representation of \var{object} on the configured |
| 151 | stream, followed by a newline. |
| 152 | \end{funcdesc} |
| 153 | |
| 154 | The following methods provide the implementations for the |
| 155 | corresponding functions of the same names. Using these methods on an |
| 156 | instance is slightly more efficient since new PrettyPrinter objects |
| 157 | don't need to be created. |
| 158 | |
| 159 | \begin{funcdesc}{isreadable}{object} |
| 160 | Determine if the formatted representation of the object is |
| 161 | ``readable,'' or can be used to reconstruct the value using |
| 162 | \code{eval()}. Note that this returns false for recursive objects. |
| 163 | If the \var{depth} parameter of the PrettyPrinter is set and the |
| 164 | object is deeper than allowed, this returns false. |
| 165 | \end{funcdesc} |
| 166 | |
| 167 | \begin{funcdesc}{isrecursive}{object} |
| 168 | Determine if the object requires a recursive representation. |
| 169 | \end{funcdesc} |