blob: 9203b3aed14cc5166463db4d0f7a08e5199d497a [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{pprint} ---
Fred Drakef8ca7d82000-10-10 17:03:45 +00002 Data pretty printer}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake2a2f1fe1999-02-18 21:10:32 +00004\declaremodule{standard}{pprint}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Data pretty printer.}
Fred Drake2a2f1fe1999-02-18 21:10:32 +00006\moduleauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
7\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Fred Drakeee8d3ca1997-07-18 20:41:58 +00009
Fred Drake2c8aa651998-02-20 06:03:52 +000010The \module{pprint} module provides a capability to ``pretty-print''
Fred Drakeee8d3ca1997-07-18 20:41:58 +000011arbitrary Python data structures in a form which can be used as input
12to the interpreter. If the formatted structures include objects which
13are not fundamental Python types, the representation may not be
14loadable. This may be the case if objects such as files, sockets,
15classes, or instances are included, as well as many other builtin
16objects which are not representable as Python constants.
17
18The formatted representation keeps objects on a single line if it can,
Fred Drake12d9eac1997-07-24 15:39:16 +000019and breaks them onto multiple lines if they don't fit within the
Fred Drake2c8aa651998-02-20 06:03:52 +000020allowed width. Construct \class{PrettyPrinter} objects explicitly if
21you need to adjust the width constraint.
Fred Drakeee8d3ca1997-07-18 20:41:58 +000022
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000023\versionchanged[Dictionaries are sorted by key before the display is
24computed; before 2.5, a dictionary was sorted only if its display
25required more than one line, although that wasn't documented]{2.5}
26
Fred Drake2c8aa651998-02-20 06:03:52 +000027The \module{pprint} module defines one class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000028
Fred Drakeee8d3ca1997-07-18 20:41:58 +000029
Fred Drake12d9eac1997-07-24 15:39:16 +000030% First the implementation class:
Fred Drakeee8d3ca1997-07-18 20:41:58 +000031
Fred Drake2c8aa651998-02-20 06:03:52 +000032\begin{classdesc}{PrettyPrinter}{...}
33Construct a \class{PrettyPrinter} instance. This constructor
34understands several keyword parameters. An output stream may be set
35using the \var{stream} keyword; the only method used on the stream
36object is the file protocol's \method{write()} method. If not
37specified, the \class{PrettyPrinter} adopts \code{sys.stdout}. Three
38additional parameters may be used to control the formatted
39representation. The keywords are \var{indent}, \var{depth}, and
40\var{width}. The amount of indentation added for each recursive level
41is specified by \var{indent}; the default is one. Other values can
42cause output to look a little odd, but can make nesting easier to
43spot. The number of levels which may be printed is controlled by
44\var{depth}; if the data structure being printed is too deep, the next
45contained level is replaced by \samp{...}. By default, there is no
46constraint on the depth of the objects being formatted. The desired
47output width is constrained using the \var{width} parameter; the
48default is eighty characters. If a structure cannot be formatted
49within the constrained width, a best effort will be made.
Fred Drakeee8d3ca1997-07-18 20:41:58 +000050
51\begin{verbatim}
Fred Drake12d9eac1997-07-24 15:39:16 +000052>>> import pprint, sys
Fred Drakeee8d3ca1997-07-18 20:41:58 +000053>>> stuff = sys.path[:]
Fred Drake12d9eac1997-07-24 15:39:16 +000054>>> stuff.insert(0, stuff[:])
Fred Drakeee8d3ca1997-07-18 20:41:58 +000055>>> pp = pprint.PrettyPrinter(indent=4)
56>>> pp.pprint(stuff)
57[ [ '',
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 '',
Fred Drake12d9eac1997-07-24 15:39:16 +000064 '/usr/local/lib/python1.5',
65 '/usr/local/lib/python1.5/test',
66 '/usr/local/lib/python1.5/sunos5',
67 '/usr/local/lib/python1.5/sharedmodules',
68 '/usr/local/lib/python1.5/tkinter']
Fred Drakeee8d3ca1997-07-18 20:41:58 +000069>>>
70>>> import parser
71>>> tup = parser.ast2tuple(
72... parser.suite(open('pprint.py').read()))[1][1][1]
73>>> pp = pprint.PrettyPrinter(depth=6)
74>>> pp.pprint(tup)
75(266, (267, (307, (287, (288, (...))))))
76\end{verbatim}
Fred Drake2c8aa651998-02-20 06:03:52 +000077\end{classdesc}
Fred Drake12d9eac1997-07-24 15:39:16 +000078
79
80% Now the derivative functions:
81
Fred Drake2c8aa651998-02-20 06:03:52 +000082The \class{PrettyPrinter} class supports several derivative functions:
Fred Drake12d9eac1997-07-24 15:39:16 +000083
Walter Dörwaldc8de4582003-12-03 20:26:05 +000084\begin{funcdesc}{pformat}{object\optional{, indent\optional{,
85width\optional{, depth}}}}
86Return the formatted representation of \var{object} as a string. \var{indent},
87\var{width} and \var{depth} will be passed to the \class{PrettyPrinter}
88constructor as formatting parameters.
89\versionchanged[The parameters \var{indent}, \var{width} and \var{depth}
90were added]{2.4}
Fred Drake12d9eac1997-07-24 15:39:16 +000091\end{funcdesc}
92
Walter Dörwaldc8de4582003-12-03 20:26:05 +000093\begin{funcdesc}{pprint}{object\optional{, stream\optional{,
94indent\optional{, width\optional{, depth}}}}}
Fred Drake12d9eac1997-07-24 15:39:16 +000095Prints the formatted representation of \var{object} on \var{stream},
96followed by a newline. If \var{stream} is omitted, \code{sys.stdout}
97is used. This may be used in the interactive interpreter instead of a
Walter Dörwaldc8de4582003-12-03 20:26:05 +000098\keyword{print} statement for inspecting values. \var{indent},
99\var{width} and \var{depth} will be passed to the \class{PrettyPrinter}
100constructor as formatting parameters.
Fred Drake12d9eac1997-07-24 15:39:16 +0000101
102\begin{verbatim}
103>>> stuff = sys.path[:]
104>>> stuff.insert(0, stuff)
105>>> pprint.pprint(stuff)
106[<Recursion on list with id=869440>,
107 '',
108 '/usr/local/lib/python1.5',
109 '/usr/local/lib/python1.5/test',
110 '/usr/local/lib/python1.5/sunos5',
111 '/usr/local/lib/python1.5/sharedmodules',
112 '/usr/local/lib/python1.5/tkinter']
113\end{verbatim}
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000114\versionchanged[The parameters \var{indent}, \var{width} and \var{depth}
115were added]{2.4}
Fred Drake12d9eac1997-07-24 15:39:16 +0000116\end{funcdesc}
117
118\begin{funcdesc}{isreadable}{object}
119Determine if the formatted representation of \var{object} is
120``readable,'' or can be used to reconstruct the value using
Fred Drake14c198b1998-04-03 07:11:32 +0000121\function{eval()}\bifuncindex{eval}. This always returns false for
Fred Drake2c8aa651998-02-20 06:03:52 +0000122recursive objects.
Fred Drake12d9eac1997-07-24 15:39:16 +0000123
124\begin{verbatim}
125>>> pprint.isreadable(stuff)
Martin v. Löwisccabed32003-11-27 19:48:03 +0000126False
Fred Drake12d9eac1997-07-24 15:39:16 +0000127\end{verbatim}
128\end{funcdesc}
129
130\begin{funcdesc}{isrecursive}{object}
131Determine if \var{object} requires a recursive representation.
132\end{funcdesc}
133
134
135One more support function is also defined:
136
137\begin{funcdesc}{saferepr}{object}
138Return a string representation of \var{object}, protected against
139recursive data structures. If the representation of \var{object}
140exposes a recursive entry, the recursive reference will be represented
Fred Drake2303d311997-12-17 14:07:25 +0000141as \samp{<Recursion on \var{typename} with id=\var{number}>}. The
Fred Drake12d9eac1997-07-24 15:39:16 +0000142representation is not otherwise formatted.
Fred Drakeb55f9d31998-03-08 07:03:27 +0000143\end{funcdesc}
Fred Drake12d9eac1997-07-24 15:39:16 +0000144
Fred Drakeb55f9d31998-03-08 07:03:27 +0000145% This example is outside the {funcdesc} to keep it from running over
146% the right margin.
Fred Drake12d9eac1997-07-24 15:39:16 +0000147\begin{verbatim}
148>>> pprint.saferepr(stuff)
Fred Drake14c198b1998-04-03 07:11:32 +0000149"[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.5', '/usr/loca
150l/lib/python1.5/test', '/usr/local/lib/python1.5/sunos5', '/usr/local/lib/python
1511.5/sharedmodules', '/usr/local/lib/python1.5/tkinter']"
Fred Drake12d9eac1997-07-24 15:39:16 +0000152\end{verbatim}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000153
154
155\subsection{PrettyPrinter Objects}
Fred Drake2c8aa651998-02-20 06:03:52 +0000156\label{PrettyPrinter Objects}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000157
Fred Drakeb55f9d31998-03-08 07:03:27 +0000158\class{PrettyPrinter} instances have the following methods:
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000159
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000160
Guido van Rossumd8faa362007-04-27 19:54:29 +0000161\begin{methoddesc}[PrettyPrinter]{pformat}{object}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000162Return the formatted representation of \var{object}. This takes into
Fredrik Lundhbb2bf2c2005-12-25 12:05:42 +0000163account the options passed to the \class{PrettyPrinter} constructor.
Fred Drake8fe533e1998-03-27 05:27:08 +0000164\end{methoddesc}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000165
Guido van Rossumd8faa362007-04-27 19:54:29 +0000166\begin{methoddesc}[PrettyPrinter]{pprint}{object}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000167Print the formatted representation of \var{object} on the configured
168stream, followed by a newline.
Fred Drake8fe533e1998-03-27 05:27:08 +0000169\end{methoddesc}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000170
171The following methods provide the implementations for the
172corresponding functions of the same names. Using these methods on an
Fred Drake2c8aa651998-02-20 06:03:52 +0000173instance is slightly more efficient since new \class{PrettyPrinter}
174objects don't need to be created.
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000175
Guido van Rossumd8faa362007-04-27 19:54:29 +0000176\begin{methoddesc}[PrettyPrinter]{isreadable}{object}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000177Determine if the formatted representation of the object is
178``readable,'' or can be used to reconstruct the value using
Fred Drake2c8aa651998-02-20 06:03:52 +0000179\function{eval()}\bifuncindex{eval}. Note that this returns false for
180recursive objects. If the \var{depth} parameter of the
181\class{PrettyPrinter} is set and the object is deeper than allowed,
182this returns false.
Fred Drake8fe533e1998-03-27 05:27:08 +0000183\end{methoddesc}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000184
Guido van Rossumd8faa362007-04-27 19:54:29 +0000185\begin{methoddesc}[PrettyPrinter]{isrecursive}{object}
Fred Drakeee8d3ca1997-07-18 20:41:58 +0000186Determine if the object requires a recursive representation.
Fred Drake8fe533e1998-03-27 05:27:08 +0000187\end{methoddesc}
Fred Drakeaee113d2002-04-02 05:08:35 +0000188
189This method is provided as a hook to allow subclasses to modify the
190way objects are converted to strings. The default implementation uses
191the internals of the \function{saferepr()} implementation.
192
Guido van Rossumd8faa362007-04-27 19:54:29 +0000193\begin{methoddesc}[PrettyPrinter]{format}{object, context, maxlevels, level}
Fred Drakeaee113d2002-04-02 05:08:35 +0000194Returns three values: the formatted version of \var{object} as a
195string, a flag indicating whether the result is readable, and a flag
196indicating whether recursion was detected. The first argument is the
197object to be presented. The second is a dictionary which contains the
198\function{id()} of objects that are part of the current presentation
199context (direct and indirect containers for \var{object} that are
200affecting the presentation) as the keys; if an object needs to be
201presented which is already represented in \var{context}, the third
202return value should be true. Recursive calls to the \method{format()}
Raymond Hettinger68804312005-01-01 00:28:46 +0000203method should add additional entries for containers to this
Fredrik Lundh428b4132005-12-25 11:36:43 +0000204dictionary. The third argument, \var{maxlevels}, gives the requested
Fred Drakeaee113d2002-04-02 05:08:35 +0000205limit to recursion; this will be \code{0} if there is no requested
206limit. This argument should be passed unmodified to recursive calls.
Fredrik Lundh428b4132005-12-25 11:36:43 +0000207The fourth argument, \var{level}, gives the current level; recursive
Fred Drakeaee113d2002-04-02 05:08:35 +0000208calls should be passed a value less than that of the current call.
209\versionadded{2.3}
210\end{methoddesc}