blob: 063287523cb0c49cc55d56462c94bd28778ec750 [file] [log] [blame]
Fred Drakeee8d3ca1997-07-18 20:41:58 +00001%% Author: Fred L. Drake, Jr. <fdrake@acm.org>
2
Fred Drake3a0351c1998-04-04 07:23:21 +00003\section{Standard Module \module{pprint}}
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{pprint}
5
6\modulesynopsis{Data pretty printer.}
7
Fred Drakeee8d3ca1997-07-18 20:41:58 +00008
Fred Drake2c8aa651998-02-20 06:03:52 +00009The \module{pprint} module provides a capability to ``pretty-print''
Fred Drakeee8d3ca1997-07-18 20:41:58 +000010arbitrary Python data structures in a form which can be used as input
11to the interpreter. If the formatted structures include objects which
12are not fundamental Python types, the representation may not be
13loadable. This may be the case if objects such as files, sockets,
14classes, or instances are included, as well as many other builtin
15objects which are not representable as Python constants.
16
17The formatted representation keeps objects on a single line if it can,
Fred Drake12d9eac1997-07-24 15:39:16 +000018and breaks them onto multiple lines if they don't fit within the
Fred Drake2c8aa651998-02-20 06:03:52 +000019allowed width. Construct \class{PrettyPrinter} objects explicitly if
20you need to adjust the width constraint.
Fred Drakeee8d3ca1997-07-18 20:41:58 +000021
Fred Drake2c8aa651998-02-20 06:03:52 +000022The \module{pprint} module defines one class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000023
Fred Drakeee8d3ca1997-07-18 20:41:58 +000024
Fred Drake12d9eac1997-07-24 15:39:16 +000025% First the implementation class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000026
Fred Drake2c8aa651998-02-20 06:03:52 +000027\begin{classdesc}{PrettyPrinter}{...}
28Construct a \class{PrettyPrinter} instance. This constructor
29understands several keyword parameters. An output stream may be set
30using the \var{stream} keyword; the only method used on the stream
31object is the file protocol's \method{write()} method. If not
32specified, the \class{PrettyPrinter} adopts \code{sys.stdout}. Three
33additional parameters may be used to control the formatted
34representation. The keywords are \var{indent}, \var{depth}, and
35\var{width}. The amount of indentation added for each recursive level
36is specified by \var{indent}; the default is one. Other values can
37cause output to look a little odd, but can make nesting easier to
38spot. The number of levels which may be printed is controlled by
39\var{depth}; if the data structure being printed is too deep, the next
40contained level is replaced by \samp{...}. By default, there is no
41constraint on the depth of the objects being formatted. The desired
42output width is constrained using the \var{width} parameter; the
43default is eighty characters. If a structure cannot be formatted
44within the constrained width, a best effort will be made.
Fred Drakeee8d3ca1997-07-18 20:41:58 +000045
46\begin{verbatim}
Fred Drake12d9eac1997-07-24 15:39:16 +000047>>> import pprint, sys
Fred Drakeee8d3ca1997-07-18 20:41:58 +000048>>> stuff = sys.path[:]
Fred Drake12d9eac1997-07-24 15:39:16 +000049>>> stuff.insert(0, stuff[:])
Fred Drakeee8d3ca1997-07-18 20:41:58 +000050>>> pp = pprint.PrettyPrinter(indent=4)
51>>> pp.pprint(stuff)
52[ [ '',
Fred Drake12d9eac1997-07-24 15:39:16 +000053 '/usr/local/lib/python1.5',
54 '/usr/local/lib/python1.5/test',
55 '/usr/local/lib/python1.5/sunos5',
56 '/usr/local/lib/python1.5/sharedmodules',
57 '/usr/local/lib/python1.5/tkinter'],
Fred Drakeee8d3ca1997-07-18 20:41:58 +000058 '',
Fred Drake12d9eac1997-07-24 15:39:16 +000059 '/usr/local/lib/python1.5',
60 '/usr/local/lib/python1.5/test',
61 '/usr/local/lib/python1.5/sunos5',
62 '/usr/local/lib/python1.5/sharedmodules',
63 '/usr/local/lib/python1.5/tkinter']
Fred Drakeee8d3ca1997-07-18 20:41:58 +000064>>>
65>>> import parser
66>>> tup = parser.ast2tuple(
67... parser.suite(open('pprint.py').read()))[1][1][1]
68>>> pp = pprint.PrettyPrinter(depth=6)
69>>> pp.pprint(tup)
70(266, (267, (307, (287, (288, (...))))))
71\end{verbatim}
Fred Drake2c8aa651998-02-20 06:03:52 +000072\end{classdesc}
Fred Drake12d9eac1997-07-24 15:39:16 +000073
74
75% Now the derivative functions:
76
Fred Drake2c8aa651998-02-20 06:03:52 +000077The \class{PrettyPrinter} class supports several derivative functions:
Fred Drake12d9eac1997-07-24 15:39:16 +000078
79\begin{funcdesc}{pformat}{object}
80Return the formatted representation of \var{object} as a string. The
81default parameters for formatting are used.
82\end{funcdesc}
83
84\begin{funcdesc}{pprint}{object\optional{, stream}}
85Prints the formatted representation of \var{object} on \var{stream},
86followed by a newline. If \var{stream} is omitted, \code{sys.stdout}
87is used. This may be used in the interactive interpreter instead of a
Fred Drake2c8aa651998-02-20 06:03:52 +000088\keyword{print} statement for inspecting values. The default
89parameters for formatting are used.
Fred Drake12d9eac1997-07-24 15:39:16 +000090
91\begin{verbatim}
92>>> stuff = sys.path[:]
93>>> stuff.insert(0, stuff)
94>>> pprint.pprint(stuff)
95[<Recursion on list with id=869440>,
96 '',
97 '/usr/local/lib/python1.5',
98 '/usr/local/lib/python1.5/test',
99 '/usr/local/lib/python1.5/sunos5',
100 '/usr/local/lib/python1.5/sharedmodules',
101 '/usr/local/lib/python1.5/tkinter']
102\end{verbatim}
103\end{funcdesc}
104
105\begin{funcdesc}{isreadable}{object}
106Determine if the formatted representation of \var{object} is
107``readable,'' or can be used to reconstruct the value using
Fred Drake14c198b1998-04-03 07:11:32 +0000108\function{eval()}\bifuncindex{eval}. This always returns false for
Fred Drake2c8aa651998-02-20 06:03:52 +0000109recursive objects.
Fred Drake12d9eac1997-07-24 15:39:16 +0000110
111\begin{verbatim}
112>>> pprint.isreadable(stuff)
1130
114\end{verbatim}
115\end{funcdesc}
116
117\begin{funcdesc}{isrecursive}{object}
118Determine if \var{object} requires a recursive representation.
119\end{funcdesc}
120
121
122One more support function is also defined:
123
124\begin{funcdesc}{saferepr}{object}
125Return a string representation of \var{object}, protected against
126recursive data structures. If the representation of \var{object}
127exposes a recursive entry, the recursive reference will be represented
Fred Drake2303d311997-12-17 14:07:25 +0000128as \samp{<Recursion on \var{typename} with id=\var{number}>}. The
Fred Drake12d9eac1997-07-24 15:39:16 +0000129representation is not otherwise formatted.
Fred Drakeb55f9d31998-03-08 07:03:27 +0000130\end{funcdesc}
Fred Drake12d9eac1997-07-24 15:39:16 +0000131
Fred Drakeb55f9d31998-03-08 07:03:27 +0000132% This example is outside the {funcdesc} to keep it from running over
133% the right margin.
Fred Drake12d9eac1997-07-24 15:39:16 +0000134\begin{verbatim}
135>>> pprint.saferepr(stuff)
Fred Drake14c198b1998-04-03 07:11:32 +0000136"[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.5', '/usr/loca
137l/lib/python1.5/test', '/usr/local/lib/python1.5/sunos5', '/usr/local/lib/python
1381.5/sharedmodules', '/usr/local/lib/python1.5/tkinter']"
Fred Drake12d9eac1997-07-24 15:39:16 +0000139\end{verbatim}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000140
141
142\subsection{PrettyPrinter Objects}
Fred Drake2c8aa651998-02-20 06:03:52 +0000143\label{PrettyPrinter Objects}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000144
Fred Drakeb55f9d31998-03-08 07:03:27 +0000145\class{PrettyPrinter} instances have the following methods:
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000146
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000147
Fred Drake8fe533e1998-03-27 05:27:08 +0000148\begin{methoddesc}{pformat}{object}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000149Return the formatted representation of \var{object}. This takes into
Fred Drake8fe533e1998-03-27 05:27:08 +0000150Account the options passed to the \class{PrettyPrinter} constructor.
151\end{methoddesc}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000152
Fred Drake8fe533e1998-03-27 05:27:08 +0000153\begin{methoddesc}{pprint}{object}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000154Print the formatted representation of \var{object} on the configured
155stream, followed by a newline.
Fred Drake8fe533e1998-03-27 05:27:08 +0000156\end{methoddesc}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000157
158The following methods provide the implementations for the
159corresponding functions of the same names. Using these methods on an
Fred Drake2c8aa651998-02-20 06:03:52 +0000160instance is slightly more efficient since new \class{PrettyPrinter}
161objects don't need to be created.
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000162
Fred Drake8fe533e1998-03-27 05:27:08 +0000163\begin{methoddesc}{isreadable}{object}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000164Determine if the formatted representation of the object is
165``readable,'' or can be used to reconstruct the value using
Fred Drake2c8aa651998-02-20 06:03:52 +0000166\function{eval()}\bifuncindex{eval}. Note that this returns false for
167recursive objects. If the \var{depth} parameter of the
168\class{PrettyPrinter} is set and the object is deeper than allowed,
169this returns false.
Fred Drake8fe533e1998-03-27 05:27:08 +0000170\end{methoddesc}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000171
Fred Drake8fe533e1998-03-27 05:27:08 +0000172\begin{methoddesc}{isrecursive}{object}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000173Determine if the object requires a recursive representation.
Fred Drake8fe533e1998-03-27 05:27:08 +0000174\end{methoddesc}