Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libpprint.tex @ 7829:e2fb25784530 legacy-trunk
Talk about some minor nits...
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Fri, 03 Apr 1998 07:11:32 +0000 |
| parents | 8f177933c41f |
| children | 8e5223680a9d |
| 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 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
7 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
|
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 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
17 allowed width. Construct \class{PrettyPrinter} objects explicitly if |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
18 you need to adjust the width constraint. |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
20 The \module{pprint} module defines one class: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 |
| 5896 | 23 % First the implementation class: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
25 \begin{classdesc}{PrettyPrinter}{...} |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
26 Construct a \class{PrettyPrinter} instance. This constructor |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 \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
|
34 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
|
35 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
|
36 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
|
37 \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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 \begin{verbatim} |
| 5896 | 45 >>> import pprint, sys |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 >>> stuff = sys.path[:] |
| 5896 | 47 >>> stuff.insert(0, stuff[:]) |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 >>> pp = pprint.PrettyPrinter(indent=4) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 >>> pp.pprint(stuff) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 [ [ '', |
| 5896 | 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'], | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 '', |
| 5896 | 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'] | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 >>> |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 >>> import parser |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 >>> tup = parser.ast2tuple( |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 ... parser.suite(open('pprint.py').read()))[1][1][1] |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 >>> pp = pprint.PrettyPrinter(depth=6) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 >>> pp.pprint(tup) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 (266, (267, (307, (287, (288, (...)))))) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 \end{verbatim} |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
70 \end{classdesc} |
| 5896 | 71 |
| 72 | |
| 73 % Now the derivative functions: | |
| 74 | |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
75 The \class{PrettyPrinter} class supports several derivative functions: |
| 5896 | 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 | |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
86 \keyword{print} statement for inspecting values. The default |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
87 parameters for formatting are used. |
| 5896 | 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 | |
| 7829 | 106 \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
|
107 recursive objects. |
| 5896 | 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. |
| 7608 | 128 \end{funcdesc} |
| 5896 | 129 |
| 7608 | 130 % This example is outside the {funcdesc} to keep it from running over |
| 131 % the right margin. | |
| 5896 | 132 \begin{verbatim} |
| 133 >>> pprint.saferepr(stuff) | |
| 7829 | 134 "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.5', '/usr/loca |
| 135 l/lib/python1.5/test', '/usr/local/lib/python1.5/sunos5', '/usr/local/lib/python | |
| 136 1.5/sharedmodules', '/usr/local/lib/python1.5/tkinter']" | |
| 5896 | 137 \end{verbatim} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 |
|
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 \subsection{PrettyPrinter Objects} |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
141 \label{PrettyPrinter Objects} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
142 |
| 7608 | 143 \class{PrettyPrinter} instances have the following methods: |
|
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 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
146 \begin{methoddesc}{pformat}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 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
|
148 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
|
149 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
150 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
151 \begin{methoddesc}{pprint}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 Print the formatted representation of \var{object} on the configured |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
153 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
|
154 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
156 The following methods provide the implementations for the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 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
|
158 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
|
159 objects don't need to be created. |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
161 \begin{methoddesc}{isreadable}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 Determine if the formatted representation of the object is |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
163 ``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
|
164 \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
|
165 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
|
166 \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
|
167 this returns false. |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
168 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
170 \begin{methoddesc}{isrecursive}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
171 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
|
172 \end{methoddesc} |
