1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# Author: Fred L. Drake, Jr.
# fdrake@cnri.reston.va.us, fdrake@acm.org
#
# This is a simple little module I wrote to make life easier. I didn't
# see anything quite like it in the library, though I may have overlooked
# something. I wrote this when I was trying to read some heavily nested
# tuples with fairly non-descriptive content. This is modeled very much
# after Lisp/Scheme - style pretty-printing of lists. If you find it
# useful, thank small children who sleep at night.
"""Support to pretty-print lists, tuples, & dictionaries recursively.
Very simple, but useful, especially in debugging data structures.
Classes
-------
PrettyPrinter()
Handle pretty-printing operations onto a stream using a configured
set of formatting parameters.
Functions
---------
pformat()
Format a Python object into a pretty-printed representation.
pprint()
Pretty-print a Python object to a stream [default is sys.sydout].
saferepr()
Generate a 'standard' repr()-like value, but protect against recursive
data structures.
"""
from types import DictType, ListType, TupleType
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
"PrettyPrinter"]
def pprint(object, stream=None):
"""Pretty-print a Python object to a stream [default is sys.sydout]."""
printer = PrettyPrinter(stream=stream)
printer.pprint(object)
def pformat(object):
"""Format a Python object into a pretty-printed representation."""
return PrettyPrinter().pformat(object)
def saferepr(object):
"""Version of repr() which can handle recursive data structures."""
return _safe_repr(object, {})[0]
def isreadable(object):
"""Determine if saferepr(object) is readable by eval()."""
return _safe_repr(object, {})[1]
def isrecursive(object):
"""Determine if object requires a recursive representation."""
return _safe_repr(object, {})[2]
class PrettyPrinter:
def __init__(self, indent=1, width=80, depth=None, stream=None):
"""Handle pretty printing operations onto a stream using a set of
configured parameters.
indent
Number of spaces to indent for each level of nesting.
width
Attempted maximum number of columns in the output.
depth
The maximum depth to print out nested structures.
stream
The desired output stream. If omitted (or false), the standard
output stream available at construction will be used.
"""
indent = int(indent)
width = int(width)
assert indent >= 0
assert depth is None or depth > 0, "depth must be > 0"
assert width
self.__depth = depth
self.__indent_per_level = indent
self.__width = width
if stream:
self.__stream = stream
else:
import sys
self.__stream = sys.stdout
def pprint(self, object):
self.__stream.write(self.pformat(object) + "\n")
def pformat(self, object):
sio = StringIO()
self.__format(object, sio, 0, 0, {}, 0)
return sio.getvalue()
def isrecursive(self, object):
self.__recursive = 0
self.pformat(object)
return self.__recursive
def isreadable(self, object):
self.__recursive = 0
self.__readable = 1
self.pformat(object)
return self.__readable and not self.__recursive
def __format(self, object, stream, indent, allowance, context, level):
level = level + 1
if context.has_key(id(object)):
object = _Recursion(object)
self.__recursive = 1
rep = self.__repr(object, context, level - 1)
objid = id(object)
context[objid] = 1
typ = type(object)
sepLines = len(rep) > (self.__width - 1 - indent - allowance)
if sepLines and typ in (ListType, TupleType):
# Pretty-print the sequence.
stream.write((typ is ListType) and '[' or '(')
if self.__indent_per_level > 1:
stream.write((self.__indent_per_level - 1) * ' ')
length = len(object)
if length:
indent = indent + self.__indent_per_level
self.__format(object[0], stream, indent, allowance + 1,
context, level)
if length > 1:
for ent in object[1:]:
stream.write(',\n' + ' '*indent)
self.__format(ent, stream, indent,
allowance + 1, context, level)
indent = indent - self.__indent_per_level
if typ is TupleType and length == 1:
stream.write(',')
stream.write(((typ is ListType) and ']') or ')')
elif sepLines and typ is DictType:
stream.write('{')
if self.__indent_per_level > 1:
stream.write((self.__indent_per_level - 1) * ' ')
length = len(object)
if length:
indent = indent + self.__indent_per_level
items = object.items()
items.sort()
key, ent = items[0]
rep = self.__repr(key, context, level) + ': '
stream.write(rep)
self.__format(ent, stream, indent + len(rep),
allowance + 1, context, level)
if len(items) > 1:
for key, ent in items[1:]:
rep = self.__repr(key, context, level) + ': '
stream.write(',\n' + ' '*indent + rep)
self.__format(ent, stream, indent + len(rep),
allowance + 1, context, level)
indent = indent - self.__indent_per_level
stream.write('}')
else:
stream.write(rep)
del context[objid]
def __repr(self, object, context, level):
repr, readable, recursive = _safe_repr(object, context,
self.__depth, level)
if not readable:
self.__readable = 0
if recursive:
self.__recursive = 1
return repr
# Return triple (repr_string, isreadable, isrecursive).
def _safe_repr(object, context, maxlevels=None, level=0):
level = level + 1
typ = type(object)
if not (typ in (DictType, ListType, TupleType) and object):
rep = `object`
return rep, (rep and (rep[0] != '<')), 0
if context.has_key(id(object)):
return `_Recursion(object)`, 0, 1
objid = id(object)
context[objid] = 1
readable = 1
recursive = 0
if typ is DictType:
if maxlevels and level >= maxlevels:
s = "{...}"
readable = 0
else:
items = object.items()
k, v = items[0]
krepr, kreadable, krecur = _safe_repr(k, context, maxlevels,
level)
vrepr, vreadable, vrecur = _safe_repr(v, context, maxlevels,
level)
readable = readable and kreadable and vreadable
recursive = recursive or krecur or vrecur
s = "{%s: %s" % (krepr, vrepr)
for k, v in items[1:]:
krepr, kreadable, krecur = _safe_repr(k, context, maxlevels,
level)
vrepr, vreadable, vrecur = _safe_repr(v, context, maxlevels,
level)
readable = readable and kreadable and vreadable
recursive = recursive or krecur or vrecur
s = "%s, %s: %s" % (s, krepr, vrepr)
s = s + "}"
else:
s, term = (typ is ListType) and ('[', ']') or ('(', ')')
if maxlevels and level >= maxlevels:
s = s + "..."
readable = 0
else:
subrepr, subreadable, subrecur = _safe_repr(
object[0], context, maxlevels, level)
readable = readable and subreadable
recursive = recursive or subrecur
s = s + subrepr
tail = object[1:]
if not tail:
if typ is TupleType:
s = s + ','
for ent in tail:
subrepr, subreadable, subrecur = _safe_repr(
ent, context, maxlevels, level)
readable = readable and subreadable
recursive = recursive or subrecur
s = "%s, %s" % (s, subrepr)
s = s + term
del context[objid]
return s, readable and not recursive, recursive
class _Recursion:
# represent a recursive relationship; really only used for the __repr__()
# method...
def __init__(self, object):
self.__repr = "<Recursion on %s with id=%s>" \
% (type(object).__name__, id(object))
def __repr__(self):
return self.__repr
|