Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libpprint.tex @ 8831:00a6bb65ff29 legacy-trunk
Getting rid of this ancient stuff.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 05 Aug 1998 21:10:09 +0000 |
| parents | 760dcf197048 |
| children | acfffe14628e |
| 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 |
|
7877
8e5223680a9d
Remove all uses of \sectcode; we can now use logical markup everywhere.
Fred Drake <fdrake@acm.org>
parents:
7829
diff
changeset
|
3 \section{Standard Module \module{pprint}} |
|
8716
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
4 \declaremodule{standard}{pprint} |
|
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
5 |
|
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
6 \modulesynopsis{Data pretty printer.} |
|
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
7 |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
8 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
9 The \module{pprint} module provides a capability to ``pretty-print'' |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 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
|
11 to the interpreter. If the formatted structures include objects which |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 are not fundamental Python types, the representation may not be |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 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
|
14 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
|
15 objects which are not representable as Python constants. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 The formatted representation keeps objects on a single line if it can, |
| 5896 | 18 and breaks them onto multiple lines if they don't fit within the |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
19 allowed width. Construct \class{PrettyPrinter} objects explicitly if |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
20 you need to adjust the width constraint. |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
22 The \module{pprint} module defines one class: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
23 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 |
| 5896 | 25 % First the implementation class: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
27 \begin{classdesc}{PrettyPrinter}{...} |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
28 Construct a \class{PrettyPrinter} instance. This constructor |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
29 understands several keyword parameters. An output stream may be set |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
30 using the \var{stream} keyword; the only method used on the stream |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
31 object is the file protocol's \method{write()} method. If not |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
32 specified, the \class{PrettyPrinter} adopts \code{sys.stdout}. Three |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
33 additional parameters may be used to control the formatted |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
34 representation. The keywords are \var{indent}, \var{depth}, and |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
35 \var{width}. The amount of indentation added for each recursive level |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
36 is specified by \var{indent}; the default is one. Other values can |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
37 cause output to look a little odd, but can make nesting easier to |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
38 spot. The number of levels which may be printed is controlled by |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
39 \var{depth}; if the data structure being printed is too deep, the next |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
40 contained level is replaced by \samp{...}. By default, there is no |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
41 constraint on the depth of the objects being formatted. The desired |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
42 output width is constrained using the \var{width} parameter; the |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
43 default is eighty characters. If a structure cannot be formatted |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
44 within the constrained width, a best effort will be made. |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
45 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 \begin{verbatim} |
| 5896 | 47 >>> import pprint, sys |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 >>> stuff = sys.path[:] |
| 5896 | 49 >>> stuff.insert(0, stuff[:]) |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 >>> pp = pprint.PrettyPrinter(indent=4) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 >>> pp.pprint(stuff) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 [ [ '', |
| 5896 | 53 '/usr/local/lib/python1.5', |
| 54 '/usr/local/lib/python1.5/test', | |
| 55 '/usr/local/lib/python1.5/sunos5', | |
| 56 '/usr/local/lib/python1.5/sharedmodules', | |
| 57 '/usr/local/lib/python1.5/tkinter'], | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 '', |
| 5896 | 59 '/usr/local/lib/python1.5', |
| 60 '/usr/local/lib/python1.5/test', | |
| 61 '/usr/local/lib/python1.5/sunos5', | |
| 62 '/usr/local/lib/python1.5/sharedmodules', | |
| 63 '/usr/local/lib/python1.5/tkinter'] | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 >>> |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 >>> import parser |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 >>> tup = parser.ast2tuple( |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 ... parser.suite(open('pprint.py').read()))[1][1][1] |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 >>> pp = pprint.PrettyPrinter(depth=6) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 >>> pp.pprint(tup) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 (266, (267, (307, (287, (288, (...)))))) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 \end{verbatim} |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
72 \end{classdesc} |
| 5896 | 73 |
| 74 | |
| 75 % Now the derivative functions: | |
| 76 | |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
77 The \class{PrettyPrinter} class supports several derivative functions: |
| 5896 | 78 |
| 79 \begin{funcdesc}{pformat}{object} | |
| 80 Return the formatted representation of \var{object} as a string. The | |
| 81 default parameters for formatting are used. | |
| 82 \end{funcdesc} | |
| 83 | |
| 84 \begin{funcdesc}{pprint}{object\optional{, stream}} | |
| 85 Prints the formatted representation of \var{object} on \var{stream}, | |
| 86 followed by a newline. If \var{stream} is omitted, \code{sys.stdout} | |
| 87 is used. This may be used in the interactive interpreter instead of a | |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
88 \keyword{print} statement for inspecting values. The default |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
89 parameters for formatting are used. |
| 5896 | 90 |
| 91 \begin{verbatim} | |
| 92 >>> stuff = sys.path[:] | |
| 93 >>> stuff.insert(0, stuff) | |
| 94 >>> pprint.pprint(stuff) | |
| 95 [<Recursion on list with id=869440>, | |
| 96 '', | |
| 97 '/usr/local/lib/python1.5', | |
| 98 '/usr/local/lib/python1.5/test', | |
| 99 '/usr/local/lib/python1.5/sunos5', | |
| 100 '/usr/local/lib/python1.5/sharedmodules', | |
| 101 '/usr/local/lib/python1.5/tkinter'] | |
| 102 \end{verbatim} | |
| 103 \end{funcdesc} | |
| 104 | |
| 105 \begin{funcdesc}{isreadable}{object} | |
| 106 Determine if the formatted representation of \var{object} is | |
| 107 ``readable,'' or can be used to reconstruct the value using | |
| 7829 | 108 \function{eval()}\bifuncindex{eval}. This always returns false for |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
109 recursive objects. |
| 5896 | 110 |
| 111 \begin{verbatim} | |
| 112 >>> pprint.isreadable(stuff) | |
| 113 0 | |
| 114 \end{verbatim} | |
| 115 \end{funcdesc} | |
| 116 | |
| 117 \begin{funcdesc}{isrecursive}{object} | |
| 118 Determine if \var{object} requires a recursive representation. | |
| 119 \end{funcdesc} | |
| 120 | |
| 121 | |
| 122 One more support function is also defined: | |
| 123 | |
| 124 \begin{funcdesc}{saferepr}{object} | |
| 125 Return a string representation of \var{object}, protected against | |
| 126 recursive data structures. If the representation of \var{object} | |
| 127 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
|
128 as \samp{<Recursion on \var{typename} with id=\var{number}>}. The |
| 5896 | 129 representation is not otherwise formatted. |
| 7608 | 130 \end{funcdesc} |
| 5896 | 131 |
| 7608 | 132 % This example is outside the {funcdesc} to keep it from running over |
| 133 % the right margin. | |
| 5896 | 134 \begin{verbatim} |
| 135 >>> pprint.saferepr(stuff) | |
| 7829 | 136 "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.5', '/usr/loca |
| 137 l/lib/python1.5/test', '/usr/local/lib/python1.5/sunos5', '/usr/local/lib/python | |
| 138 1.5/sharedmodules', '/usr/local/lib/python1.5/tkinter']" | |
| 5896 | 139 \end{verbatim} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
141 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
142 \subsection{PrettyPrinter Objects} |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
143 \label{PrettyPrinter Objects} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
144 |
| 7608 | 145 \class{PrettyPrinter} instances have the following methods: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
148 \begin{methoddesc}{pformat}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
149 Return the formatted representation of \var{object}. This takes into |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
150 Account the options passed to the \class{PrettyPrinter} constructor. |
|
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
151 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
153 \begin{methoddesc}{pprint}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
154 Print the formatted representation of \var{object} on the configured |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 stream, followed by a newline. |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
156 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 The following methods provide the implementations for the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 corresponding functions of the same names. Using these methods on an |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
160 instance is slightly more efficient since new \class{PrettyPrinter} |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
161 objects don't need to be created. |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
163 \begin{methoddesc}{isreadable}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
164 Determine if the formatted representation of the object is |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
165 ``readable,'' or can be used to reconstruct the value using |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
166 \function{eval()}\bifuncindex{eval}. Note that this returns false for |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
167 recursive objects. If the \var{depth} parameter of the |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
168 \class{PrettyPrinter} is set and the object is deeper than allowed, |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
169 this returns false. |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
170 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
171 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
172 \begin{methoddesc}{isrecursive}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
173 Determine if the object requires a recursive representation. |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
174 \end{methoddesc} |
