Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libpprint.tex @ 7277:bf4e790bd61a legacy-trunk
Remove all \bcode / \ecode cruft; this is no longer needed. See previous
checkin of myformat.sty.
Change "\renewcommand{\indexsubitem}{(...)}" to "\setindexsubitem{(...)}"
everywhere.
Some other minor nits that I happened to come across.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Fri, 13 Feb 1998 06:58:54 +0000 |
| parents | adb300cbd566 |
| children | 11e9337cda3d |
| rev | line source |
|---|---|
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 %% Author: Fred L. Drake, Jr. <fdrake@acm.org> |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 |
|
6901
adb300cbd566
Make the word "Module" in the section header start with an upper-case "M".
Fred Drake <fdrake@acm.org>
parents:
6647
diff
changeset
|
3 \section{Standard Module \sectcode{pprint}} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 \stmodindex{pprint} |
|
6901
adb300cbd566
Make the word "Module" in the section header start with an upper-case "M".
Fred Drake <fdrake@acm.org>
parents:
6647
diff
changeset
|
5 \label{module-pprint} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 The \code{pprint} module provides a capability to ``pretty-print'' |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
8 arbitrary Python data structures in a form which can be used as input |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 to the interpreter. If the formatted structures include objects which |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 are not fundamental Python types, the representation may not be |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
11 loadable. This may be the case if objects such as files, sockets, |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 classes, or instances are included, as well as many other builtin |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 objects which are not representable as Python constants. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 The formatted representation keeps objects on a single line if it can, |
| 5896 | 16 and breaks them onto multiple lines if they don't fit within the |
| 17 allowed width. Construct PrettyPrinter objects explicitly if you need | |
| 18 to adjust the width constraint. | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 |
| 5896 | 20 The \code{pprint} module defines one class: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 |
|
7277
bf4e790bd61a
Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents:
6901
diff
changeset
|
22 \setindexsubitem{(in module pprint)} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
23 |
| 5896 | 24 % First the implementation class: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
25 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 \begin{funcdesc}{PrettyPrinter}{...} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 Construct a PrettyPrinter instance. This constructor understands |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 several keyword parameters. An output stream may be set using the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 \var{stream} keyword; the only method used on the stream object is the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 file protocol's \code{write()} method. If not specified, the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 PrettyPrinter adopts \code{sys.stdout}. Three additional parameters |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
32 may be used to control the formatted representation. The keywords are |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
33 \var{indent}, \var{depth}, and \var{width}. The amount of indentation |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
34 added for each recursive level is specified by \var{indent}; the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 default is one. Other values can cause output to look a little odd, |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 but can make nesting easier to spot. The number of levels which may |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 be printed is controlled by \var{depth}; if the data structure being |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 printed is too deep, the next contained level is replaced by |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 \samp{...}. By default, there is no constraint on the depth of the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 objects being formatted. The desired output width is constrained |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
41 using the \var{width} parameter; the default is eighty characters. If |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 a structure cannot be formatted within the constrained width, a best |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 effort will be made. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
45 \begin{verbatim} |
| 5896 | 46 >>> import pprint, sys |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
47 >>> stuff = sys.path[:] |
| 5896 | 48 >>> stuff.insert(0, stuff[:]) |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 >>> pp = pprint.PrettyPrinter(indent=4) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 >>> pp.pprint(stuff) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 [ [ '', |
| 5896 | 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'], | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 '', |
| 5896 | 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'] | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 >>> |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 >>> import parser |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 >>> tup = parser.ast2tuple( |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 ... parser.suite(open('pprint.py').read()))[1][1][1] |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 >>> pp = pprint.PrettyPrinter(depth=6) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 >>> pp.pprint(tup) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 (266, (267, (307, (287, (288, (...)))))) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 \end{verbatim} |
| 5896 | 71 \end{funcdesc} |
| 72 | |
| 73 | |
| 74 % Now the derivative functions: | |
| 75 | |
| 76 The PrettyPrinter class supports several derivative functions: | |
| 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 | |
| 87 \code{print} command for inspecting values. The default parameters | |
| 88 for formatting are used. | |
| 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 | |
| 107 \code{eval()}. Note that this returns false for recursive objects. | |
| 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 | |
|
6901
adb300cbd566
Make the word "Module" in the section header start with an upper-case "M".
Fred Drake <fdrake@acm.org>
parents:
6647
diff
changeset
|
126 as \samp{<Recursion on \var{typename} with id=\var{number}>}. The |
| 5896 | 127 representation is not otherwise formatted. |
| 128 | |
| 129 \begin{verbatim} | |
| 130 >>> pprint.saferepr(stuff) | |
| 131 "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.4', '/usr/loca | |
| 132 l/lib/python1.4/test', '/usr/local/lib/python1.4/sunos5', '/usr/local/lib/python | |
| 133 1.4/sharedmodules', '/usr/local/lib/python1.4/tkinter']" | |
| 134 \end{verbatim} | |
| 135 \end{funcdesc} | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
137 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 \subsection{PrettyPrinter Objects} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
139 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 PrettyPrinter instances (returned by \code{PrettyPrinter()} above) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
141 have the following methods. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
142 |
|
7277
bf4e790bd61a
Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents:
6901
diff
changeset
|
143 \setindexsubitem{(PrettyPrinter method)} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
144 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
145 \begin{funcdesc}{pformat}{object} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 Return the formatted representation of \var{object}. This takes into |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 account the options passed to the PrettyPrinter constructor. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 \end{funcdesc} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
149 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
150 \begin{funcdesc}{pprint}{object} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
151 Print the formatted representation of \var{object} on the configured |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 stream, followed by a newline. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
153 \end{funcdesc} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
154 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 The following methods provide the implementations for the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
156 corresponding functions of the same names. Using these methods on an |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 instance is slightly more efficient since new PrettyPrinter objects |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 don't need to be created. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 \begin{funcdesc}{isreadable}{object} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
161 Determine if the formatted representation of the object is |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 ``readable,'' or can be used to reconstruct the value using |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
163 \code{eval()}. Note that this returns false for recursive objects. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
164 If the \var{depth} parameter of the PrettyPrinter is set and the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
165 object is deeper than allowed, this returns false. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
166 \end{funcdesc} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
167 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
168 \begin{funcdesc}{isrecursive}{object} |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 Determine if the object requires a recursive representation. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
170 \end{funcdesc} |
