blob: 9c29370f830b3720d99f475397809262513b071c [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 Drake19479911998-02-13 06:58:54 +000022\setindexsubitem{(in module pprint)}
Fred Drakeee8d3ca1997-07-18 20:41:58 +000023
Fred Drake12d9eac1997-07-24 15:39:16 +000024% First the implementation class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000025
Fred Drake2c8aa651998-02-20 06:03:52 +000026\begin{classdesc}{PrettyPrinter}{...}
27Construct a \class{PrettyPrinter} instance. This constructor
28understands several keyword parameters. An output stream may be set
29using the \var{stream} keyword; the only method used on the stream
30object is the file protocol's \method{write()} method. If not
31specified, the \class{PrettyPrinter} adopts \code{sys.stdout}. Three
32additional parameters may be used to control the formatted
33representation. The keywords are \var{indent}, \var{depth}, and
34\var{width}. The amount of indentation added for each recursive level
35is specified by \var{indent}; the default is one. Other values can
36cause output to look a little odd, but can make nesting easier to
37spot. The number of levels which may be printed is controlled by
38\var{depth}; if the data structure being printed is too deep, the next
39contained level is replaced by \samp{...}. By default, there is no
40constraint on the depth of the objects being formatted. The desired
41output width is constrained using the \var{width} parameter; the
42default is eighty characters. If a structure cannot be formatted
43within the constrained width, a best effort 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 Drake2c8aa651998-02-20 06:03:52 +000071\end{classdesc}
Fred Drake12d9eac1997-07-24 15:39:16 +000072
73
74% Now the derivative functions:
75
Fred Drake2c8aa651998-02-20 06:03:52 +000076The \class{PrettyPrinter} class supports several derivative functions:
Fred Drake12d9eac1997-07-24 15:39:16 +000077
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
Fred Drake2c8aa651998-02-20 06:03:52 +000087\keyword{print} statement for inspecting values. The default
88parameters for formatting are used.
Fred Drake12d9eac1997-07-24 15:39:16 +000089
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
Fred Drake2c8aa651998-02-20 06:03:52 +0000107\function{eval()}\bifuncindex{eval}. Note that this returns false for
108recursive objects.
Fred Drake12d9eac1997-07-24 15:39:16 +0000109
110\begin{verbatim}
111>>> pprint.isreadable(stuff)
1120
113\end{verbatim}
114\end{funcdesc}
115
116\begin{funcdesc}{isrecursive}{object}
117Determine if \var{object} requires a recursive representation.
118\end{funcdesc}
119
120
121One more support function is also defined:
122
123\begin{funcdesc}{saferepr}{object}
124Return a string representation of \var{object}, protected against
125recursive data structures. If the representation of \var{object}
126exposes a recursive entry, the recursive reference will be represented
Fred Drake2303d311997-12-17 14:07:25 +0000127as \samp{<Recursion on \var{typename} with id=\var{number}>}. The
Fred Drake12d9eac1997-07-24 15:39:16 +0000128representation is not otherwise formatted.
129
130\begin{verbatim}
131>>> pprint.saferepr(stuff)
132"[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.4', '/usr/loca
133l/lib/python1.4/test', '/usr/local/lib/python1.4/sunos5', '/usr/local/lib/python
1341.4/sharedmodules', '/usr/local/lib/python1.4/tkinter']"
135\end{verbatim}
136\end{funcdesc}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000137
138
139\subsection{PrettyPrinter Objects}
Fred Drake2c8aa651998-02-20 06:03:52 +0000140\label{PrettyPrinter Objects}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000141
Fred Drake2c8aa651998-02-20 06:03:52 +0000142PrettyPrinter instances have the following methods:
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000143
Fred Drake19479911998-02-13 06:58:54 +0000144\setindexsubitem{(PrettyPrinter method)}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000145
146\begin{funcdesc}{pformat}{object}
147Return the formatted representation of \var{object}. This takes into
Fred Drake2c8aa651998-02-20 06:03:52 +0000148account the options passed to the \class{PrettyPrinter} constructor.
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000149\end{funcdesc}
150
151\begin{funcdesc}{pprint}{object}
152Print the formatted representation of \var{object} on the configured
153stream, followed by a newline.
154\end{funcdesc}
155
156The following methods provide the implementations for the
157corresponding functions of the same names. Using these methods on an
Fred Drake2c8aa651998-02-20 06:03:52 +0000158instance is slightly more efficient since new \class{PrettyPrinter}
159objects don't need to be created.
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000160
161\begin{funcdesc}{isreadable}{object}
162Determine if the formatted representation of the object is
163``readable,'' or can be used to reconstruct the value using
Fred Drake2c8aa651998-02-20 06:03:52 +0000164\function{eval()}\bifuncindex{eval}. Note that this returns false for
165recursive objects. If the \var{depth} parameter of the
166\class{PrettyPrinter} is set and the object is deeper than allowed,
167this returns false.
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000168\end{funcdesc}
169
170\begin{funcdesc}{isrecursive}{object}
171Determine if the object requires a recursive representation.
172\end{funcdesc}