blob: 7f0ab72428ca29dc5885854d863882380130db49 [file] [log] [blame]
Guido van Rossum5e92aff1997-04-16 00:49:59 +00001# Author: Fred L. Drake, Jr.
Fred Drakea89fda01997-04-16 16:59:30 +00002# fdrake@cnri.reston.va.us, fdrake@acm.org
Guido van Rossum5e92aff1997-04-16 00:49:59 +00003#
4# This is a simple little module I wrote to make life easier. I didn't
5# see anything quite like it in the library, though I may have overlooked
6# something. I wrote this when I was trying to read some heavily nested
7# tuples with fairly non-descriptive content. This is modelled very much
8# after Lisp/Scheme - style pretty-printing of lists. If you find it
9# useful, thank small children who sleep at night.
10
11"""Support to pretty-print lists, tuples, & dictionaries recursively.
12
13Very simple, but useful, especially in debugging data structures.
14
Fred Drakea89fda01997-04-16 16:59:30 +000015Classes
16-------
17
18PrettyPrinter()
19 Handle pretty-printing operations onto a stream using a configured
20 set of formatting parameters.
21
Guido van Rossum5e92aff1997-04-16 00:49:59 +000022Functions
23---------
24
25pformat()
26 Format a Python object into a pretty-printed representation.
27
28pprint()
Fred Drakea89fda01997-04-16 16:59:30 +000029 Pretty-print a Python object to a stream [default is sys.sydout].
Guido van Rossum5e92aff1997-04-16 00:49:59 +000030
Fred Drakea89fda01997-04-16 16:59:30 +000031saferepr()
32 Generate a 'standard' repr()-like value, but protect against recursive
33 data structures.
Guido van Rossum5e92aff1997-04-16 00:49:59 +000034
35"""
36
Guido van Rossum5e92aff1997-04-16 00:49:59 +000037from types import DictType, ListType, TupleType
38
Fred Drakea89fda01997-04-16 16:59:30 +000039try:
40 from cStringIO import StringIO
41except ImportError:
42 from StringIO import StringIO
Guido van Rossum5e92aff1997-04-16 00:49:59 +000043
44
Fred Drakea89fda01997-04-16 16:59:30 +000045def pprint(object, stream=None):
46 """Pretty-print a Python object to a stream [default is sys.sydout]."""
47 printer = PrettyPrinter(stream=stream)
48 printer.pprint(object)
Guido van Rossum5e92aff1997-04-16 00:49:59 +000049
Guido van Rossum5e92aff1997-04-16 00:49:59 +000050
Fred Drakea89fda01997-04-16 16:59:30 +000051def pformat(object):
52 """Format a Python object into a pretty-printed representation."""
53 return PrettyPrinter().pformat(object)
Guido van Rossum5e92aff1997-04-16 00:49:59 +000054
Guido van Rossum5e92aff1997-04-16 00:49:59 +000055
Fred Drakee0ffabe1997-07-18 20:42:39 +000056def isreadable(object):
57 """Determine if saferepr(object) is readable by eval()."""
58 return PrettyPrinter().isreadable(object)
59
60
61def isrecursive(object):
62 """Determine if object requires a recursive representation."""
63 return PrettyPrinter().isrecursive(object)
64
65
Fred Drakea89fda01997-04-16 16:59:30 +000066def saferepr(object):
67 """Version of repr() which can handle recursive data structures."""
Fred Drakee0ffabe1997-07-18 20:42:39 +000068 return _safe_repr(object, {})[0]
Guido van Rossum5e92aff1997-04-16 00:49:59 +000069
Guido van Rossum5e92aff1997-04-16 00:49:59 +000070
Fred Drakea89fda01997-04-16 16:59:30 +000071class PrettyPrinter:
72 def __init__(self, indent=1, width=80, depth=None, stream=None):
73 """Handle pretty printing operations onto a stream using a set of
74 configured parameters.
Guido van Rossum5e92aff1997-04-16 00:49:59 +000075
Fred Drakea89fda01997-04-16 16:59:30 +000076 indent
77 Number of spaces to indent for each level of nesting.
Guido van Rossum5e92aff1997-04-16 00:49:59 +000078
Fred Drakea89fda01997-04-16 16:59:30 +000079 width
80 Attempted maximum number of columns in the output.
Guido van Rossum5e92aff1997-04-16 00:49:59 +000081
Fred Drakea89fda01997-04-16 16:59:30 +000082 depth
83 The maximum depth to print out nested structures.
Guido van Rossum5e92aff1997-04-16 00:49:59 +000084
Fred Drakea89fda01997-04-16 16:59:30 +000085 stream
86 The desired output stream. If omitted (or false), the standard
87 output stream available at construction will be used.
Guido van Rossum5e92aff1997-04-16 00:49:59 +000088
Fred Drakea89fda01997-04-16 16:59:30 +000089 """
Fred Drakee0ffabe1997-07-18 20:42:39 +000090 indent = int(indent)
91 width = int(width)
92 assert indent >= 0
Fred Drakea89fda01997-04-16 16:59:30 +000093 assert (not depth) or depth > 0, "depth may not be negative"
Fred Drakee0ffabe1997-07-18 20:42:39 +000094 assert width
Fred Drakea89fda01997-04-16 16:59:30 +000095 self.__depth = depth
96 self.__indent_per_level = indent
97 self.__width = width
98 if stream:
99 self.__stream = stream
100 else:
101 import sys
102 self.__stream = sys.stdout
Guido van Rossum5e92aff1997-04-16 00:49:59 +0000103
Fred Drakea89fda01997-04-16 16:59:30 +0000104 def pprint(self, object):
105 self.__stream.write(self.pformat(object) + "\n")
106
107 def pformat(self, object):
108 sio = StringIO()
109 self.__format(object, sio, 0, 0, {}, 0)
110 return sio.getvalue()
111
Fred Drakee0ffabe1997-07-18 20:42:39 +0000112 def isrecursive(self, object):
113 self.__recursive = 0
114 self.pformat(object)
115 return self.__recursive
116
117 def isreadable(self, object):
118 self.__recursive = 0
119 self.__readable = 1
120 self.pformat(object)
121 return self.__readable and not self.__recursive
122
Fred Drakea89fda01997-04-16 16:59:30 +0000123 def __format(self, object, stream, indent, allowance, context, level):
124 level = level + 1
125 if context.has_key(id(object)):
126 object = _Recursion(object)
Fred Drakee0ffabe1997-07-18 20:42:39 +0000127 self.__recursive = 1
Fred Drake5fd026d1997-04-18 13:54:13 +0000128 rep = self.__repr(object, context, level - 1)
Fred Drakea89fda01997-04-16 16:59:30 +0000129 objid = id(object)
130 context[objid] = 1
131 typ = type(object)
132 sepLines = len(rep) > (self.__width - 1 - indent - allowance)
133
134 if sepLines and typ in (ListType, TupleType):
135 # Pretty-print the sequence.
136 stream.write((typ is ListType) and '[' or '(')
Fred Drakee0ffabe1997-07-18 20:42:39 +0000137 if self.__indent_per_level > 1:
138 stream.write((self.__indent_per_level - 1) * ' ')
Fred Drakea89fda01997-04-16 16:59:30 +0000139 length = len(object)
140 if length:
141 indent = indent + self.__indent_per_level
Fred Drake5fd026d1997-04-18 13:54:13 +0000142 self.__format(object[0], stream, indent, allowance + 1,
143 context, level)
Fred Drakea89fda01997-04-16 16:59:30 +0000144 if len(object) > 1:
145 for ent in object[1:]:
146 stream.write(',\n' + ' '*indent)
147 self.__format(ent, stream, indent,
148 allowance + 1, context, level)
149 indent = indent - self.__indent_per_level
150 stream.write(((typ is ListType) and ']') or ')')
151
152 elif sepLines and typ is DictType:
153 stream.write('{')
Fred Drakee0ffabe1997-07-18 20:42:39 +0000154 if self.__indent_per_level > 1:
155 stream.write((self.__indent_per_level - 1) * ' ')
Fred Drakea89fda01997-04-16 16:59:30 +0000156 length = len(object)
157 if length:
158 indent = indent + self.__indent_per_level
159 items = object.items()
160 items.sort()
161 key, ent = items[0]
162 rep = self.__repr(key, context, level) + ': '
163 stream.write(rep)
164 self.__format(ent, stream, indent + len(rep),
165 allowance + 1, context, level)
166 if len(items) > 1:
167 for key, ent in items[1:]:
168 rep = self.__repr(key, context, level) + ': '
169 stream.write(',\n' + ' '*indent + rep)
170 self.__format(ent, stream, indent + len(rep),
171 allowance + 1, context, level)
172 indent = indent - self.__indent_per_level
173 stream.write('}')
174
175 else:
Guido van Rossum5e92aff1997-04-16 00:49:59 +0000176 stream.write(rep)
Fred Drakea89fda01997-04-16 16:59:30 +0000177 del context[objid]
Guido van Rossum5e92aff1997-04-16 00:49:59 +0000178
Fred Drakea89fda01997-04-16 16:59:30 +0000179 def __repr(self, object, context, level):
Fred Drakee0ffabe1997-07-18 20:42:39 +0000180 repr, readable = _safe_repr(object, context, self.__depth, level)
181 if not readable:
182 self.__readable = 0
183 return repr
Guido van Rossum5e92aff1997-04-16 00:49:59 +0000184
Guido van Rossum5e92aff1997-04-16 00:49:59 +0000185
Fred Drakef39d0511997-04-16 18:55:58 +0000186def _safe_repr(object, context, maxlevels=None, level=0):
Fred Drakea89fda01997-04-16 16:59:30 +0000187 level = level + 1
Fred Drakee0ffabe1997-07-18 20:42:39 +0000188 readable = 1
Fred Drakea89fda01997-04-16 16:59:30 +0000189 typ = type(object)
190 if not (typ in (DictType, ListType, TupleType) and object):
Fred Drakee0ffabe1997-07-18 20:42:39 +0000191 rep = `object`
192 if rep:
193 if rep[0] == '<':
194 readable = 0
195 else:
196 readable = 0
197 return `object`, readable
Fred Drakef39d0511997-04-16 18:55:58 +0000198 if context.has_key(id(object)):
Fred Drakee0ffabe1997-07-18 20:42:39 +0000199 return `_Recursion(object)`, 0
Fred Drakea89fda01997-04-16 16:59:30 +0000200 objid = id(object)
201 context[objid] = 1
202 if typ is DictType:
203 if maxlevels and level >= maxlevels:
204 s = "{...}"
Fred Drakee0ffabe1997-07-18 20:42:39 +0000205 readable = 0
Fred Drakea89fda01997-04-16 16:59:30 +0000206 else:
207 items = object.items()
208 k, v = items[0]
Fred Drakee0ffabe1997-07-18 20:42:39 +0000209 krepr, kreadable = _safe_repr(k, context, maxlevels, level)
210 vrepr, vreadable = _safe_repr(v, context, maxlevels, level)
211 readable = readable and kreadable and vreadable
212 s = "{%s: %s" % (krepr, vrepr)
Fred Drakea89fda01997-04-16 16:59:30 +0000213 for k, v in items[1:]:
Fred Drakee0ffabe1997-07-18 20:42:39 +0000214 krepr, kreadable = _safe_repr(k, context, maxlevels, level)
215 vrepr, vreadable = _safe_repr(v, context, maxlevels, level)
216 readable = readable and kreadable and vreadable
217 s = "%s, %s: %s" % (s, krepr, vrepr)
Fred Drakea89fda01997-04-16 16:59:30 +0000218 s = s + "}"
219 else:
220 s, term = (typ is ListType) and ('[', ']') or ('(', ')')
221 if maxlevels and level >= maxlevels:
222 s = s + "..."
Fred Drakee0ffabe1997-07-18 20:42:39 +0000223 readable = 0
Fred Drakea89fda01997-04-16 16:59:30 +0000224 else:
Fred Drakee0ffabe1997-07-18 20:42:39 +0000225 subrepr, subreadable = _safe_repr(
226 object[0], context, maxlevels, level)
227 readable = readable and subreadable
228 s = s + subrepr
Fred Drakea89fda01997-04-16 16:59:30 +0000229 for ent in object[1:]:
Fred Drakee0ffabe1997-07-18 20:42:39 +0000230 subrepr, subreadable = _safe_repr(
231 ent, context, maxlevels, level)
232 readable = readable and subreadable
233 s = "%s, %s" % (s, subrepr)
Fred Drakea89fda01997-04-16 16:59:30 +0000234 s = s + term
235 del context[objid]
Fred Drakee0ffabe1997-07-18 20:42:39 +0000236 return s, readable
Guido van Rossum5e92aff1997-04-16 00:49:59 +0000237
Fred Drakea89fda01997-04-16 16:59:30 +0000238
239class _Recursion:
240 # represent a recursive relationship; really only used for the __repr__()
241 # method...
242 def __init__(self, object):
243 self.__repr = "<Recursion on %s with id=%s>" \
244 % (type(object).__name__, id(object))
245
246 def __repr__(self):
247 return self.__repr