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