Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libpprint.tex @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 563dee598d3a |
| children | d26d8575ac62 |
| rev | line source |
|---|---|
| 8918 | 1 \section{\module{pprint} --- |
|
15142
563dee598d3a
Fixed lots of small nits caught by Ka-Ping Yee <ping@lfw.org>.
Fred Drake <fdrake@acm.org>
parents:
10239
diff
changeset
|
2 Data pretty printer} |
|
10239
c1befdbd64e5
Record module & documentation author.
Fred Drake <fdrake@acm.org>
parents:
8918
diff
changeset
|
3 |
|
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 \modulesynopsis{Data pretty printer.} |
|
10239
c1befdbd64e5
Record module & documentation author.
Fred Drake <fdrake@acm.org>
parents:
8918
diff
changeset
|
6 \moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
|
c1befdbd64e5
Record module & documentation author.
Fred Drake <fdrake@acm.org>
parents:
8918
diff
changeset
|
7 \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
|
8716
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
8 |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
10 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
|
11 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
|
12 to the interpreter. If the formatted structures include objects which |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 are not fundamental Python types, the representation may not be |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 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
|
15 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
|
16 objects which are not representable as Python constants. |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 The formatted representation keeps objects on a single line if it can, |
| 5896 | 19 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
|
20 allowed width. Construct \class{PrettyPrinter} objects explicitly if |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
21 you need to adjust the width constraint. |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
23 The \module{pprint} module defines one class: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
25 |
| 5896 | 26 % First the implementation class: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
28 \begin{classdesc}{PrettyPrinter}{...} |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
29 Construct a \class{PrettyPrinter} instance. This constructor |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 \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
|
37 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
|
38 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
|
39 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
|
40 \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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
47 \begin{verbatim} |
| 5896 | 48 >>> import pprint, sys |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 >>> stuff = sys.path[:] |
| 5896 | 50 >>> stuff.insert(0, stuff[:]) |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 >>> pp = pprint.PrettyPrinter(indent=4) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 >>> pp.pprint(stuff) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
53 [ [ '', |
| 5896 | 54 '/usr/local/lib/python1.5', |
| 55 '/usr/local/lib/python1.5/test', | |
| 56 '/usr/local/lib/python1.5/sunos5', | |
| 57 '/usr/local/lib/python1.5/sharedmodules', | |
| 58 '/usr/local/lib/python1.5/tkinter'], | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
59 '', |
| 5896 | 60 '/usr/local/lib/python1.5', |
| 61 '/usr/local/lib/python1.5/test', | |
| 62 '/usr/local/lib/python1.5/sunos5', | |
| 63 '/usr/local/lib/python1.5/sharedmodules', | |
| 64 '/usr/local/lib/python1.5/tkinter'] | |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 >>> |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 >>> import parser |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 >>> tup = parser.ast2tuple( |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 ... parser.suite(open('pprint.py').read()))[1][1][1] |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 >>> pp = pprint.PrettyPrinter(depth=6) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 >>> pp.pprint(tup) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 (266, (267, (307, (287, (288, (...)))))) |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
72 \end{verbatim} |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
73 \end{classdesc} |
| 5896 | 74 |
| 75 | |
| 76 % Now the derivative functions: | |
| 77 | |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
78 The \class{PrettyPrinter} class supports several derivative functions: |
| 5896 | 79 |
| 80 \begin{funcdesc}{pformat}{object} | |
| 81 Return the formatted representation of \var{object} as a string. The | |
| 82 default parameters for formatting are used. | |
| 83 \end{funcdesc} | |
| 84 | |
| 85 \begin{funcdesc}{pprint}{object\optional{, stream}} | |
| 86 Prints the formatted representation of \var{object} on \var{stream}, | |
| 87 followed by a newline. If \var{stream} is omitted, \code{sys.stdout} | |
| 88 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
|
89 \keyword{print} statement for inspecting values. The default |
|
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
90 parameters for formatting are used. |
| 5896 | 91 |
| 92 \begin{verbatim} | |
| 93 >>> stuff = sys.path[:] | |
| 94 >>> stuff.insert(0, stuff) | |
| 95 >>> pprint.pprint(stuff) | |
| 96 [<Recursion on list with id=869440>, | |
| 97 '', | |
| 98 '/usr/local/lib/python1.5', | |
| 99 '/usr/local/lib/python1.5/test', | |
| 100 '/usr/local/lib/python1.5/sunos5', | |
| 101 '/usr/local/lib/python1.5/sharedmodules', | |
| 102 '/usr/local/lib/python1.5/tkinter'] | |
| 103 \end{verbatim} | |
| 104 \end{funcdesc} | |
| 105 | |
| 106 \begin{funcdesc}{isreadable}{object} | |
| 107 Determine if the formatted representation of \var{object} is | |
| 108 ``readable,'' or can be used to reconstruct the value using | |
| 7829 | 109 \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
|
110 recursive objects. |
| 5896 | 111 |
| 112 \begin{verbatim} | |
| 113 >>> pprint.isreadable(stuff) | |
| 114 0 | |
| 115 \end{verbatim} | |
| 116 \end{funcdesc} | |
| 117 | |
| 118 \begin{funcdesc}{isrecursive}{object} | |
| 119 Determine if \var{object} requires a recursive representation. | |
| 120 \end{funcdesc} | |
| 121 | |
| 122 | |
| 123 One more support function is also defined: | |
| 124 | |
| 125 \begin{funcdesc}{saferepr}{object} | |
| 126 Return a string representation of \var{object}, protected against | |
| 127 recursive data structures. If the representation of \var{object} | |
| 128 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
|
129 as \samp{<Recursion on \var{typename} with id=\var{number}>}. The |
| 5896 | 130 representation is not otherwise formatted. |
| 7608 | 131 \end{funcdesc} |
| 5896 | 132 |
| 7608 | 133 % This example is outside the {funcdesc} to keep it from running over |
| 134 % the right margin. | |
| 5896 | 135 \begin{verbatim} |
| 136 >>> pprint.saferepr(stuff) | |
| 7829 | 137 "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.5', '/usr/loca |
| 138 l/lib/python1.5/test', '/usr/local/lib/python1.5/sunos5', '/usr/local/lib/python | |
| 139 1.5/sharedmodules', '/usr/local/lib/python1.5/tkinter']" | |
| 5896 | 140 \end{verbatim} |
|
5836
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 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
143 \subsection{PrettyPrinter Objects} |
|
7424
11e9337cda3d
Added \label{}s for logical addressing.
Fred Drake <fdrake@acm.org>
parents:
7277
diff
changeset
|
144 \label{PrettyPrinter Objects} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
145 |
| 7608 | 146 \class{PrettyPrinter} instances have the following methods: |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
149 \begin{methoddesc}{pformat}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
150 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
|
151 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
|
152 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
153 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
154 \begin{methoddesc}{pprint}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 Print the formatted representation of \var{object} on the configured |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
156 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
|
157 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 The following methods provide the implementations for the |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 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
|
161 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
|
162 objects don't need to be created. |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
163 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
164 \begin{methoddesc}{isreadable}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
165 Determine if the formatted representation of the object is |
|
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
166 ``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
|
167 \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
|
168 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
|
169 \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
|
170 this returns false. |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
171 \end{methoddesc} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
172 |
|
7768
8f177933c41f
Use the new {methoddesc} and {memberdesc} environments as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7608
diff
changeset
|
173 \begin{methoddesc}{isrecursive}{object} |
|
5836
3a798677506c
Documentation for the pprint module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
174 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
|
175 \end{methoddesc} |
