blob: 6f034317671c139837907f80e15030bd09098500 [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
Fred Drake2c8aa651998-02-20 06:03:52 +00007The \module{pprint} module provides a capability to ``pretty-print''
Fred Drakeee8d3ca1997-07-18 20:41:58 +00008arbitrary 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
Fred Drake2c8aa651998-02-20 06:03:52 +000017allowed width. Construct \class{PrettyPrinter} objects explicitly if
18you need to adjust the width constraint.
Fred Drakeee8d3ca1997-07-18 20:41:58 +000019
Fred Drake2c8aa651998-02-20 06:03:52 +000020The \module{pprint} module defines one class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000021
Fred Drakeee8d3ca1997-07-18 20:41:58 +000022
Fred Drake12d9eac1997-07-24 15:39:16 +000023% First the implementation class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000024
Fred Drake2c8aa651998-02-20 06:03:52 +000025\begin{classdesc}{PrettyPrinter}{...}
26Construct a \class{PrettyPrinter} instance. This constructor
27understands several keyword parameters. An output stream may be set
28using the \var{stream} keyword; the only method used on the stream
29object is the file protocol's \method{write()} method. If not
30specified, the \class{PrettyPrinter} adopts \code{sys.stdout}. Three
31additional parameters may be used to control the formatted
32representation. The keywords are \var{indent}, \var{depth}, and
33\var{width}. The amount of indentation added for each recursive level
34is specified by \var{indent}; the default is one. Other values can
35cause output to look a little odd, but can make nesting easier to
36spot. The number of levels which may be printed is controlled by
37\var{depth}; if the data structure being printed is too deep, the next
38contained level is replaced by \samp{...}. By default, there is no
39constraint on the depth of the objects being formatted. The desired
40output width is constrained using the \var{width} parameter; the
41default is eighty characters. If a structure cannot be formatted
42within the constrained width, a best effort 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 Drake2c8aa651998-02-20 06:03:52 +000070\end{classdesc}
Fred Drake12d9eac1997-07-24 15:39:16 +000071
72
73% Now the derivative functions:
74
Fred Drake2c8aa651998-02-20 06:03:52 +000075The \class{PrettyPrinter} class supports several derivative functions:
Fred Drake12d9eac1997-07-24 15:39:16 +000076
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
Fred Drake2c8aa651998-02-20 06:03:52 +000086\keyword{print} statement for inspecting values. The default
87parameters for formatting are used.
Fred Drake12d9eac1997-07-24 15:39:16 +000088
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
Fred Drake2c8aa651998-02-20 06:03:52 +0000106\function{eval()}\bifuncindex{eval}. Note that this returns false for
107recursive objects.
Fred Drake12d9eac1997-07-24 15:39:16 +0000108
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.
Fred Drakeb55f9d31998-03-08 07:03:27 +0000128\end{funcdesc}
Fred Drake12d9eac1997-07-24 15:39:16 +0000129
Fred Drakeb55f9d31998-03-08 07:03:27 +0000130% This example is outside the {funcdesc} to keep it from running over
131% the right margin.
Fred Drake12d9eac1997-07-24 15:39:16 +0000132\begin{verbatim}
133>>> pprint.saferepr(stuff)
134"[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.4', '/usr/loca
135l/lib/python1.4/test', '/usr/local/lib/python1.4/sunos5', '/usr/local/lib/python
1361.4/sharedmodules', '/usr/local/lib/python1.4/tkinter']"
137\end{verbatim}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000138
139
140\subsection{PrettyPrinter Objects}
Fred Drake2c8aa651998-02-20 06:03:52 +0000141\label{PrettyPrinter Objects}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000142
Fred Drakeb55f9d31998-03-08 07:03:27 +0000143\class{PrettyPrinter} instances have the following methods:
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000144
Fred Drake19479911998-02-13 06:58:54 +0000145\setindexsubitem{(PrettyPrinter method)}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000146
147\begin{funcdesc}{pformat}{object}
148Return the formatted representation of \var{object}. This takes into
Fred Drake2c8aa651998-02-20 06:03:52 +0000149account the options passed to the \class{PrettyPrinter} constructor.
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000150\end{funcdesc}
151
152\begin{funcdesc}{pprint}{object}
153Print the formatted representation of \var{object} on the configured
154stream, followed by a newline.
155\end{funcdesc}
156
157The following methods provide the implementations for the
158corresponding functions of the same names. Using these methods on an
Fred Drake2c8aa651998-02-20 06:03:52 +0000159instance is slightly more efficient since new \class{PrettyPrinter}
160objects don't need to be created.
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000161
162\begin{funcdesc}{isreadable}{object}
163Determine if the formatted representation of the object is
164``readable,'' or can be used to reconstruct the value using
Fred Drake2c8aa651998-02-20 06:03:52 +0000165\function{eval()}\bifuncindex{eval}. Note that this returns false for
166recursive objects. If the \var{depth} parameter of the
167\class{PrettyPrinter} is set and the object is deeper than allowed,
168this returns false.
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000169\end{funcdesc}
170
171\begin{funcdesc}{isrecursive}{object}
172Determine if the object requires a recursive representation.
173\end{funcdesc}