@@ -12,46 +12,15 @@ \section{Standard module \sectcode{pprint}}
1212objects which are not representable as Python constants.
1313
1414The formatted representation keeps objects on a single line if it can,
15- and breaks them out onto multiple lines if they won 't fit within the
16- width allowed width. Construct PrettyPrinter objects explicitly if
17- you need to adjust the width constraint.
15+ and breaks them onto multiple lines if they don 't fit within the
16+ allowed width. Construct PrettyPrinter objects explicitly if you need
17+ to adjust the width constraint.
1818
19- The \code {pprint} module defines the following functions :
19+ The \code {pprint} module defines one class :
2020
2121\renewcommand {\indexsubitem }{(in module pprint)}
2222
23- \begin {funcdesc }{pformat}{object}
24- Return the formatted representation of \var {object} as a string. The
25- default parameters for formatting are used.
26- \end {funcdesc }
27-
28- \begin {funcdesc }{pprint}{object\optional {, stream}}
29- Prints the formatted representation of \var {object} on \var {stream},
30- followed by a newline. If \var {stream} is omitted, \code {sys.stdout}
31- is used. This may be used in the interactive interpreter instead of a
32- \code {print} command for inspecting values. The default parameters
33- for formatting are used.
34- \end {funcdesc }
35-
36- \begin {funcdesc }{isreadable}{object}
37- Determine if the formatted representation of \var {object} is
38- `` readable,'' or can be used to reconstruct the value using
39- \code {eval()}. Note that this returns false for recursive objects.
40- \end {funcdesc }
41-
42- \begin {funcdesc }{isrecursive}{object}
43- Determine if \var {object} requires a recursive representation.
44- \end {funcdesc }
45-
46- \begin {funcdesc }{saferepr}{object}
47- Return a string representation of \var {object}, protected against
48- recursive data structures. If the representation of \var {object}
49- exposes a recursive entry, the recursive reference will be represented
50- as \samp {$ <$ Recursion on \var {typename} with id=\var {number}$ >$ }.
51- \end {funcdesc }
52-
53-
54- % Now for the implementation class:
23+ % First the implementation class:
5524
5625\begin {funcdesc }{PrettyPrinter}{...}
5726Construct a PrettyPrinter instance. This constructor understands
@@ -71,42 +40,25 @@ \section{Standard module \sectcode{pprint}}
7140using the \var {width} parameter; the default is eighty characters. If
7241a structure cannot be formatted within the constrained width, a best
7342effort will be made.
74- \end {funcdesc }
75-
76-
77- % Guido marked this as a good spot for an example in the template,
78- % but I think this needs a better location in this module. Not sure where.
79-
80- Example:
8143
8244\begin {verbatim }
83- >>> import pprint
45+ >>> import pprint, sys
8446>>> stuff = sys.path[:]
85- >>> stuff.insert(0, stuff)
86- >>> pprint.pprint(stuff)
87- [<Recursion on list with id=869440>,
88- '',
89- '/usr/local/lib/python1.4',
90- '/usr/local/lib/python1.4/test',
91- '/usr/local/lib/python1.4/sunos5',
92- '/usr/local/lib/python1.4/sharedmodules',
93- '/usr/local/lib/python1.4/tkinter']
94- >>>
95- >>> stuff[0] = stuff[1:]
47+ >>> stuff.insert(0, stuff[:])
9648>>> pp = pprint.PrettyPrinter(indent=4)
9749>>> pp.pprint(stuff)
9850[ [ '',
99- '/usr/local/lib/python1.4 ',
100- '/usr/local/lib/python1.4 /test',
101- '/usr/local/lib/python1.4 /sunos5',
102- '/usr/local/lib/python1.4 /sharedmodules',
103- '/usr/local/lib/python1.4 /tkinter'],
51+ '/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'],
10456 '',
105- '/usr/local/lib/python1.4 ',
106- '/usr/local/lib/python1.4 /test',
107- '/usr/local/lib/python1.4 /sunos5',
108- '/usr/local/lib/python1.4 /sharedmodules',
109- '/usr/local/lib/python1.4 /tkinter']
57+ '/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']
11062>>>
11163>>> import parser
11264>>> tup = parser.ast2tuple(
@@ -115,6 +67,71 @@ \section{Standard module \sectcode{pprint}}
11567>>> pp.pprint(tup)
11668(266, (267, (307, (287, (288, (...))))))
11769\end {verbatim }
70+ \end {funcdesc }
71+
72+
73+ % Now the derivative functions:
74+
75+ The PrettyPrinter class supports several derivative functions:
76+
77+ \begin {funcdesc }{pformat}{object}
78+ Return the formatted representation of \var {object} as a string. The
79+ default parameters for formatting are used.
80+ \end {funcdesc }
81+
82+ \begin {funcdesc }{pprint}{object\optional {, stream}}
83+ Prints the formatted representation of \var {object} on \var {stream},
84+ followed by a newline. If \var {stream} is omitted, \code {sys.stdout}
85+ is used. This may be used in the interactive interpreter instead of a
86+ \code {print} command for inspecting values. The default parameters
87+ for formatting are used.
88+
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}
104+ Determine if the formatted representation of \var {object} is
105+ `` readable,'' or can be used to reconstruct the value using
106+ \code {eval()}. Note that this returns false for recursive objects.
107+
108+ \begin {verbatim }
109+ >>> pprint.isreadable(stuff)
110+ 0
111+ \end {verbatim }
112+ \end {funcdesc }
113+
114+ \begin {funcdesc }{isrecursive}{object}
115+ Determine if \var {object} requires a recursive representation.
116+ \end {funcdesc }
117+
118+
119+ One more support function is also defined:
120+
121+ \begin {funcdesc }{saferepr}{object}
122+ Return a string representation of \var {object}, protected against
123+ recursive data structures. If the representation of \var {object}
124+ exposes a recursive entry, the recursive reference will be represented
125+ as \samp {$ <$ Recursion on \var {typename} with id=\var {number}$ >$ }. The
126+ representation is not otherwise formatted.
127+
128+ \begin {verbatim }
129+ >>> pprint.saferepr(stuff)
130+ "[<Recursion on list with id=682968>, '', '/usr/local/lib/python1.4', '/usr/loca
131+ l/lib/python1.4/test', '/usr/local/lib/python1.4/sunos5', '/usr/local/lib/python
132+ 1.4/sharedmodules', '/usr/local/lib/python1.4/tkinter']"
133+ \end {verbatim }
134+ \end {funcdesc }
118135
119136
120137\subsection {PrettyPrinter Objects }
0 commit comments