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