Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1 | # Author: Fred L. Drake, Jr. |
Fred Drake | 3e5e661 | 2001-10-09 20:53:48 +0000 | [diff] [blame] | 2 | # 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 |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7 | # tuples with fairly non-descriptive content. This is modeled very much |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 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() |
Skip Montanaro | 2dc0c13 | 2004-05-14 16:31:56 +0000 | [diff] [blame] | 29 | Pretty-print a Python object to a stream [default is sys.stdout]. |
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 | |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 37 | import sys as _sys |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 38 | |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 39 | from cStringIO import StringIO as _StringIO |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 40 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 41 | __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", |
| 42 | "PrettyPrinter"] |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 43 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 44 | # cache these for faster access: |
| 45 | _commajoin = ", ".join |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 46 | _id = id |
| 47 | _len = len |
| 48 | _type = type |
| 49 | |
| 50 | |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 51 | def pprint(object, stream=None, indent=1, width=80, depth=None): |
Skip Montanaro | 2dc0c13 | 2004-05-14 16:31:56 +0000 | [diff] [blame] | 52 | """Pretty-print a Python object to a stream [default is sys.stdout].""" |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 53 | printer = PrettyPrinter( |
| 54 | stream=stream, indent=indent, width=width, depth=depth) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 55 | printer.pprint(object) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 56 | |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 57 | def pformat(object, indent=1, width=80, depth=None): |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 58 | """Format a Python object into a pretty-printed representation.""" |
Walter Dörwald | c8de458 | 2003-12-03 20:26:05 +0000 | [diff] [blame] | 59 | return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 60 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 61 | def saferepr(object): |
| 62 | """Version of repr() which can handle recursive data structures.""" |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 63 | return _safe_repr(object, {}, None, 0)[0] |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 64 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 65 | def isreadable(object): |
| 66 | """Determine if saferepr(object) is readable by eval().""" |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 67 | return _safe_repr(object, {}, None, 0)[1] |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 68 | |
| 69 | def isrecursive(object): |
| 70 | """Determine if object requires a recursive representation.""" |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 71 | return _safe_repr(object, {}, None, 0)[2] |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 72 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 73 | class PrettyPrinter: |
| 74 | def __init__(self, indent=1, width=80, depth=None, stream=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 75 | """Handle pretty printing operations onto a stream using a set of |
| 76 | configured parameters. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 78 | indent |
| 79 | Number of spaces to indent for each level of nesting. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 81 | width |
| 82 | Attempted maximum number of columns in the output. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 84 | depth |
| 85 | The maximum depth to print out nested structures. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | stream |
| 88 | The desired output stream. If omitted (or false), the standard |
| 89 | output stream available at construction will be used. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | """ |
| 92 | indent = int(indent) |
| 93 | width = int(width) |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 94 | assert indent >= 0, "indent must be >= 0" |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 95 | assert depth is None or depth > 0, "depth must be > 0" |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 96 | assert width, "width must be != 0" |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 97 | self._depth = depth |
| 98 | self._indent_per_level = indent |
| 99 | self._width = width |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 100 | if stream is not None: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 101 | self._stream = stream |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 102 | else: |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 103 | self._stream = _sys.stdout |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 104 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 105 | def pprint(self, object): |
Walter Dörwald | e62e936 | 2005-11-11 18:18:51 +0000 | [diff] [blame] | 106 | self._format(object, self._stream, 0, 0, {}, 0) |
| 107 | self._stream.write("\n") |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 108 | |
| 109 | def pformat(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 110 | sio = _StringIO() |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 111 | self._format(object, sio, 0, 0, {}, 0) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 112 | return sio.getvalue() |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 113 | |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 114 | def isrecursive(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 115 | return self.format(object, {}, 0, 0)[2] |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 116 | |
| 117 | def isreadable(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 118 | s, readable, recursive = self.format(object, {}, 0, 0) |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 119 | return readable and not recursive |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 120 | |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 121 | def _format(self, object, stream, indent, allowance, context, level): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 122 | level = level + 1 |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 123 | objid = _id(object) |
| 124 | if objid in context: |
| 125 | stream.write(_recursion(object)) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 126 | self._recursive = True |
| 127 | self._readable = False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 128 | return |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 129 | rep = self._repr(object, context, level - 1) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 130 | typ = _type(object) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 131 | sepLines = _len(rep) > (self._width - 1 - indent - allowance) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 132 | write = stream.write |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 133 | |
Georg Brandl | 23da6e6 | 2008-05-12 16:26:52 +0000 | [diff] [blame] | 134 | if self._depth and level > self._depth: |
| 135 | write(rep) |
| 136 | return |
| 137 | |
Georg Brandl | dcd6b52 | 2008-01-20 11:13:29 +0000 | [diff] [blame] | 138 | r = getattr(typ, "__repr__", None) |
| 139 | if issubclass(typ, dict) and r is dict.__repr__: |
| 140 | write('{') |
| 141 | if self._indent_per_level > 1: |
| 142 | write((self._indent_per_level - 1) * ' ') |
| 143 | length = _len(object) |
| 144 | if length: |
| 145 | context[objid] = 1 |
| 146 | indent = indent + self._indent_per_level |
| 147 | items = object.items() |
| 148 | items.sort() |
| 149 | key, ent = items[0] |
| 150 | rep = self._repr(key, context, level) |
| 151 | write(rep) |
| 152 | write(': ') |
| 153 | self._format(ent, stream, indent + _len(rep) + 2, |
| 154 | allowance + 1, context, level) |
| 155 | if length > 1: |
| 156 | for key, ent in items[1:]: |
| 157 | rep = self._repr(key, context, level) |
| 158 | if sepLines: |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 159 | write(',\n%s%s: ' % (' '*indent, rep)) |
Georg Brandl | dcd6b52 | 2008-01-20 11:13:29 +0000 | [diff] [blame] | 160 | else: |
| 161 | write(', %s: ' % rep) |
| 162 | self._format(ent, stream, indent + _len(rep) + 2, |
| 163 | allowance + 1, context, level) |
| 164 | indent = indent - self._indent_per_level |
| 165 | del context[objid] |
| 166 | write('}') |
| 167 | return |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 168 | |
Raymond Hettinger | c226c31 | 2008-01-23 00:04:40 +0000 | [diff] [blame] | 169 | if ((issubclass(typ, list) and r is list.__repr__) or |
| 170 | (issubclass(typ, tuple) and r is tuple.__repr__) or |
| 171 | (issubclass(typ, set) and r is set.__repr__) or |
| 172 | (issubclass(typ, frozenset) and r is frozenset.__repr__) |
| 173 | ): |
Raymond Hettinger | 5310b69 | 2008-01-24 21:47:56 +0000 | [diff] [blame] | 174 | length = _len(object) |
Georg Brandl | dcd6b52 | 2008-01-20 11:13:29 +0000 | [diff] [blame] | 175 | if issubclass(typ, list): |
| 176 | write('[') |
| 177 | endchar = ']' |
Raymond Hettinger | c226c31 | 2008-01-23 00:04:40 +0000 | [diff] [blame] | 178 | elif issubclass(typ, set): |
Raymond Hettinger | 5310b69 | 2008-01-24 21:47:56 +0000 | [diff] [blame] | 179 | if not length: |
| 180 | write('set()') |
| 181 | return |
Raymond Hettinger | c226c31 | 2008-01-23 00:04:40 +0000 | [diff] [blame] | 182 | write('set([') |
| 183 | endchar = '])' |
| 184 | object = sorted(object) |
| 185 | indent += 4 |
| 186 | elif issubclass(typ, frozenset): |
Raymond Hettinger | 5310b69 | 2008-01-24 21:47:56 +0000 | [diff] [blame] | 187 | if not length: |
| 188 | write('frozenset()') |
| 189 | return |
Raymond Hettinger | c226c31 | 2008-01-23 00:04:40 +0000 | [diff] [blame] | 190 | write('frozenset([') |
| 191 | endchar = '])' |
| 192 | object = sorted(object) |
Raymond Hettinger | 5310b69 | 2008-01-24 21:47:56 +0000 | [diff] [blame] | 193 | indent += 10 |
Georg Brandl | dcd6b52 | 2008-01-20 11:13:29 +0000 | [diff] [blame] | 194 | else: |
| 195 | write('(') |
| 196 | endchar = ')' |
Facundo Batista | 2da91c3 | 2008-06-21 17:43:56 +0000 | [diff] [blame] | 197 | if self._indent_per_level > 1 and sepLines: |
Georg Brandl | dcd6b52 | 2008-01-20 11:13:29 +0000 | [diff] [blame] | 198 | write((self._indent_per_level - 1) * ' ') |
Georg Brandl | dcd6b52 | 2008-01-20 11:13:29 +0000 | [diff] [blame] | 199 | if length: |
| 200 | context[objid] = 1 |
| 201 | indent = indent + self._indent_per_level |
| 202 | self._format(object[0], stream, indent, allowance + 1, |
| 203 | context, level) |
| 204 | if length > 1: |
| 205 | for ent in object[1:]: |
| 206 | if sepLines: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 207 | write(',\n' + ' '*indent) |
Georg Brandl | dcd6b52 | 2008-01-20 11:13:29 +0000 | [diff] [blame] | 208 | else: |
| 209 | write(', ') |
| 210 | self._format(ent, stream, indent, |
| 211 | allowance + 1, context, level) |
| 212 | indent = indent - self._indent_per_level |
| 213 | del context[objid] |
| 214 | if issubclass(typ, tuple) and length == 1: |
| 215 | write(',') |
| 216 | write(endchar) |
| 217 | return |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 218 | |
Georg Brandl | 23da6e6 | 2008-05-12 16:26:52 +0000 | [diff] [blame] | 219 | write(rep) |
Georg Brandl | dcd6b52 | 2008-01-20 11:13:29 +0000 | [diff] [blame] | 220 | |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 221 | def _repr(self, object, context, level): |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 222 | repr, readable, recursive = self.format(object, context.copy(), |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 223 | self._depth, level) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 224 | if not readable: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 225 | self._readable = False |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 226 | if recursive: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 227 | self._recursive = True |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 228 | return repr |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 229 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 230 | def format(self, object, context, maxlevels, level): |
| 231 | """Format object for a specific context, returning a string |
| 232 | and flags indicating whether the representation is 'readable' |
| 233 | and whether the object represents a recursive construct. |
| 234 | """ |
| 235 | return _safe_repr(object, context, maxlevels, level) |
| 236 | |
| 237 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 238 | # Return triple (repr_string, isreadable, isrecursive). |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 239 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 240 | def _safe_repr(object, context, maxlevels, level): |
| 241 | typ = _type(object) |
Martin v. Löwis | d02879d | 2003-06-07 20:47:37 +0000 | [diff] [blame] | 242 | if typ is str: |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 243 | if 'locale' not in _sys.modules: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 244 | return repr(object), True, False |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 245 | if "'" in object and '"' not in object: |
| 246 | closure = '"' |
| 247 | quotes = {'"': '\\"'} |
| 248 | else: |
| 249 | closure = "'" |
| 250 | quotes = {"'": "\\'"} |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 251 | qget = quotes.get |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 252 | sio = _StringIO() |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 253 | write = sio.write |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 254 | for char in object: |
| 255 | if char.isalpha(): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 256 | write(char) |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 257 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 258 | write(qget(char, repr(char)[1:-1])) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 259 | return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 260 | |
Walter Dörwald | 1b626ca | 2004-11-15 13:51:41 +0000 | [diff] [blame] | 261 | r = getattr(typ, "__repr__", None) |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 262 | if issubclass(typ, dict) and r is dict.__repr__: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 263 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 264 | return "{}", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 265 | objid = _id(object) |
Georg Brandl | 23da6e6 | 2008-05-12 16:26:52 +0000 | [diff] [blame] | 266 | if maxlevels and level >= maxlevels: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 267 | return "{...}", False, objid in context |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 268 | if objid in context: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 269 | return _recursion(object), False, True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 270 | context[objid] = 1 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 271 | readable = True |
| 272 | recursive = False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 273 | components = [] |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 274 | append = components.append |
| 275 | level += 1 |
| 276 | saferepr = _safe_repr |
Tim Peters | d609b1a | 2006-06-02 23:22:51 +0000 | [diff] [blame] | 277 | for k, v in sorted(object.items()): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 278 | krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) |
| 279 | vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) |
| 280 | append("%s: %s" % (krepr, vrepr)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 281 | readable = readable and kreadable and vreadable |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 282 | if krecur or vrecur: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 283 | recursive = True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 284 | del context[objid] |
| 285 | return "{%s}" % _commajoin(components), readable, recursive |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 286 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 287 | if (issubclass(typ, list) and r is list.__repr__) or \ |
| 288 | (issubclass(typ, tuple) and r is tuple.__repr__): |
| 289 | if issubclass(typ, list): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 290 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 291 | return "[]", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 292 | format = "[%s]" |
| 293 | elif _len(object) == 1: |
| 294 | format = "(%s,)" |
| 295 | else: |
| 296 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 297 | return "()", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 298 | format = "(%s)" |
| 299 | objid = _id(object) |
Georg Brandl | 23da6e6 | 2008-05-12 16:26:52 +0000 | [diff] [blame] | 300 | if maxlevels and level >= maxlevels: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 301 | return format % "...", False, objid in context |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 302 | if objid in context: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 303 | return _recursion(object), False, True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 304 | context[objid] = 1 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 305 | readable = True |
| 306 | recursive = False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 307 | components = [] |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 308 | append = components.append |
| 309 | level += 1 |
| 310 | for o in object: |
| 311 | orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) |
| 312 | append(orepr) |
| 313 | if not oreadable: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 314 | readable = False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 315 | if orecur: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 316 | recursive = True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 317 | del context[objid] |
| 318 | return format % _commajoin(components), readable, recursive |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 319 | |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 320 | rep = repr(object) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 321 | return rep, (rep and not rep.startswith('<')), False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 322 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 323 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 324 | def _recursion(object): |
| 325 | return ("<Recursion on %s with id=%s>" |
| 326 | % (_type(object).__name__, _id(object))) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 327 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 328 | |
| 329 | def _perfcheck(object=None): |
| 330 | import time |
| 331 | if object is None: |
| 332 | object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000 |
| 333 | p = PrettyPrinter() |
| 334 | t1 = time.time() |
| 335 | _safe_repr(object, {}, None, 0) |
| 336 | t2 = time.time() |
| 337 | p.pformat(object) |
| 338 | t3 = time.time() |
| 339 | print "_safe_repr:", t2 - t1 |
| 340 | print "pformat:", t3 - t2 |
| 341 | |
| 342 | if __name__ == "__main__": |
| 343 | _perfcheck() |