Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 1 | %% Author: Fred L. Drake, Jr. <fdrake@acm.org> |
| 2 | |
Fred Drake | 2303d31 | 1997-12-17 14:07:25 +0000 | [diff] [blame] | 3 | \section{Standard Module \sectcode{pprint}} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 4 | \stmodindex{pprint} |
Fred Drake | 2303d31 | 1997-12-17 14:07:25 +0000 | [diff] [blame] | 5 | \label{module-pprint} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 6 | |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 7 | The \module{pprint} module provides a capability to ``pretty-print'' |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 8 | arbitrary Python data structures in a form which can be used as input |
| 9 | to the interpreter. If the formatted structures include objects which |
| 10 | are not fundamental Python types, the representation may not be |
| 11 | loadable. This may be the case if objects such as files, sockets, |
| 12 | classes, or instances are included, as well as many other builtin |
| 13 | objects which are not representable as Python constants. |
| 14 | |
| 15 | The formatted representation keeps objects on a single line if it can, |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 16 | 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] | 17 | allowed width. Construct \class{PrettyPrinter} objects explicitly if |
| 18 | you need to adjust the width constraint. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 19 | |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 20 | The \module{pprint} module defines one class: |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 21 | |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 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 | |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 25 | \begin{classdesc}{PrettyPrinter}{...} |
| 26 | Construct a \class{PrettyPrinter} instance. This constructor |
| 27 | understands several keyword parameters. An output stream may be set |
| 28 | using the \var{stream} keyword; the only method used on the stream |
| 29 | object is the file protocol's \method{write()} method. If not |
| 30 | specified, the \class{PrettyPrinter} adopts \code{sys.stdout}. Three |
| 31 | additional parameters may be used to control the formatted |
| 32 | representation. The keywords are \var{indent}, \var{depth}, and |
| 33 | \var{width}. The amount of indentation added for each recursive level |
| 34 | is specified by \var{indent}; the default is one. Other values can |
| 35 | cause output to look a little odd, but can make nesting easier to |
| 36 | spot. The number of levels which may be printed is controlled by |
| 37 | \var{depth}; if the data structure being printed is too deep, the next |
| 38 | contained level is replaced by \samp{...}. By default, there is no |
| 39 | constraint on the depth of the objects being formatted. The desired |
| 40 | output width is constrained using the \var{width} parameter; the |
| 41 | default is eighty characters. If a structure cannot be formatted |
| 42 | within the constrained width, a best 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 | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 70 | \end{classdesc} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | % Now the derivative functions: |
| 74 | |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 75 | The \class{PrettyPrinter} class supports several derivative functions: |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 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 |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 86 | \keyword{print} statement for inspecting values. The default |
| 87 | parameters for formatting are used. |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 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 |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 106 | \function{eval()}\bifuncindex{eval}. Note that this returns false for |
| 107 | recursive objects. |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 108 | |
| 109 | \begin{verbatim} |
| 110 | >>> pprint.isreadable(stuff) |
| 111 | 0 |
| 112 | \end{verbatim} |
| 113 | \end{funcdesc} |
| 114 | |
| 115 | \begin{funcdesc}{isrecursive}{object} |
| 116 | Determine if \var{object} requires a recursive representation. |
| 117 | \end{funcdesc} |
| 118 | |
| 119 | |
| 120 | One more support function is also defined: |
| 121 | |
| 122 | \begin{funcdesc}{saferepr}{object} |
| 123 | Return a string representation of \var{object}, protected against |
| 124 | recursive data structures. If the representation of \var{object} |
| 125 | exposes a recursive entry, the recursive reference will be represented |
Fred Drake | 2303d31 | 1997-12-17 14:07:25 +0000 | [diff] [blame] | 126 | as \samp{<Recursion on \var{typename} with id=\var{number}>}. The |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 127 | representation is not otherwise formatted. |
Fred Drake | b55f9d3 | 1998-03-08 07:03:27 +0000 | [diff] [blame] | 128 | \end{funcdesc} |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 129 | |
Fred Drake | b55f9d3 | 1998-03-08 07:03:27 +0000 | [diff] [blame] | 130 | % This example is outside the {funcdesc} to keep it from running over |
| 131 | % the right margin. |
Fred Drake | 12d9eac | 1997-07-24 15:39:16 +0000 | [diff] [blame] | 132 | \begin{verbatim} |
| 133 | >>> pprint.saferepr(stuff) |
| 134 | "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.4', '/usr/loca |
| 135 | l/lib/python1.4/test', '/usr/local/lib/python1.4/sunos5', '/usr/local/lib/python |
| 136 | 1.4/sharedmodules', '/usr/local/lib/python1.4/tkinter']" |
| 137 | \end{verbatim} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 138 | |
| 139 | |
| 140 | \subsection{PrettyPrinter Objects} |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 141 | \label{PrettyPrinter Objects} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 142 | |
Fred Drake | b55f9d3 | 1998-03-08 07:03:27 +0000 | [diff] [blame] | 143 | \class{PrettyPrinter} instances have the following methods: |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 144 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 145 | \setindexsubitem{(PrettyPrinter method)} |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 146 | |
| 147 | \begin{funcdesc}{pformat}{object} |
| 148 | Return the formatted representation of \var{object}. This takes into |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 149 | account the options passed to the \class{PrettyPrinter} constructor. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 150 | \end{funcdesc} |
| 151 | |
| 152 | \begin{funcdesc}{pprint}{object} |
| 153 | Print the formatted representation of \var{object} on the configured |
| 154 | stream, followed by a newline. |
| 155 | \end{funcdesc} |
| 156 | |
| 157 | The following methods provide the implementations for the |
| 158 | corresponding functions of the same names. Using these methods on an |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 159 | instance is slightly more efficient since new \class{PrettyPrinter} |
| 160 | objects don't need to be created. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 161 | |
| 162 | \begin{funcdesc}{isreadable}{object} |
| 163 | Determine if the formatted representation of the object is |
| 164 | ``readable,'' or can be used to reconstruct the value using |
Fred Drake | 2c8aa65 | 1998-02-20 06:03:52 +0000 | [diff] [blame] | 165 | \function{eval()}\bifuncindex{eval}. Note that this returns false for |
| 166 | recursive objects. If the \var{depth} parameter of the |
| 167 | \class{PrettyPrinter} is set and the object is deeper than allowed, |
| 168 | this returns false. |
Fred Drake | ee8d3ca | 1997-07-18 20:41:58 +0000 | [diff] [blame] | 169 | \end{funcdesc} |
| 170 | |
| 171 | \begin{funcdesc}{isrecursive}{object} |
| 172 | Determine if the object requires a recursive representation. |
| 173 | \end{funcdesc} |