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 |
Raymond Hettinger | bad3c88 | 2010-09-09 12:31:00 +0000 | [diff] [blame] | 38 | from collections import OrderedDict as _OrderedDict |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 39 | from io 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 | |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 73 | class _safe_key: |
| 74 | """Helper function for key functions when sorting unorderable objects. |
| 75 | |
| 76 | The wrapped-object will fallback to an Py2.x style comparison for |
| 77 | unorderable types (sorting first comparing the type name and then by |
| 78 | the obj ids). Does not work recursively, so dict.items() must have |
| 79 | _safe_key applied to both the key and the value. |
| 80 | |
| 81 | """ |
| 82 | |
| 83 | __slots__ = ['obj'] |
| 84 | |
| 85 | def __init__(self, obj): |
| 86 | self.obj = obj |
| 87 | |
| 88 | def __lt__(self, other): |
Florent Xicluna | d6da90f | 2012-07-21 11:17:38 +0200 | [diff] [blame] | 89 | try: |
| 90 | rv = self.obj.__lt__(other.obj) |
| 91 | except TypeError: |
| 92 | rv = NotImplemented |
| 93 | |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 94 | if rv is NotImplemented: |
| 95 | rv = (str(type(self.obj)), id(self.obj)) < \ |
| 96 | (str(type(other.obj)), id(other.obj)) |
| 97 | return rv |
| 98 | |
| 99 | def _safe_tuple(t): |
| 100 | "Helper function for comparing 2-tuples" |
| 101 | return _safe_key(t[0]), _safe_key(t[1]) |
| 102 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 103 | class PrettyPrinter: |
| 104 | def __init__(self, indent=1, width=80, depth=None, stream=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 105 | """Handle pretty printing operations onto a stream using a set of |
| 106 | configured parameters. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 108 | indent |
| 109 | Number of spaces to indent for each level of nesting. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 111 | width |
| 112 | Attempted maximum number of columns in the output. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 114 | depth |
| 115 | The maximum depth to print out nested structures. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 117 | stream |
| 118 | The desired output stream. If omitted (or false), the standard |
| 119 | output stream available at construction will be used. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 121 | """ |
| 122 | indent = int(indent) |
| 123 | width = int(width) |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 124 | assert indent >= 0, "indent must be >= 0" |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 125 | assert depth is None or depth > 0, "depth must be > 0" |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 126 | assert width, "width must be != 0" |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 127 | self._depth = depth |
| 128 | self._indent_per_level = indent |
| 129 | self._width = width |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 130 | if stream is not None: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 131 | self._stream = stream |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 132 | else: |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 133 | self._stream = _sys.stdout |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 134 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 135 | def pprint(self, object): |
Walter Dörwald | e62e936 | 2005-11-11 18:18:51 +0000 | [diff] [blame] | 136 | self._format(object, self._stream, 0, 0, {}, 0) |
| 137 | self._stream.write("\n") |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 138 | |
| 139 | def pformat(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 140 | sio = _StringIO() |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 141 | self._format(object, sio, 0, 0, {}, 0) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 142 | return sio.getvalue() |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 143 | |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 144 | def isrecursive(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 145 | return self.format(object, {}, 0, 0)[2] |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 146 | |
| 147 | def isreadable(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 148 | s, readable, recursive = self.format(object, {}, 0, 0) |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 149 | return readable and not recursive |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 150 | |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 151 | def _format(self, object, stream, indent, allowance, context, level): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 152 | level = level + 1 |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 153 | objid = _id(object) |
| 154 | if objid in context: |
| 155 | stream.write(_recursion(object)) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 156 | self._recursive = True |
| 157 | self._readable = False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 158 | return |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 159 | rep = self._repr(object, context, level - 1) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 160 | typ = _type(object) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 161 | sepLines = _len(rep) > (self._width - 1 - indent - allowance) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 162 | write = stream.write |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 163 | |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 164 | if self._depth and level > self._depth: |
| 165 | write(rep) |
| 166 | return |
| 167 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 168 | if sepLines: |
Walter Dörwald | 1b626ca | 2004-11-15 13:51:41 +0000 | [diff] [blame] | 169 | r = getattr(typ, "__repr__", None) |
Raymond Hettinger | bad3c88 | 2010-09-09 12:31:00 +0000 | [diff] [blame] | 170 | if issubclass(typ, dict): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 171 | write('{') |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 172 | if self._indent_per_level > 1: |
| 173 | write((self._indent_per_level - 1) * ' ') |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 174 | length = _len(object) |
| 175 | if length: |
| 176 | context[objid] = 1 |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 177 | indent = indent + self._indent_per_level |
Raymond Hettinger | bad3c88 | 2010-09-09 12:31:00 +0000 | [diff] [blame] | 178 | if issubclass(typ, _OrderedDict): |
| 179 | items = list(object.items()) |
| 180 | else: |
| 181 | items = sorted(object.items(), key=_safe_tuple) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 182 | key, ent = items[0] |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 183 | rep = self._repr(key, context, level) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 184 | write(rep) |
| 185 | write(': ') |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 186 | self._format(ent, stream, indent + _len(rep) + 2, |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 187 | allowance + 1, context, level) |
| 188 | if length > 1: |
| 189 | for key, ent in items[1:]: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 190 | rep = self._repr(key, context, level) |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 191 | write(',\n%s%s: ' % (' '*indent, rep)) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 192 | self._format(ent, stream, indent + _len(rep) + 2, |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 193 | allowance + 1, context, level) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 194 | indent = indent - self._indent_per_level |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 195 | del context[objid] |
| 196 | write('}') |
| 197 | return |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 198 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 199 | if ((issubclass(typ, list) and r is list.__repr__) or |
| 200 | (issubclass(typ, tuple) and r is tuple.__repr__) or |
| 201 | (issubclass(typ, set) and r is set.__repr__) or |
| 202 | (issubclass(typ, frozenset) and r is frozenset.__repr__) |
| 203 | ): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 204 | length = _len(object) |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 205 | if issubclass(typ, list): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 206 | write('[') |
| 207 | endchar = ']' |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 208 | elif issubclass(typ, set): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 209 | if not length: |
| 210 | write('set()') |
| 211 | return |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 212 | write('{') |
| 213 | endchar = '}' |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 214 | object = sorted(object, key=_safe_key) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 215 | elif issubclass(typ, frozenset): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 216 | if not length: |
| 217 | write('frozenset()') |
| 218 | return |
Raymond Hettinger | 4b8db41 | 2008-01-31 01:10:03 +0000 | [diff] [blame] | 219 | write('frozenset({') |
| 220 | endchar = '})' |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 221 | object = sorted(object, key=_safe_key) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 222 | indent += 10 |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 223 | else: |
| 224 | write('(') |
| 225 | endchar = ')' |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 226 | if self._indent_per_level > 1: |
| 227 | write((self._indent_per_level - 1) * ' ') |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 228 | if length: |
| 229 | context[objid] = 1 |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 230 | indent = indent + self._indent_per_level |
| 231 | self._format(object[0], stream, indent, allowance + 1, |
| 232 | context, level) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 233 | if length > 1: |
| 234 | for ent in object[1:]: |
| 235 | write(',\n' + ' '*indent) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 236 | self._format(ent, stream, indent, |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 237 | allowance + 1, context, level) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 238 | indent = indent - self._indent_per_level |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 239 | del context[objid] |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 240 | if issubclass(typ, tuple) and length == 1: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 241 | write(',') |
| 242 | write(endchar) |
| 243 | return |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 244 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 245 | write(rep) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 246 | |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 247 | def _repr(self, object, context, level): |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 248 | repr, readable, recursive = self.format(object, context.copy(), |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 249 | self._depth, level) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 250 | if not readable: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 251 | self._readable = False |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 252 | if recursive: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 253 | self._recursive = True |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 254 | return repr |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 255 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 256 | def format(self, object, context, maxlevels, level): |
| 257 | """Format object for a specific context, returning a string |
| 258 | and flags indicating whether the representation is 'readable' |
| 259 | and whether the object represents a recursive construct. |
| 260 | """ |
| 261 | return _safe_repr(object, context, maxlevels, level) |
| 262 | |
| 263 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 264 | # Return triple (repr_string, isreadable, isrecursive). |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 265 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 266 | def _safe_repr(object, context, maxlevels, level): |
| 267 | typ = _type(object) |
Martin v. Löwis | d02879d | 2003-06-07 20:47:37 +0000 | [diff] [blame] | 268 | if typ is str: |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 269 | if 'locale' not in _sys.modules: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 270 | return repr(object), True, False |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 271 | if "'" in object and '"' not in object: |
| 272 | closure = '"' |
| 273 | quotes = {'"': '\\"'} |
| 274 | else: |
| 275 | closure = "'" |
| 276 | quotes = {"'": "\\'"} |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 277 | qget = quotes.get |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 278 | sio = _StringIO() |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 279 | write = sio.write |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 280 | for char in object: |
| 281 | if char.isalpha(): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 282 | write(char) |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 283 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 284 | write(qget(char, repr(char)[1:-1])) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 285 | return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 286 | |
Walter Dörwald | 1b626ca | 2004-11-15 13:51:41 +0000 | [diff] [blame] | 287 | r = getattr(typ, "__repr__", None) |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 288 | if issubclass(typ, dict) and r is dict.__repr__: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 289 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 290 | return "{}", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 291 | objid = _id(object) |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 292 | if maxlevels and level >= maxlevels: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 293 | return "{...}", False, objid in context |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 294 | if objid in context: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 295 | return _recursion(object), False, True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 296 | context[objid] = 1 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 297 | readable = True |
| 298 | recursive = False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 299 | components = [] |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 300 | append = components.append |
| 301 | level += 1 |
| 302 | saferepr = _safe_repr |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 303 | items = sorted(object.items(), key=_safe_tuple) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 304 | for k, v in items: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 305 | krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) |
| 306 | vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) |
| 307 | append("%s: %s" % (krepr, vrepr)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 308 | readable = readable and kreadable and vreadable |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 309 | if krecur or vrecur: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 310 | recursive = True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 311 | del context[objid] |
| 312 | return "{%s}" % _commajoin(components), readable, recursive |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 313 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 314 | if (issubclass(typ, list) and r is list.__repr__) or \ |
| 315 | (issubclass(typ, tuple) and r is tuple.__repr__): |
| 316 | if issubclass(typ, list): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 317 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 318 | return "[]", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 319 | format = "[%s]" |
| 320 | elif _len(object) == 1: |
| 321 | format = "(%s,)" |
| 322 | else: |
| 323 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 324 | return "()", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 325 | format = "(%s)" |
| 326 | objid = _id(object) |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 327 | if maxlevels and level >= maxlevels: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 328 | return format % "...", False, objid in context |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 329 | if objid in context: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 330 | return _recursion(object), False, True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 331 | context[objid] = 1 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 332 | readable = True |
| 333 | recursive = False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 334 | components = [] |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 335 | append = components.append |
| 336 | level += 1 |
| 337 | for o in object: |
| 338 | orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) |
| 339 | append(orepr) |
| 340 | if not oreadable: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 341 | readable = False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 342 | if orecur: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 343 | recursive = True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 344 | del context[objid] |
| 345 | return format % _commajoin(components), readable, recursive |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 346 | |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 347 | rep = repr(object) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 348 | return rep, (rep and not rep.startswith('<')), False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 349 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 350 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 351 | def _recursion(object): |
| 352 | return ("<Recursion on %s with id=%s>" |
| 353 | % (_type(object).__name__, _id(object))) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 354 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 355 | |
| 356 | def _perfcheck(object=None): |
| 357 | import time |
| 358 | if object is None: |
| 359 | object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000 |
| 360 | p = PrettyPrinter() |
| 361 | t1 = time.time() |
| 362 | _safe_repr(object, {}, None, 0) |
| 363 | t2 = time.time() |
| 364 | p.pformat(object) |
| 365 | t3 = time.time() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 366 | print("_safe_repr:", t2 - t1) |
| 367 | print("pformat:", t3 - t2) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 368 | |
| 369 | if __name__ == "__main__": |
| 370 | _perfcheck() |