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() |
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 | |
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 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 51 | def pprint(object, stream=None): |
| 52 | """Pretty-print a Python object to a stream [default is sys.sydout].""" |
| 53 | printer = PrettyPrinter(stream=stream) |
| 54 | printer.pprint(object) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 55 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 56 | def pformat(object): |
| 57 | """Format a Python object into a pretty-printed representation.""" |
| 58 | return PrettyPrinter().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 | |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 72 | class PrettyPrinter: |
| 73 | def __init__(self, indent=1, width=80, depth=None, stream=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 74 | """Handle pretty printing operations onto a stream using a set of |
| 75 | configured parameters. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | indent |
| 78 | Number of spaces to indent for each level of nesting. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 80 | width |
| 81 | Attempted maximum number of columns in the output. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 83 | depth |
| 84 | The maximum depth to print out nested structures. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 86 | stream |
| 87 | The desired output stream. If omitted (or false), the standard |
| 88 | output stream available at construction will be used. |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 90 | """ |
| 91 | indent = int(indent) |
| 92 | width = int(width) |
| 93 | assert indent >= 0 |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 94 | assert depth is None or depth > 0, "depth must be > 0" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 95 | assert width |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 96 | self._depth = depth |
| 97 | self._indent_per_level = indent |
| 98 | self._width = width |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 99 | if stream is not None: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 100 | self._stream = stream |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 101 | else: |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 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): |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 105 | self._stream.write(self.pformat(object) + "\n") |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 106 | |
| 107 | def pformat(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 108 | sio = _StringIO() |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 109 | self._format(object, sio, 0, 0, {}, 0) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 110 | return sio.getvalue() |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 111 | |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 112 | def isrecursive(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 113 | return self.format(object, {}, 0, 0)[2] |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 114 | |
| 115 | def isreadable(self, object): |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 116 | s, readable, recursive = self.format(object, {}, 0, 0) |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 117 | return readable and not recursive |
Fred Drake | e0ffabe | 1997-07-18 20:42:39 +0000 | [diff] [blame] | 118 | |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 119 | def _format(self, object, stream, indent, allowance, context, level): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 120 | level = level + 1 |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 121 | objid = _id(object) |
| 122 | if objid in context: |
| 123 | stream.write(_recursion(object)) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 124 | self._recursive = True |
| 125 | self._readable = False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 126 | return |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 127 | rep = self._repr(object, context, level - 1) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 128 | typ = _type(object) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 129 | sepLines = _len(rep) > (self._width - 1 - indent - allowance) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 130 | write = stream.write |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 131 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 132 | if sepLines: |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 133 | if typ is dict: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 134 | write('{') |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 135 | if self._indent_per_level > 1: |
| 136 | write((self._indent_per_level - 1) * ' ') |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 137 | length = _len(object) |
| 138 | if length: |
| 139 | context[objid] = 1 |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 140 | indent = indent + self._indent_per_level |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 141 | items = object.items() |
| 142 | items.sort() |
| 143 | key, ent = items[0] |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 144 | rep = self._repr(key, context, level) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 145 | write(rep) |
| 146 | write(': ') |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 147 | self._format(ent, stream, indent + _len(rep) + 2, |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 148 | allowance + 1, context, level) |
| 149 | if length > 1: |
| 150 | for key, ent in items[1:]: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 151 | rep = self._repr(key, context, level) |
Barry Warsaw | 00859c0 | 2001-11-28 05:49:39 +0000 | [diff] [blame] | 152 | write(',\n%s%s: ' % (' '*indent, rep)) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 153 | self._format(ent, stream, indent + _len(rep) + 2, |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 154 | allowance + 1, context, level) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 155 | indent = indent - self._indent_per_level |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 156 | del context[objid] |
| 157 | write('}') |
| 158 | return |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 159 | |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 160 | if typ is list or typ is tuple: |
| 161 | if typ is list: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 162 | write('[') |
| 163 | endchar = ']' |
| 164 | else: |
| 165 | write('(') |
| 166 | endchar = ')' |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 167 | if self._indent_per_level > 1: |
| 168 | write((self._indent_per_level - 1) * ' ') |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 169 | length = _len(object) |
| 170 | if length: |
| 171 | context[objid] = 1 |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 172 | indent = indent + self._indent_per_level |
| 173 | self._format(object[0], stream, indent, allowance + 1, |
| 174 | context, level) |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 175 | if length > 1: |
| 176 | for ent in object[1:]: |
| 177 | write(',\n' + ' '*indent) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 178 | self._format(ent, stream, indent, |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 179 | allowance + 1, context, level) |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 180 | indent = indent - self._indent_per_level |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 181 | del context[objid] |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 182 | if typ is tuple and length == 1: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 183 | write(',') |
| 184 | write(endchar) |
| 185 | return |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 186 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 187 | write(rep) |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 188 | |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 189 | def _repr(self, object, context, level): |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 190 | repr, readable, recursive = self.format(object, context.copy(), |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 191 | self._depth, level) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 192 | if not readable: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 193 | self._readable = False |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 194 | if recursive: |
Fred Drake | e6691ef | 2002-07-08 12:28:06 +0000 | [diff] [blame] | 195 | self._recursive = True |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 196 | return repr |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 197 | |
Fred Drake | aee113d | 2002-04-02 05:08:35 +0000 | [diff] [blame] | 198 | def format(self, object, context, maxlevels, level): |
| 199 | """Format object for a specific context, returning a string |
| 200 | and flags indicating whether the representation is 'readable' |
| 201 | and whether the object represents a recursive construct. |
| 202 | """ |
| 203 | return _safe_repr(object, context, maxlevels, level) |
| 204 | |
| 205 | |
Tim Peters | a814db5 | 2001-05-14 07:05:58 +0000 | [diff] [blame] | 206 | # Return triple (repr_string, isreadable, isrecursive). |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 207 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 208 | def _safe_repr(object, context, maxlevels, level): |
| 209 | typ = _type(object) |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 210 | if typ is str: |
| 211 | if 'locale' not in _sys.modules: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 212 | return `object`, True, False |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 213 | if "'" in object and '"' not in object: |
| 214 | closure = '"' |
| 215 | quotes = {'"': '\\"'} |
| 216 | else: |
| 217 | closure = "'" |
| 218 | quotes = {"'": "\\'"} |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 219 | qget = quotes.get |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 220 | sio = _StringIO() |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 221 | write = sio.write |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 222 | for char in object: |
| 223 | if char.isalpha(): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 224 | write(char) |
Fred Drake | 1ef106c | 2001-09-04 19:43:26 +0000 | [diff] [blame] | 225 | else: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 226 | write(qget(char, `char`[1:-1])) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 227 | return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 228 | |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 229 | if typ is dict: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 230 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 231 | return "{}", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 232 | objid = _id(object) |
| 233 | if maxlevels and level > maxlevels: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 234 | return "{...}", False, objid in context |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 235 | if objid in context: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 236 | return _recursion(object), False, True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 237 | context[objid] = 1 |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 238 | readable = True |
| 239 | recursive = False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 240 | components = [] |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 241 | append = components.append |
| 242 | level += 1 |
| 243 | saferepr = _safe_repr |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 244 | for k, v in object.iteritems(): |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 245 | krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) |
| 246 | vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) |
| 247 | append("%s: %s" % (krepr, vrepr)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 248 | readable = readable and kreadable and vreadable |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 249 | if krecur or vrecur: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 250 | recursive = True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 251 | del context[objid] |
| 252 | return "{%s}" % _commajoin(components), readable, recursive |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 253 | |
Fred Drake | 397b615 | 2002-12-31 07:14:18 +0000 | [diff] [blame] | 254 | if typ is list or typ is tuple: |
| 255 | if typ is list: |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 256 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 257 | return "[]", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 258 | format = "[%s]" |
| 259 | elif _len(object) == 1: |
| 260 | format = "(%s,)" |
| 261 | else: |
| 262 | if not object: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 263 | return "()", True, False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 264 | format = "(%s)" |
| 265 | objid = _id(object) |
| 266 | if maxlevels and level > maxlevels: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 267 | return format % "...", 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 | for o in object: |
| 277 | orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) |
| 278 | append(orepr) |
| 279 | if not oreadable: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 280 | readable = False |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 281 | if orecur: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 282 | recursive = True |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 283 | del context[objid] |
| 284 | return format % _commajoin(components), readable, recursive |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 285 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 286 | rep = `object` |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 287 | return rep, (rep and not rep.startswith('<')), False |
Tim Peters | 95b3f78 | 2001-05-14 18:39:41 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 5e92aff | 1997-04-16 00:49:59 +0000 | [diff] [blame] | 289 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 290 | def _recursion(object): |
| 291 | return ("<Recursion on %s with id=%s>" |
| 292 | % (_type(object).__name__, _id(object))) |
Fred Drake | a89fda0 | 1997-04-16 16:59:30 +0000 | [diff] [blame] | 293 | |
Fred Drake | 49cc01e | 2001-11-01 17:50:38 +0000 | [diff] [blame] | 294 | |
| 295 | def _perfcheck(object=None): |
| 296 | import time |
| 297 | if object is None: |
| 298 | object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000 |
| 299 | p = PrettyPrinter() |
| 300 | t1 = time.time() |
| 301 | _safe_repr(object, {}, None, 0) |
| 302 | t2 = time.time() |
| 303 | p.pformat(object) |
| 304 | t3 = time.time() |
| 305 | print "_safe_repr:", t2 - t1 |
| 306 | print "pformat:", t3 - t2 |
| 307 | |
| 308 | if __name__ == "__main__": |
| 309 | _perfcheck() |