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, |
| 15 | and breaks them out onto multiple lines if they won't fit within the |
| 16 | width allowed width. Construct PrettyPrinter objects explicitly if |
| 17 | you need to adjust the width constraint. |
| 18 | |
| 19 | The \code{pprint} module defines the following functions: |
| 20 | |
| 21 | \renewcommand{\indexsubitem}{(in module pprint)} |
| 22 | |
| 23 | \begin{funcdesc}{pformat}{object} |
| 24 | Return the formatted representation of \var{object} as a string. The |
| 25 | default parameters for formatting are used. |
| 26 | \end{funcdesc} |
| 27 | |
| 28 | \begin{funcdesc}{pprint}{object\optional{, stream}} |
| 29 | Prints the formatted representation of \var{object} on \var{stream}, |
| 30 | followed by a newline. If \var{stream} is omitted, \code{sys.stdout} |
| 31 | is used. This may be used in the interactive interpreter instead of a |
| 32 | \code{print} command for inspecting values. The default parameters |
| 33 | for formatting are used. |
| 34 | \end{funcdesc} |
| 35 | |
| 36 | \begin{funcdesc}{isreadable}{object} |
| 37 | Determine if the formatted representation of \var{object} is |
| 38 | ``readable,'' or can be used to reconstruct the value using |
| 39 | \code{eval()}. Note that this returns false for recursive objects. |
| 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{isrecursive}{object} |
| 43 | Determine if \var{object} requires a recursive representation. |
| 44 | \end{funcdesc} |
| 45 | |
| 46 | \begin{funcdesc}{saferepr}{object} |
| 47 | Return a string representation of \var{object}, protected against |
| 48 | recursive data structures. If the representation of \var{object} |
| 49 | exposes a recursive entry, the recursive reference will be represented |
| 50 | as \samp{$<$Recursion on \var{typename} with id=\var{number}$>$}. |
| 51 | \end{funcdesc} |
| 52 | |
| 53 | |
| 54 | % Now for the implementation class: |
| 55 | |
| 56 | \begin{funcdesc}{PrettyPrinter}{...} |
| 57 | Construct a PrettyPrinter instance. This constructor understands |
| 58 | several keyword parameters. An output stream may be set using the |
| 59 | \var{stream} keyword; the only method used on the stream object is the |
| 60 | file protocol's \code{write()} method. If not specified, the |
| 61 | PrettyPrinter adopts \code{sys.stdout}. Three additional parameters |
| 62 | may be used to control the formatted representation. The keywords are |
| 63 | \var{indent}, \var{depth}, and \var{width}. The amount of indentation |
| 64 | added for each recursive level is specified by \var{indent}; the |
| 65 | default is one. Other values can cause output to look a little odd, |
| 66 | but can make nesting easier to spot. The number of levels which may |
| 67 | be printed is controlled by \var{depth}; if the data structure being |
| 68 | printed is too deep, the next contained level is replaced by |
| 69 | \samp{...}. By default, there is no constraint on the depth of the |
| 70 | objects being formatted. The desired output width is constrained |
| 71 | using the \var{width} parameter; the default is eighty characters. If |
| 72 | a structure cannot be formatted within the constrained width, a best |
| 73 | effort will be made. |
| 74 | \end{funcdesc} |
| 75 | |
| 76 | |
| 77 | % Guido marked this as a good spot for an example in the template, |
| 78 | % but I think this needs a better location in this module. Not sure where. |
| 79 | |
| 80 | Example: |
| 81 | |
| 82 | \begin{verbatim} |
| 83 | >>> import pprint |
| 84 | >>> stuff = sys.path[:] |
| 85 | >>> stuff.insert(0, stuff) |
| 86 | >>> pprint.pprint(stuff) |
| 87 | [<Recursion on list with id=869440>, |
| 88 | '', |
| 89 | '/usr/local/lib/python1.4', |
| 90 | '/usr/local/lib/python1.4/test', |
| 91 | '/usr/local/lib/python1.4/sunos5', |
| 92 | '/usr/local/lib/python1.4/sharedmodules', |
| 93 | '/usr/local/lib/python1.4/tkinter'] |
| 94 | >>> |
| 95 | >>> stuff[0] = stuff[1:] |
| 96 | >>> pp = pprint.PrettyPrinter(indent=4) |
| 97 | >>> pp.pprint(stuff) |
| 98 | [ [ '', |
| 99 | '/usr/local/lib/python1.4', |
| 100 | '/usr/local/lib/python1.4/test', |
| 101 | '/usr/local/lib/python1.4/sunos5', |
| 102 | '/usr/local/lib/python1.4/sharedmodules', |
| 103 | '/usr/local/lib/python1.4/tkinter'], |
| 104 | '', |
| 105 | '/usr/local/lib/python1.4', |
| 106 | '/usr/local/lib/python1.4/test', |
| 107 | '/usr/local/lib/python1.4/sunos5', |
| 108 | '/usr/local/lib/python1.4/sharedmodules', |
| 109 | '/usr/local/lib/python1.4/tkinter'] |
| 110 | >>> |
| 111 | >>> import parser |
| 112 | >>> tup = parser.ast2tuple( |
| 113 | ... parser.suite(open('pprint.py').read()))[1][1][1] |
| 114 | >>> pp = pprint.PrettyPrinter(depth=6) |
| 115 | >>> pp.pprint(tup) |
| 116 | (266, (267, (307, (287, (288, (...)))))) |
| 117 | \end{verbatim} |
| 118 | |
| 119 | |
| 120 | \subsection{PrettyPrinter Objects} |
| 121 | |
| 122 | PrettyPrinter instances (returned by \code{PrettyPrinter()} above) |
| 123 | have the following methods. |
| 124 | |
| 125 | \renewcommand{\indexsubitem}{(PrettyPrinter method)} |
| 126 | |
| 127 | \begin{funcdesc}{pformat}{object} |
| 128 | Return the formatted representation of \var{object}. This takes into |
| 129 | account the options passed to the PrettyPrinter constructor. |
| 130 | \end{funcdesc} |
| 131 | |
| 132 | \begin{funcdesc}{pprint}{object} |
| 133 | Print the formatted representation of \var{object} on the configured |
| 134 | stream, followed by a newline. |
| 135 | \end{funcdesc} |
| 136 | |
| 137 | The following methods provide the implementations for the |
| 138 | corresponding functions of the same names. Using these methods on an |
| 139 | instance is slightly more efficient since new PrettyPrinter objects |
| 140 | don't need to be created. |
| 141 | |
| 142 | \begin{funcdesc}{isreadable}{object} |
| 143 | Determine if the formatted representation of the object is |
| 144 | ``readable,'' or can be used to reconstruct the value using |
| 145 | \code{eval()}. Note that this returns false for recursive objects. |
| 146 | If the \var{depth} parameter of the PrettyPrinter is set and the |
| 147 | object is deeper than allowed, this returns false. |
| 148 | \end{funcdesc} |
| 149 | |
| 150 | \begin{funcdesc}{isrecursive}{object} |
| 151 | Determine if the object requires a recursive representation. |
| 152 | \end{funcdesc} |