Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-pprint.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-repr.html" />
13<link rel="prev" href="module-codeop.html" />
14<link rel="parent" href="python.html" />
15<link rel="next" href="node96.html" />
16<meta name='aesop' content='information' />
17<title>3.27 pprint -- Data pretty printer</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="3.26 codeop "
25 href="module-codeop.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
28 href="python.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="3.27.1 PrettyPrinter Objects"
31 href="node96.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="module-codeop.html">3.26 codeop </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="node96.html">3.27.1 PrettyPrinter Objects</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0052700000000000000000">
563.27 <tt class="module">pprint</tt> --
57 Data pretty printer</A>
58</H1>
59
60<P>
61<A NAME="module-pprint"></A>
62
63<P>
64The <tt class="module">pprint</tt> module provides a capability to ``pretty-print''
65arbitrary Python data structures in a form which can be used as input
66to the interpreter. If the formatted structures include objects which
67are not fundamental Python types, the representation may not be
68loadable. This may be the case if objects such as files, sockets,
69classes, or instances are included, as well as many other builtin
70objects which are not representable as Python constants.
71
72<P>
73The formatted representation keeps objects on a single line if it can,
74and breaks them onto multiple lines if they don't fit within the
75allowed width. Construct <tt class="class">PrettyPrinter</tt> objects explicitly if
76you need to adjust the width constraint.
77
78<P>
79The <tt class="module">pprint</tt> module defines one class:
80
81<P>
82<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
83 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-762' xml:id='l2h-762' class="class">PrettyPrinter</tt></b>(</nobr></td>
84 <td><var>...</var>)</td></tr></table></dt>
85<dd>
86Construct a <tt class="class">PrettyPrinter</tt> instance. This constructor
87understands several keyword parameters. An output stream may be set
88using the <var>stream</var> keyword; the only method used on the stream
89object is the file protocol's <tt class="method">write()</tt> method. If not
90specified, the <tt class="class">PrettyPrinter</tt> adopts <code>sys.stdout</code>. Three
91additional parameters may be used to control the formatted
92representation. The keywords are <var>indent</var>, <var>depth</var>, and
93<var>width</var>. The amount of indentation added for each recursive level
94is specified by <var>indent</var>; the default is one. Other values can
95cause output to look a little odd, but can make nesting easier to
96spot. The number of levels which may be printed is controlled by
97<var>depth</var>; if the data structure being printed is too deep, the next
98contained level is replaced by "<tt class="samp">...</tt>". By default, there is no
99constraint on the depth of the objects being formatted. The desired
100output width is constrained using the <var>width</var> parameter; the
101default is eighty characters. If a structure cannot be formatted
102within the constrained width, a best effort will be made.
103
104<P>
105<div class="verbatim"><pre>
106&gt;&gt;&gt; import pprint, sys
107&gt;&gt;&gt; stuff = sys.path[:]
108&gt;&gt;&gt; stuff.insert(0, stuff[:])
109&gt;&gt;&gt; pp = pprint.PrettyPrinter(indent=4)
110&gt;&gt;&gt; pp.pprint(stuff)
111[ [ '',
112 '/usr/local/lib/python1.5',
113 '/usr/local/lib/python1.5/test',
114 '/usr/local/lib/python1.5/sunos5',
115 '/usr/local/lib/python1.5/sharedmodules',
116 '/usr/local/lib/python1.5/tkinter'],
117 '',
118 '/usr/local/lib/python1.5',
119 '/usr/local/lib/python1.5/test',
120 '/usr/local/lib/python1.5/sunos5',
121 '/usr/local/lib/python1.5/sharedmodules',
122 '/usr/local/lib/python1.5/tkinter']
123&gt;&gt;&gt;
124&gt;&gt;&gt; import parser
125&gt;&gt;&gt; tup = parser.ast2tuple(
126... parser.suite(open('pprint.py').read()))[1][1][1]
127&gt;&gt;&gt; pp = pprint.PrettyPrinter(depth=6)
128&gt;&gt;&gt; pp.pprint(tup)
129(266, (267, (307, (287, (288, (...))))))
130</pre></div>
131</dl>
132
133<P>
134The <tt class="class">PrettyPrinter</tt> class supports several derivative functions:
135
136<P>
137<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
138 <td><nobr><b><tt id='l2h-763' xml:id='l2h-763' class="function">pformat</tt></b>(</nobr></td>
139 <td><var>object</var><big>[</big><var>, indent</var><big>[</big><var>,
140width</var><big>[</big><var>, depth</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
141<dd>
142Return the formatted representation of <var>object</var> as a string. <var>indent</var>,
143<var>width</var> and <var>depth</var> will be passed to the <tt class="class">PrettyPrinter</tt>
144constructor as formatting parameters.
145
146<span class="versionnote">Changed in version 2.4:
147The parameters <var>indent</var>, <var>width</var> and <var>depth</var>
148were added.</span>
149
150</dl>
151
152<P>
153<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
154 <td><nobr><b><tt id='l2h-764' xml:id='l2h-764' class="function">pprint</tt></b>(</nobr></td>
155 <td><var>object</var><big>[</big><var>, stream</var><big>[</big><var>,
156indent</var><big>[</big><var>, width</var><big>[</big><var>, depth</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
157<dd>
158Prints the formatted representation of <var>object</var> on <var>stream</var>,
159followed by a newline. If <var>stream</var> is omitted, <code>sys.stdout</code>
160is used. This may be used in the interactive interpreter instead of a
161<tt class="keyword">print</tt> statement for inspecting values. <var>indent</var>,
162<var>width</var> and <var>depth</var> will be passed to the <tt class="class">PrettyPrinter</tt>
163constructor as formatting parameters.
164
165<P>
166<div class="verbatim"><pre>
167&gt;&gt;&gt; stuff = sys.path[:]
168&gt;&gt;&gt; stuff.insert(0, stuff)
169&gt;&gt;&gt; pprint.pprint(stuff)
170[&lt;Recursion on list with id=869440&gt;,
171 '',
172 '/usr/local/lib/python1.5',
173 '/usr/local/lib/python1.5/test',
174 '/usr/local/lib/python1.5/sunos5',
175 '/usr/local/lib/python1.5/sharedmodules',
176 '/usr/local/lib/python1.5/tkinter']
177</pre></div>
178
179<span class="versionnote">Changed in version 2.4:
180The parameters <var>indent</var>, <var>width</var> and <var>depth</var>
181were added.</span>
182
183</dl>
184
185<P>
186<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
187 <td><nobr><b><tt id='l2h-765' xml:id='l2h-765' class="function">isreadable</tt></b>(</nobr></td>
188 <td><var>object</var>)</td></tr></table></dt>
189<dd>
190Determine if the formatted representation of <var>object</var> is
191``readable,'' or can be used to reconstruct the value using
192<tt class="function">eval()</tt><a id='l2h-766' xml:id='l2h-766'></a>. This always returns false for
193recursive objects.
194
195<P>
196<div class="verbatim"><pre>
197&gt;&gt;&gt; pprint.isreadable(stuff)
198False
199</pre></div>
200</dl>
201
202<P>
203<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
204 <td><nobr><b><tt id='l2h-767' xml:id='l2h-767' class="function">isrecursive</tt></b>(</nobr></td>
205 <td><var>object</var>)</td></tr></table></dt>
206<dd>
207Determine if <var>object</var> requires a recursive representation.
208</dl>
209
210<P>
211One more support function is also defined:
212
213<P>
214<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
215 <td><nobr><b><tt id='l2h-768' xml:id='l2h-768' class="function">saferepr</tt></b>(</nobr></td>
216 <td><var>object</var>)</td></tr></table></dt>
217<dd>
218Return a string representation of <var>object</var>, protected against
219recursive data structures. If the representation of <var>object</var>
220exposes a recursive entry, the recursive reference will be represented
221as "<tt class="samp">&lt;Recursion on <var>typename</var> with id=<var>number</var>&gt;</tt>". The
222representation is not otherwise formatted.
223</dl>
224
225<P>
226<div class="verbatim"><pre>
227&gt;&gt;&gt; pprint.saferepr(stuff)
228"[&lt;Recursion on list with id=682968&gt;, '', '/usr/local/lib/python1.5', '/usr/loca
229l/lib/python1.5/test', '/usr/local/lib/python1.5/sunos5', '/usr/local/lib/python
2301.5/sharedmodules', '/usr/local/lib/python1.5/tkinter']"
231</pre></div>
232
233<P>
234
235<p><br /></p><hr class='online-navigation' />
236<div class='online-navigation'>
237<!--Table of Child-Links-->
238<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
239
240<UL CLASS="ChildLinks">
241<LI><A href="node96.html">3.27.1 PrettyPrinter Objects</a>
242</ul>
243<!--End of Table of Child-Links-->
244</div>
245
246<DIV CLASS="navigation">
247<div class='online-navigation'>
248<p></p><hr />
249<table align="center" width="100%" cellpadding="0" cellspacing="2">
250<tr>
251<td class='online-navigation'><a rel="prev" title="3.26 codeop "
252 href="module-codeop.html"><img src='../icons/previous.png'
253 border='0' height='32' alt='Previous Page' width='32' /></A></td>
254<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
255 href="python.html"><img src='../icons/up.png'
256 border='0' height='32' alt='Up One Level' width='32' /></A></td>
257<td class='online-navigation'><a rel="next" title="3.27.1 PrettyPrinter Objects"
258 href="node96.html"><img src='../icons/next.png'
259 border='0' height='32' alt='Next Page' width='32' /></A></td>
260<td align="center" width="100%">Python Library Reference</td>
261<td class='online-navigation'><a rel="contents" title="Table of Contents"
262 href="contents.html"><img src='../icons/contents.png'
263 border='0' height='32' alt='Contents' width='32' /></A></td>
264<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
265 border='0' height='32' alt='Module Index' width='32' /></a></td>
266<td class='online-navigation'><a rel="index" title="Index"
267 href="genindex.html"><img src='../icons/index.png'
268 border='0' height='32' alt='Index' width='32' /></A></td>
269</tr></table>
270<div class='online-navigation'>
271<b class="navlabel">Previous:</b>
272<a class="sectref" rel="prev" href="module-codeop.html">3.26 codeop </A>
273<b class="navlabel">Up:</b>
274<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
275<b class="navlabel">Next:</b>
276<a class="sectref" rel="next" href="node96.html">3.27.1 PrettyPrinter Objects</A>
277</div>
278</div>
279<hr />
280<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
281</DIV>
282<!--End of Navigation Panel-->
283<ADDRESS>
284See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
285</ADDRESS>
286</BODY>
287</HTML>