blob: a602482ca58c4948359963954e539da892784b12 [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,
15and breaks them out onto multiple lines if they won't fit within the
16width allowed width. Construct PrettyPrinter objects explicitly if
17you need to adjust the width constraint.
18
19The \code{pprint} module defines the following functions:
20
21\renewcommand{\indexsubitem}{(in module pprint)}
22
23\begin{funcdesc}{pformat}{object}
24Return the formatted representation of \var{object} as a string. The
25default parameters for formatting are used.
26\end{funcdesc}
27
28\begin{funcdesc}{pprint}{object\optional{, stream}}
29Prints the formatted representation of \var{object} on \var{stream},
30followed by a newline. If \var{stream} is omitted, \code{sys.stdout}
31is used. This may be used in the interactive interpreter instead of a
32\code{print} command for inspecting values. The default parameters
33for formatting are used.
34\end{funcdesc}
35
36\begin{funcdesc}{isreadable}{object}
37Determine if the formatted representation of \var{object} is
38``readable,'' or can be used to reconstruct the value using
39\code{eval()}. Note that this returns false for recursive objects.
40\end{funcdesc}
41
42\begin{funcdesc}{isrecursive}{object}
43Determine if \var{object} requires a recursive representation.
44\end{funcdesc}
45
46\begin{funcdesc}{saferepr}{object}
47Return a string representation of \var{object}, protected against
48recursive data structures. If the representation of \var{object}
49exposes a recursive entry, the recursive reference will be represented
50as \samp{$<$Recursion on \var{typename} with id=\var{number}$>$}.
51\end{funcdesc}
52
53
54% Now for the implementation class:
55
56\begin{funcdesc}{PrettyPrinter}{...}
57Construct a PrettyPrinter instance. This constructor understands
58several keyword parameters. An output stream may be set using the
59\var{stream} keyword; the only method used on the stream object is the
60file protocol's \code{write()} method. If not specified, the
61PrettyPrinter adopts \code{sys.stdout}. Three additional parameters
62may be used to control the formatted representation. The keywords are
63\var{indent}, \var{depth}, and \var{width}. The amount of indentation
64added for each recursive level is specified by \var{indent}; the
65default is one. Other values can cause output to look a little odd,
66but can make nesting easier to spot. The number of levels which may
67be printed is controlled by \var{depth}; if the data structure being
68printed is too deep, the next contained level is replaced by
69\samp{...}. By default, there is no constraint on the depth of the
70objects being formatted. The desired output width is constrained
71using the \var{width} parameter; the default is eighty characters. If
72a structure cannot be formatted within the constrained width, a best
73effort will be made.
74\end{funcdesc}
75
76
77% Guido marked this as a good spot for an example in the template,
78% but I think this needs a better location in this module. Not sure where.
79
80Example:
81
82\begin{verbatim}
83>>> import pprint
84>>> stuff = sys.path[:]
85>>> stuff.insert(0, stuff)
86>>> pprint.pprint(stuff)
87[<Recursion on list with id=869440>,
88 '',
89 '/usr/local/lib/python1.4',
90 '/usr/local/lib/python1.4/test',
91 '/usr/local/lib/python1.4/sunos5',
92 '/usr/local/lib/python1.4/sharedmodules',
93 '/usr/local/lib/python1.4/tkinter']
94>>>
95>>> stuff[0] = stuff[1:]
96>>> pp = pprint.PrettyPrinter(indent=4)
97>>> pp.pprint(stuff)
98[ [ '',
99 '/usr/local/lib/python1.4',
100 '/usr/local/lib/python1.4/test',
101 '/usr/local/lib/python1.4/sunos5',
102 '/usr/local/lib/python1.4/sharedmodules',
103 '/usr/local/lib/python1.4/tkinter'],
104 '',
105 '/usr/local/lib/python1.4',
106 '/usr/local/lib/python1.4/test',
107 '/usr/local/lib/python1.4/sunos5',
108 '/usr/local/lib/python1.4/sharedmodules',
109 '/usr/local/lib/python1.4/tkinter']
110>>>
111>>> import parser
112>>> tup = parser.ast2tuple(
113... parser.suite(open('pprint.py').read()))[1][1][1]
114>>> pp = pprint.PrettyPrinter(depth=6)
115>>> pp.pprint(tup)
116(266, (267, (307, (287, (288, (...))))))
117\end{verbatim}
118
119
120\subsection{PrettyPrinter Objects}
121
122PrettyPrinter instances (returned by \code{PrettyPrinter()} above)
123have the following methods.
124
125\renewcommand{\indexsubitem}{(PrettyPrinter method)}
126
127\begin{funcdesc}{pformat}{object}
128Return the formatted representation of \var{object}. This takes into
129account the options passed to the PrettyPrinter constructor.
130\end{funcdesc}
131
132\begin{funcdesc}{pprint}{object}
133Print the formatted representation of \var{object} on the configured
134stream, followed by a newline.
135\end{funcdesc}
136
137The following methods provide the implementations for the
138corresponding functions of the same names. Using these methods on an
139instance is slightly more efficient since new PrettyPrinter objects
140don't need to be created.
141
142\begin{funcdesc}{isreadable}{object}
143Determine if the formatted representation of the object is
144``readable,'' or can be used to reconstruct the value using
145\code{eval()}. Note that this returns false for recursive objects.
146If the \var{depth} parameter of the PrettyPrinter is set and the
147object is deeper than allowed, this returns false.
148\end{funcdesc}
149
150\begin{funcdesc}{isrecursive}{object}
151Determine if the object requires a recursive representation.
152\end{funcdesc}