Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 1 | # Author: Fred L. Drake, Jr. |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 2 | # fdrake@cnri.reston.va.us, fdrake@acm.org |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 3 | # |
| 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 | |
| 13 | Very simple, but useful, especially in debugging data structures. |
| 14 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 15 | Classes |
| 16 | ------- |
| 17 | |
| 18 | PrettyPrinter() |
| 19 | Handle pretty-printing operations onto a stream using a configured |
| 20 | set of formatting parameters. |
| 21 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 22 | Functions |
| 23 | --------- |
| 24 | |
| 25 | pformat() |
| 26 | Format a Python object into a pretty-printed representation. |
| 27 | |
| 28 | pprint() |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 29 | Pretty-print a Python object to a stream [default is sys.sydout]. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 30 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 31 | saferepr() |
| 32 | Generate a 'standard' repr()-like value, but protect against recursive |
| 33 | data structures. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 34 | |
| 35 | """ |
| 36 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 37 | from types import DictType, ListType, TupleType |
| 38 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 39 | try: |
| 40 | from cStringIO import StringIO |
| 41 | except ImportError: |
| 42 | from StringIO import StringIO |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 43 | |
| 44 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 45 | def 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 Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 50 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 51 | def pformat(object): |
| 52 | """Format a Python object into a pretty-printed representation.""" |
| 53 | return PrettyPrinter().pformat(object) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 55 | |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 56 | def isreadable(object): |
| 57 | """Determine if saferepr(object) is readable by eval().""" |
| 58 | return PrettyPrinter().isreadable(object) |
| 59 | |
| 60 | |
| 61 | def isrecursive(object): |
| 62 | """Determine if object requires a recursive representation.""" |
| 63 | return PrettyPrinter().isrecursive(object) |
| 64 | |
| 65 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 66 | def saferepr(object): |
| 67 | """Version of repr() which can handle recursive data structures.""" |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 68 | return _safe_repr(object, {})[0] |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 70 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 71 | class 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 Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 75 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 76 | indent |
| 77 | Number of spaces to indent for each level of nesting. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 78 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 79 | width |
| 80 | Attempted maximum number of columns in the output. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 81 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 82 | depth |
| 83 | The maximum depth to print out nested structures. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 84 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 85 | stream |
| 86 | The desired output stream. If omitted (or false), the standard |
| 87 | output stream available at construction will be used. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 88 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 89 | """ |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 90 | indent = int(indent) |
| 91 | width = int(width) |
| 92 | assert indent >= 0 |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 93 | assert (not depth) or depth > 0, "depth may not be negative" |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 94 | assert width |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 95 | 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 Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 103 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 104 | 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 Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 112 | 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 Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 123 | 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 Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 127 | self.__recursive = 1 |
Fred Drake | 5fd026d | 1997-04-18 13:54:13 +0000 | [diff] [blame] | 128 | rep = self.__repr(object, context, level - 1) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 129 | 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 Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 137 | if self.__indent_per_level > 1: |
| 138 | stream.write((self.__indent_per_level - 1) * ' ') |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 139 | length = len(object) |
| 140 | if length: |
| 141 | indent = indent + self.__indent_per_level |
Fred Drake | 5fd026d | 1997-04-18 13:54:13 +0000 | [diff] [blame] | 142 | self.__format(object[0], stream, indent, allowance + 1, |
| 143 | context, level) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 144 | 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 Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 154 | if self.__indent_per_level > 1: |
| 155 | stream.write((self.__indent_per_level - 1) * ' ') |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 156 | 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 Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 176 | stream.write(rep) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 177 | del context[objid] |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 178 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 179 | def __repr(self, object, context, level): |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 180 | repr, readable = _safe_repr(object, context, self.__depth, level) |
| 181 | if not readable: |
| 182 | self.__readable = 0 |
| 183 | return repr |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 185 | |
Fred Drake | f39d051 | 1997-04-16 18:55:58 +0000 | [diff] [blame] | 186 | def _safe_repr(object, context, maxlevels=None, level=0): |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 187 | level = level + 1 |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 188 | readable = 1 |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 189 | typ = type(object) |
| 190 | if not (typ in (DictType, ListType, TupleType) and object): |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 191 | rep = `object` |
| 192 | if rep: |
| 193 | if rep[0] == '<': |
| 194 | readable = 0 |
| 195 | else: |
| 196 | readable = 0 |
| 197 | return `object`, readable |
Fred Drake | f39d051 | 1997-04-16 18:55:58 +0000 | [diff] [blame] | 198 | if context.has_key(id(object)): |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 199 | return `_Recursion(object)`, 0 |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 200 | objid = id(object) |
| 201 | context[objid] = 1 |
| 202 | if typ is DictType: |
| 203 | if maxlevels and level >= maxlevels: |
| 204 | s = "{...}" |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 205 | readable = 0 |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 206 | else: |
| 207 | items = object.items() |
| 208 | k, v = items[0] |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 209 | 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 Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 213 | for k, v in items[1:]: |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 214 | 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 Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 218 | s = s + "}" |
| 219 | else: |
| 220 | s, term = (typ is ListType) and ('[', ']') or ('(', ')') |
| 221 | if maxlevels and level >= maxlevels: |
| 222 | s = s + "..." |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 223 | readable = 0 |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 224 | else: |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 225 | subrepr, subreadable = _safe_repr( |
| 226 | object[0], context, maxlevels, level) |
| 227 | readable = readable and subreadable |
| 228 | s = s + subrepr |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 229 | for ent in object[1:]: |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 230 | subrepr, subreadable = _safe_repr( |
| 231 | ent, context, maxlevels, level) |
| 232 | readable = readable and subreadable |
| 233 | s = "%s, %s" % (s, subrepr) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 234 | s = s + term |
| 235 | del context[objid] |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 236 | return s, readable |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 237 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 238 | |
| 239 | class _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 |