blob: 9e14d5f5af28edb7878c8dddb78449612673ad07 [file] [log] [blame]
Fred Drakeee8d3ca1997-07-18 20:41:58 +00001%% Author: Fred L. Drake, Jr. <fdrake@acm.org>
2
Fred Drake2303d311997-12-17 14:07:25 +00003\section{Standard Module \sectcode{pprint}}
Fred Drakeee8d3ca1997-07-18 20:41:58 +00004\stmodindex{pprint}
Fred Drake2303d311997-12-17 14:07:25 +00005\label{module-pprint}
Fred Drakeee8d3ca1997-07-18 20:41:58 +00006
7The \code{pprint} module provides a capability to ``pretty-print''
8arbitrary Python data structures in a form which can be used as input
9to the interpreter. If the formatted structures include objects which
10are not fundamental Python types, the representation may not be
11loadable. This may be the case if objects such as files, sockets,
12classes, or instances are included, as well as many other builtin
13objects which are not representable as Python constants.
14
15The formatted representation keeps objects on a single line if it can,
Fred Drake12d9eac1997-07-24 15:39:16 +000016and breaks them onto multiple lines if they don't fit within the
17allowed width. Construct PrettyPrinter objects explicitly if you need
18to adjust the width constraint.
Fred Drakeee8d3ca1997-07-18 20:41:58 +000019
Fred Drake12d9eac1997-07-24 15:39:16 +000020The \code{pprint} module defines one class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000021
22\renewcommand{\indexsubitem}{(in module pprint)}
23
Fred Drake12d9eac1997-07-24 15:39:16 +000024% First the implementation class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000025
26\begin{funcdesc}{PrettyPrinter}{...}
27Construct a PrettyPrinter instance. This constructor understands
28several keyword parameters. An output stream may be set using the
29\var{stream} keyword; the only method used on the stream object is the
30file protocol's \code{write()} method. If not specified, the
31PrettyPrinter adopts \code{sys.stdout}. Three additional parameters
32may be used to control the formatted representation. The keywords are
33\var{indent}, \var{depth}, and \var{width}. The amount of indentation
34added for each recursive level is specified by \var{indent}; the
35default is one. Other values can cause output to look a little odd,
36but can make nesting easier to spot. The number of levels which may
37be printed is controlled by \var{depth}; if the data structure being
38printed is too deep, the next contained level is replaced by
39\samp{...}. By default, there is no constraint on the depth of the
40objects being formatted. The desired output width is constrained
41using the \var{width} parameter; the default is eighty characters. If
42a structure cannot be formatted within the constrained width, a best
43effort will be made.
Fred Drakeee8d3ca1997-07-18 20:41:58 +000044
45\begin{verbatim}
Fred Drake12d9eac1997-07-24 15:39:16 +000046>>> import pprint, sys
Fred Drakeee8d3ca1997-07-18 20:41:58 +000047>>> stuff = sys.path[:]
Fred Drake12d9eac1997-07-24 15:39:16 +000048>>> stuff.insert(0, stuff[:])
Fred Drakeee8d3ca1997-07-18 20:41:58 +000049>>> pp = pprint.PrettyPrinter(indent=4)
50>>> pp.pprint(stuff)
51[ [ '',
Fred Drake12d9eac1997-07-24 15:39:16 +000052 '/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 Drakeee8d3ca1997-07-18 20:41:58 +000057 '',
Fred Drake12d9eac1997-07-24 15:39:16 +000058 '/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 Drakeee8d3ca1997-07-18 20:41:58 +000063>>>
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 Drake12d9eac1997-07-24 15:39:16 +000071\end{funcdesc}
72
73
74% Now the derivative functions:
75
76The PrettyPrinter class supports several derivative functions:
77
78\begin{funcdesc}{pformat}{object}
79Return the formatted representation of \var{object} as a string. The
80default parameters for formatting are used.
81\end{funcdesc}
82
83\begin{funcdesc}{pprint}{object\optional{, stream}}
84Prints the formatted representation of \var{object} on \var{stream},
85followed by a newline. If \var{stream} is omitted, \code{sys.stdout}
86is used. This may be used in the interactive interpreter instead of a
87\code{print} command for inspecting values. The default parameters
88for 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}
105Determine 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)
1110
112\end{verbatim}
113\end{funcdesc}
114
115\begin{funcdesc}{isrecursive}{object}
116Determine if \var{object} requires a recursive representation.
117\end{funcdesc}
118
119
120One more support function is also defined:
121
122\begin{funcdesc}{saferepr}{object}
123Return a string representation of \var{object}, protected against
124recursive data structures. If the representation of \var{object}
125exposes a recursive entry, the recursive reference will be represented
Fred Drake2303d311997-12-17 14:07:25 +0000126as \samp{<Recursion on \var{typename} with id=\var{number}>}. The
Fred Drake12d9eac1997-07-24 15:39:16 +0000127representation 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
132l/lib/python1.4/test', '/usr/local/lib/python1.4/sunos5', '/usr/local/lib/python
1331.4/sharedmodules', '/usr/local/lib/python1.4/tkinter']"
134\end{verbatim}
135\end{funcdesc}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000136
137
138\subsection{PrettyPrinter Objects}
139
140PrettyPrinter instances (returned by \code{PrettyPrinter()} above)
141have the following methods.
142
143\renewcommand{\indexsubitem}{(PrettyPrinter method)}
144
145\begin{funcdesc}{pformat}{object}
146Return the formatted representation of \var{object}. This takes into
147account the options passed to the PrettyPrinter constructor.
148\end{funcdesc}
149
150\begin{funcdesc}{pprint}{object}
151Print the formatted representation of \var{object} on the configured
152stream, followed by a newline.
153\end{funcdesc}
154
155The following methods provide the implementations for the
156corresponding functions of the same names. Using these methods on an
157instance is slightly more efficient since new PrettyPrinter objects
158don't need to be created.
159
160\begin{funcdesc}{isreadable}{object}
161Determine if the formatted representation of the object is
162``readable,'' or can be used to reconstruct the value using
163\code{eval()}. Note that this returns false for recursive objects.
164If the \var{depth} parameter of the PrettyPrinter is set and the
165object is deeper than allowed, this returns false.
166\end{funcdesc}
167
168\begin{funcdesc}{isrecursive}{object}
169Determine if the object requires a recursive representation.
170\end{funcdesc}