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 | |
Serhiy Storchaka | aa4c36f | 2015-03-26 08:51:33 +0200 | [diff] [blame] | 37 | import collections as _collections |
Antoine Pitrou | 64c16c3 | 2013-03-23 20:30:39 +0100 | [diff] [blame] | 38 | import re |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 39 | import sys as _sys |
Serhiy Storchaka | 87eb482 | 2015-03-24 19:31:50 +0200 | [diff] [blame] | 40 | import types as _types |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 41 | from io import StringIO as _StringIO |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 42 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 43 | __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", |
| 44 | "PrettyPrinter"] |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 45 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 46 | |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 47 | def pprint(object, stream=None, indent=1, width=80, depth=None, *, |
| 48 | compact=False): |
Skip Montanaro | 2dc0c13 | 2004-05-14 16:31:56 +0000 | [diff] [blame] | 49 | """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] | 50 | printer = PrettyPrinter( |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 51 | stream=stream, indent=indent, width=width, depth=depth, |
| 52 | compact=compact) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 53 | printer.pprint(object) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 54 | |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 55 | def pformat(object, indent=1, width=80, depth=None, *, compact=False): |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 56 | """Format a Python object into a pretty-printed representation.""" |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 57 | return PrettyPrinter(indent=indent, width=width, depth=depth, |
| 58 | compact=compact).pformat(object) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 59 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 60 | def saferepr(object): |
| 61 | """Version of repr() which can handle recursive data structures.""" |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 62 | return _safe_repr(object, {}, None, 0)[0] |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 63 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 64 | def isreadable(object): |
| 65 | """Determine if saferepr(object) is readable by eval().""" |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 66 | return _safe_repr(object, {}, None, 0)[1] |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 67 | |
| 68 | def isrecursive(object): |
| 69 | """Determine if object requires a recursive representation.""" |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 70 | return _safe_repr(object, {}, None, 0)[2] |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 71 | |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 72 | class _safe_key: |
| 73 | """Helper function for key functions when sorting unorderable objects. |
| 74 | |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 75 | The wrapped-object will fallback to a Py2.x style comparison for |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 76 | unorderable types (sorting first comparing the type name and then by |
| 77 | the obj ids). Does not work recursively, so dict.items() must have |
| 78 | _safe_key applied to both the key and the value. |
| 79 | |
| 80 | """ |
| 81 | |
| 82 | __slots__ = ['obj'] |
| 83 | |
| 84 | def __init__(self, obj): |
| 85 | self.obj = obj |
| 86 | |
| 87 | def __lt__(self, other): |
Florent Xicluna | d6da90f | 2012-07-21 11:17:38 +0200 | [diff] [blame] | 88 | try: |
Serhiy Storchaka | 62aa7dc | 2015-04-06 22:52:44 +0300 | [diff] [blame] | 89 | return self.obj < other.obj |
Florent Xicluna | d6da90f | 2012-07-21 11:17:38 +0200 | [diff] [blame] | 90 | except TypeError: |
Serhiy Storchaka | 62aa7dc | 2015-04-06 22:52:44 +0300 | [diff] [blame] | 91 | return ((str(type(self.obj)), id(self.obj)) < \ |
| 92 | (str(type(other.obj)), id(other.obj))) |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 93 | |
| 94 | def _safe_tuple(t): |
| 95 | "Helper function for comparing 2-tuples" |
| 96 | return _safe_key(t[0]), _safe_key(t[1]) |
| 97 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 98 | class PrettyPrinter: |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 99 | def __init__(self, indent=1, width=80, depth=None, stream=None, *, |
| 100 | compact=False): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 101 | """Handle pretty printing operations onto a stream using a set of |
| 102 | configured parameters. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 104 | indent |
| 105 | Number of spaces to indent for each level of nesting. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | width |
| 108 | Attempted maximum number of columns in the output. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 110 | depth |
| 111 | The maximum depth to print out nested structures. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 113 | stream |
| 114 | The desired output stream. If omitted (or false), the standard |
| 115 | output stream available at construction will be used. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 116 | |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 117 | compact |
| 118 | If true, several items will be combined in one line. |
| 119 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 120 | """ |
| 121 | indent = int(indent) |
| 122 | width = int(width) |
Serhiy Storchaka | f3fa308 | 2015-03-26 08:43:21 +0200 | [diff] [blame] | 123 | if indent < 0: |
| 124 | raise ValueError('indent must be >= 0') |
| 125 | if depth is not None and depth <= 0: |
| 126 | raise ValueError('depth must be > 0') |
| 127 | if not width: |
| 128 | raise ValueError('width must be != 0') |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 129 | self._depth = depth |
| 130 | self._indent_per_level = indent |
| 131 | self._width = width |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 132 | if stream is not None: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 133 | self._stream = stream |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 134 | else: |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 135 | self._stream = _sys.stdout |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 136 | self._compact = bool(compact) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 137 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 138 | def pprint(self, object): |
Walter Dörwald | e62e936 | 2005-11-11 18:18:51 +0000 | [diff] [blame] | 139 | self._format(object, self._stream, 0, 0, {}, 0) |
| 140 | self._stream.write("\n") |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 141 | |
| 142 | def pformat(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 143 | sio = _StringIO() |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 144 | self._format(object, sio, 0, 0, {}, 0) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 145 | return sio.getvalue() |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 146 | |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 147 | def isrecursive(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 148 | return self.format(object, {}, 0, 0)[2] |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 149 | |
| 150 | def isreadable(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 151 | s, readable, recursive = self.format(object, {}, 0, 0) |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 152 | return readable and not recursive |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 153 | |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 154 | def _format(self, object, stream, indent, allowance, context, level): |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 155 | objid = id(object) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 156 | if objid in context: |
| 157 | stream.write(_recursion(object)) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 158 | self._recursive = True |
| 159 | self._readable = False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 160 | return |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 161 | rep = self._repr(object, context, level) |
Serhiy Storchaka | a750ce3 | 2015-02-14 10:55:19 +0200 | [diff] [blame] | 162 | max_width = self._width - indent - allowance |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 163 | if len(rep) > max_width: |
| 164 | p = self._dispatch.get(type(object).__repr__, None) |
| 165 | if p is not None: |
| 166 | context[objid] = 1 |
| 167 | p(self, object, stream, indent, allowance, context, level + 1) |
| 168 | del context[objid] |
| 169 | return |
| 170 | elif isinstance(object, dict): |
| 171 | context[objid] = 1 |
| 172 | self._pprint_dict(object, stream, indent, allowance, |
| 173 | context, level + 1) |
| 174 | del context[objid] |
| 175 | return |
| 176 | stream.write(rep) |
| 177 | |
| 178 | _dispatch = {} |
| 179 | |
| 180 | def _pprint_dict(self, object, stream, indent, allowance, context, level): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 181 | write = stream.write |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 182 | write('{') |
| 183 | if self._indent_per_level > 1: |
| 184 | write((self._indent_per_level - 1) * ' ') |
| 185 | length = len(object) |
| 186 | if length: |
Serhiy Storchaka | aa4c36f | 2015-03-26 08:51:33 +0200 | [diff] [blame] | 187 | items = sorted(object.items(), key=_safe_tuple) |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 188 | self._format_dict_items(items, stream, indent, allowance + 1, |
| 189 | context, level) |
| 190 | write('}') |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 191 | |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 192 | _dispatch[dict.__repr__] = _pprint_dict |
Serhiy Storchaka | aa4c36f | 2015-03-26 08:51:33 +0200 | [diff] [blame] | 193 | |
| 194 | def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level): |
| 195 | if not len(object): |
| 196 | stream.write(repr(object)) |
| 197 | return |
| 198 | cls = object.__class__ |
| 199 | stream.write(cls.__name__ + '(') |
| 200 | self._format(list(object.items()), stream, |
| 201 | indent + len(cls.__name__) + 1, allowance + 1, |
| 202 | context, level) |
| 203 | stream.write(')') |
| 204 | |
| 205 | _dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 206 | |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 207 | def _pprint_list(self, object, stream, indent, allowance, context, level): |
| 208 | stream.write('[') |
| 209 | self._format_items(object, stream, indent, allowance + 1, |
| 210 | context, level) |
| 211 | stream.write(']') |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 212 | |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 213 | _dispatch[list.__repr__] = _pprint_list |
| 214 | |
| 215 | def _pprint_tuple(self, object, stream, indent, allowance, context, level): |
| 216 | stream.write('(') |
| 217 | endchar = ',)' if len(object) == 1 else ')' |
| 218 | self._format_items(object, stream, indent, allowance + len(endchar), |
| 219 | context, level) |
| 220 | stream.write(endchar) |
| 221 | |
| 222 | _dispatch[tuple.__repr__] = _pprint_tuple |
| 223 | |
| 224 | def _pprint_set(self, object, stream, indent, allowance, context, level): |
| 225 | if not len(object): |
| 226 | stream.write(repr(object)) |
| 227 | return |
| 228 | typ = object.__class__ |
| 229 | if typ is set: |
| 230 | stream.write('{') |
| 231 | endchar = '}' |
| 232 | else: |
| 233 | stream.write(typ.__name__ + '({') |
| 234 | endchar = '})' |
| 235 | indent += len(typ.__name__) + 1 |
| 236 | object = sorted(object, key=_safe_key) |
| 237 | self._format_items(object, stream, indent, allowance + len(endchar), |
| 238 | context, level) |
| 239 | stream.write(endchar) |
| 240 | |
| 241 | _dispatch[set.__repr__] = _pprint_set |
| 242 | _dispatch[frozenset.__repr__] = _pprint_set |
| 243 | |
| 244 | def _pprint_str(self, object, stream, indent, allowance, context, level): |
| 245 | write = stream.write |
| 246 | if not len(object): |
| 247 | write(repr(object)) |
| 248 | return |
| 249 | chunks = [] |
| 250 | lines = object.splitlines(True) |
| 251 | if level == 1: |
| 252 | indent += 1 |
| 253 | allowance += 1 |
| 254 | max_width1 = max_width = self._width - indent |
| 255 | for i, line in enumerate(lines): |
| 256 | rep = repr(line) |
| 257 | if i == len(lines) - 1: |
| 258 | max_width1 -= allowance |
| 259 | if len(rep) <= max_width1: |
| 260 | chunks.append(rep) |
| 261 | else: |
| 262 | # A list of alternating (non-space, space) strings |
| 263 | parts = re.findall(r'\S*\s*', line) |
| 264 | assert parts |
| 265 | assert not parts[-1] |
| 266 | parts.pop() # drop empty last part |
| 267 | max_width2 = max_width |
| 268 | current = '' |
| 269 | for j, part in enumerate(parts): |
| 270 | candidate = current + part |
| 271 | if j == len(parts) - 1 and i == len(lines) - 1: |
| 272 | max_width2 -= allowance |
| 273 | if len(repr(candidate)) > max_width2: |
Serhiy Storchaka | fe3dc37 | 2014-12-20 20:57:15 +0200 | [diff] [blame] | 274 | if current: |
| 275 | chunks.append(repr(current)) |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 276 | current = part |
| 277 | else: |
| 278 | current = candidate |
| 279 | if current: |
| 280 | chunks.append(repr(current)) |
| 281 | if len(chunks) == 1: |
| 282 | write(rep) |
| 283 | return |
| 284 | if level == 1: |
| 285 | write('(') |
| 286 | for i, rep in enumerate(chunks): |
| 287 | if i > 0: |
| 288 | write('\n' + ' '*indent) |
| 289 | write(rep) |
| 290 | if level == 1: |
| 291 | write(')') |
| 292 | |
| 293 | _dispatch[str.__repr__] = _pprint_str |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 294 | |
Serhiy Storchaka | 022f203 | 2015-03-24 19:22:37 +0200 | [diff] [blame] | 295 | def _pprint_bytes(self, object, stream, indent, allowance, context, level): |
| 296 | write = stream.write |
| 297 | if len(object) <= 4: |
| 298 | write(repr(object)) |
| 299 | return |
| 300 | parens = level == 1 |
| 301 | if parens: |
| 302 | indent += 1 |
| 303 | allowance += 1 |
| 304 | write('(') |
| 305 | delim = '' |
| 306 | for rep in _wrap_bytes_repr(object, self._width - indent, allowance): |
| 307 | write(delim) |
| 308 | write(rep) |
| 309 | if not delim: |
| 310 | delim = '\n' + ' '*indent |
| 311 | if parens: |
| 312 | write(')') |
| 313 | |
| 314 | _dispatch[bytes.__repr__] = _pprint_bytes |
| 315 | |
| 316 | def _pprint_bytearray(self, object, stream, indent, allowance, context, level): |
| 317 | write = stream.write |
| 318 | write('bytearray(') |
| 319 | self._pprint_bytes(bytes(object), stream, indent + 10, |
| 320 | allowance + 1, context, level + 1) |
| 321 | write(')') |
| 322 | |
| 323 | _dispatch[bytearray.__repr__] = _pprint_bytearray |
| 324 | |
Serhiy Storchaka | 87eb482 | 2015-03-24 19:31:50 +0200 | [diff] [blame] | 325 | def _pprint_mappingproxy(self, object, stream, indent, allowance, context, level): |
| 326 | stream.write('mappingproxy(') |
| 327 | self._format(object.copy(), stream, indent + 13, allowance + 1, |
| 328 | context, level) |
| 329 | stream.write(')') |
| 330 | |
| 331 | _dispatch[_types.MappingProxyType.__repr__] = _pprint_mappingproxy |
| 332 | |
Serhiy Storchaka | a750ce3 | 2015-02-14 10:55:19 +0200 | [diff] [blame] | 333 | def _format_dict_items(self, items, stream, indent, allowance, context, |
| 334 | level): |
| 335 | write = stream.write |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 336 | indent += self._indent_per_level |
Serhiy Storchaka | a750ce3 | 2015-02-14 10:55:19 +0200 | [diff] [blame] | 337 | delimnl = ',\n' + ' ' * indent |
| 338 | last_index = len(items) - 1 |
| 339 | for i, (key, ent) in enumerate(items): |
| 340 | last = i == last_index |
| 341 | rep = self._repr(key, context, level) |
| 342 | write(rep) |
| 343 | write(': ') |
| 344 | self._format(ent, stream, indent + len(rep) + 2, |
| 345 | allowance if last else 1, |
| 346 | context, level) |
| 347 | if not last: |
| 348 | write(delimnl) |
| 349 | |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 350 | def _format_items(self, items, stream, indent, allowance, context, level): |
| 351 | write = stream.write |
Serhiy Storchaka | 8e2aa88 | 2015-03-24 18:45:23 +0200 | [diff] [blame] | 352 | indent += self._indent_per_level |
| 353 | if self._indent_per_level > 1: |
| 354 | write((self._indent_per_level - 1) * ' ') |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 355 | delimnl = ',\n' + ' ' * indent |
| 356 | delim = '' |
Serhiy Storchaka | a750ce3 | 2015-02-14 10:55:19 +0200 | [diff] [blame] | 357 | width = max_width = self._width - indent + 1 |
| 358 | it = iter(items) |
| 359 | try: |
| 360 | next_ent = next(it) |
| 361 | except StopIteration: |
| 362 | return |
| 363 | last = False |
| 364 | while not last: |
| 365 | ent = next_ent |
| 366 | try: |
| 367 | next_ent = next(it) |
| 368 | except StopIteration: |
| 369 | last = True |
| 370 | max_width -= allowance |
| 371 | width -= allowance |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 372 | if self._compact: |
| 373 | rep = self._repr(ent, context, level) |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 374 | w = len(rep) + 2 |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 375 | if width < w: |
| 376 | width = max_width |
| 377 | if delim: |
| 378 | delim = delimnl |
| 379 | if width >= w: |
| 380 | width -= w |
| 381 | write(delim) |
| 382 | delim = ', ' |
| 383 | write(rep) |
| 384 | continue |
| 385 | write(delim) |
| 386 | delim = delimnl |
Serhiy Storchaka | a750ce3 | 2015-02-14 10:55:19 +0200 | [diff] [blame] | 387 | self._format(ent, stream, indent, |
| 388 | allowance if last else 1, |
| 389 | context, level) |
Serhiy Storchaka | 7c411a4 | 2013-10-02 11:56:18 +0300 | [diff] [blame] | 390 | |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 391 | def _repr(self, object, context, level): |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 392 | repr, readable, recursive = self.format(object, context.copy(), |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 393 | self._depth, level) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 394 | if not readable: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 395 | self._readable = False |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 396 | if recursive: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 397 | self._recursive = True |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 398 | return repr |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 399 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 400 | def format(self, object, context, maxlevels, level): |
| 401 | """Format object for a specific context, returning a string |
| 402 | and flags indicating whether the representation is 'readable' |
| 403 | and whether the object represents a recursive construct. |
| 404 | """ |
| 405 | return _safe_repr(object, context, maxlevels, level) |
| 406 | |
Serhiy Storchaka | bedbf96 | 2015-05-12 13:35:48 +0300 | [diff] [blame] | 407 | def _pprint_default_dict(self, object, stream, indent, allowance, context, level): |
| 408 | if not len(object): |
| 409 | stream.write(repr(object)) |
| 410 | return |
| 411 | rdf = self._repr(object.default_factory, context, level) |
| 412 | cls = object.__class__ |
| 413 | indent += len(cls.__name__) + 1 |
| 414 | stream.write('%s(%s,\n%s' % (cls.__name__, rdf, ' ' * indent)) |
| 415 | self._pprint_dict(object, stream, indent, allowance + 1, context, level) |
| 416 | stream.write(')') |
| 417 | |
| 418 | _dispatch[_collections.defaultdict.__repr__] = _pprint_default_dict |
| 419 | |
| 420 | def _pprint_counter(self, object, stream, indent, allowance, context, level): |
| 421 | if not len(object): |
| 422 | stream.write(repr(object)) |
| 423 | return |
| 424 | cls = object.__class__ |
| 425 | stream.write(cls.__name__ + '({') |
| 426 | if self._indent_per_level > 1: |
| 427 | stream.write((self._indent_per_level - 1) * ' ') |
| 428 | items = object.most_common() |
| 429 | self._format_dict_items(items, stream, |
| 430 | indent + len(cls.__name__) + 1, allowance + 2, |
| 431 | context, level) |
| 432 | stream.write('})') |
| 433 | |
| 434 | _dispatch[_collections.Counter.__repr__] = _pprint_counter |
| 435 | |
| 436 | def _pprint_chain_map(self, object, stream, indent, allowance, context, level): |
| 437 | if not len(object.maps): |
| 438 | stream.write(repr(object)) |
| 439 | return |
| 440 | cls = object.__class__ |
| 441 | stream.write(cls.__name__ + '(') |
| 442 | indent += len(cls.__name__) + 1 |
| 443 | for i, m in enumerate(object.maps): |
| 444 | if i == len(object.maps) - 1: |
| 445 | self._format(m, stream, indent, allowance + 1, context, level) |
| 446 | stream.write(')') |
| 447 | else: |
| 448 | self._format(m, stream, indent, 1, context, level) |
| 449 | stream.write(',\n' + ' ' * indent) |
| 450 | |
| 451 | _dispatch[_collections.ChainMap.__repr__] = _pprint_chain_map |
| 452 | |
| 453 | def _pprint_deque(self, object, stream, indent, allowance, context, level): |
| 454 | if not len(object): |
| 455 | stream.write(repr(object)) |
| 456 | return |
| 457 | cls = object.__class__ |
| 458 | stream.write(cls.__name__ + '(') |
| 459 | indent += len(cls.__name__) + 1 |
| 460 | stream.write('[') |
| 461 | if object.maxlen is None: |
| 462 | self._format_items(object, stream, indent, allowance + 2, |
| 463 | context, level) |
| 464 | stream.write('])') |
| 465 | else: |
| 466 | self._format_items(object, stream, indent, 2, |
| 467 | context, level) |
| 468 | rml = self._repr(object.maxlen, context, level) |
| 469 | stream.write('],\n%smaxlen=%s)' % (' ' * indent, rml)) |
| 470 | |
| 471 | _dispatch[_collections.deque.__repr__] = _pprint_deque |
| 472 | |
| 473 | def _pprint_user_dict(self, object, stream, indent, allowance, context, level): |
| 474 | self._format(object.data, stream, indent, allowance, context, level - 1) |
| 475 | |
| 476 | _dispatch[_collections.UserDict.__repr__] = _pprint_user_dict |
| 477 | |
| 478 | def _pprint_user_list(self, object, stream, indent, allowance, context, level): |
| 479 | self._format(object.data, stream, indent, allowance, context, level - 1) |
| 480 | |
| 481 | _dispatch[_collections.UserList.__repr__] = _pprint_user_list |
| 482 | |
| 483 | def _pprint_user_string(self, object, stream, indent, allowance, context, level): |
| 484 | self._format(object.data, stream, indent, allowance, context, level - 1) |
| 485 | |
| 486 | _dispatch[_collections.UserString.__repr__] = _pprint_user_string |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 487 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 488 | # Return triple (repr_string, isreadable, isrecursive). |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 489 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 490 | def _safe_repr(object, context, maxlevels, level): |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 491 | typ = type(object) |
Serhiy Storchaka | 8eb1f07 | 2015-05-16 21:38:05 +0300 | [diff] [blame] | 492 | if typ in _builtin_scalars: |
| 493 | return repr(object), True, False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 494 | |
Walter Dörwald | 1b626ca | 2004-11-15 13:51:41 +0000 | [diff] [blame] | 495 | r = getattr(typ, "__repr__", None) |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 496 | if issubclass(typ, dict) and r is dict.__repr__: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 497 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 498 | return "{}", True, False |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 499 | objid = id(object) |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 500 | if maxlevels and level >= maxlevels: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 501 | return "{...}", False, objid in context |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 502 | if objid in context: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 503 | return _recursion(object), False, True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 504 | context[objid] = 1 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 505 | readable = True |
| 506 | recursive = False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 507 | components = [] |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 508 | append = components.append |
| 509 | level += 1 |
| 510 | saferepr = _safe_repr |
Raymond Hettinger | a7da166 | 2009-11-19 01:07:05 +0000 | [diff] [blame] | 511 | items = sorted(object.items(), key=_safe_tuple) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 512 | for k, v in items: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 513 | krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) |
| 514 | vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) |
| 515 | append("%s: %s" % (krepr, vrepr)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 516 | readable = readable and kreadable and vreadable |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 517 | if krecur or vrecur: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 518 | recursive = True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 519 | del context[objid] |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 520 | return "{%s}" % ", ".join(components), readable, recursive |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 521 | |
Walter Dörwald | 7a7ede5 | 2003-12-03 20:15:28 +0000 | [diff] [blame] | 522 | if (issubclass(typ, list) and r is list.__repr__) or \ |
| 523 | (issubclass(typ, tuple) and r is tuple.__repr__): |
| 524 | if issubclass(typ, list): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 525 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 526 | return "[]", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 527 | format = "[%s]" |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 528 | elif len(object) == 1: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 529 | format = "(%s,)" |
| 530 | else: |
| 531 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 532 | return "()", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 533 | format = "(%s)" |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 534 | objid = id(object) |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 535 | if maxlevels and level >= maxlevels: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 536 | return format % "...", False, objid in context |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 537 | if objid in context: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 538 | return _recursion(object), False, True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 539 | context[objid] = 1 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 540 | readable = True |
| 541 | recursive = False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 542 | components = [] |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 543 | append = components.append |
| 544 | level += 1 |
| 545 | for o in object: |
| 546 | orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) |
| 547 | append(orepr) |
| 548 | if not oreadable: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 549 | readable = False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 550 | if orecur: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 551 | recursive = True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 552 | del context[objid] |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 553 | return format % ", ".join(components), readable, recursive |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 554 | |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 555 | rep = repr(object) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 556 | return rep, (rep and not rep.startswith('<')), False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 557 | |
Serhiy Storchaka | 8eb1f07 | 2015-05-16 21:38:05 +0300 | [diff] [blame] | 558 | _builtin_scalars = frozenset({str, bytes, bytearray, int, float, complex, |
| 559 | bool, type(None)}) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 560 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 561 | def _recursion(object): |
| 562 | return ("<Recursion on %s with id=%s>" |
Antoine Pitrou | 7d36e2f | 2013-10-03 21:29:36 +0200 | [diff] [blame] | 563 | % (type(object).__name__, id(object))) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 564 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 565 | |
| 566 | def _perfcheck(object=None): |
| 567 | import time |
| 568 | if object is None: |
| 569 | object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000 |
| 570 | p = PrettyPrinter() |
| 571 | t1 = time.time() |
| 572 | _safe_repr(object, {}, None, 0) |
| 573 | t2 = time.time() |
| 574 | p.pformat(object) |
| 575 | t3 = time.time() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 576 | print("_safe_repr:", t2 - t1) |
| 577 | print("pformat:", t3 - t2) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 578 | |
Serhiy Storchaka | 022f203 | 2015-03-24 19:22:37 +0200 | [diff] [blame] | 579 | def _wrap_bytes_repr(object, width, allowance): |
| 580 | current = b'' |
| 581 | last = len(object) // 4 * 4 |
| 582 | for i in range(0, len(object), 4): |
| 583 | part = object[i: i+4] |
| 584 | candidate = current + part |
| 585 | if i == last: |
| 586 | width -= allowance |
| 587 | if len(repr(candidate)) > width: |
| 588 | if current: |
| 589 | yield repr(current) |
| 590 | current = part |
| 591 | else: |
| 592 | current = candidate |
| 593 | if current: |
| 594 | yield repr(current) |
| 595 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 596 | if __name__ == "__main__": |
| 597 | _perfcheck() |