Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1 | """Redo the builtin repr() (representation) but with limits on most sizes.""" |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 2 | |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 3 | __all__ = ["Repr", "repr", "recursive_repr"] |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 4 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 5 | import builtins |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 6 | from itertools import islice |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 7 | try: |
Antoine Pitrou | bb0ad4c | 2011-10-11 18:51:53 +0200 | [diff] [blame] | 8 | from _thread import get_ident |
Antoine Pitrou | db1bad2 | 2010-11-05 19:58:28 +0000 | [diff] [blame] | 9 | except ImportError: |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 10 | from _dummy_thread import get_ident |
| 11 | |
| 12 | def recursive_repr(fillvalue='...'): |
| 13 | 'Decorator to make a repr function return fillvalue for a recursive call' |
| 14 | |
| 15 | def decorating_function(user_function): |
| 16 | repr_running = set() |
| 17 | |
| 18 | def wrapper(self): |
| 19 | key = id(self), get_ident() |
| 20 | if key in repr_running: |
| 21 | return fillvalue |
| 22 | repr_running.add(key) |
| 23 | try: |
| 24 | result = user_function(self) |
| 25 | finally: |
| 26 | repr_running.discard(key) |
| 27 | return result |
| 28 | |
| 29 | # Can't use functools.wraps() here because of bootstrap issues |
| 30 | wrapper.__module__ = getattr(user_function, '__module__') |
| 31 | wrapper.__doc__ = getattr(user_function, '__doc__') |
| 32 | wrapper.__name__ = getattr(user_function, '__name__') |
Raymond Hettinger | 6ef18f4 | 2011-01-23 21:33:36 +0000 | [diff] [blame] | 33 | wrapper.__annotations__ = getattr(user_function, '__annotations__', {}) |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 34 | return wrapper |
| 35 | |
| 36 | return decorating_function |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 38 | class Repr: |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 39 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 40 | def __init__(self): |
| 41 | self.maxlevel = 6 |
| 42 | self.maxtuple = 6 |
| 43 | self.maxlist = 6 |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 44 | self.maxarray = 5 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 45 | self.maxdict = 4 |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 46 | self.maxset = 6 |
| 47 | self.maxfrozenset = 6 |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 48 | self.maxdeque = 6 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 49 | self.maxstring = 30 |
| 50 | self.maxlong = 40 |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 51 | self.maxother = 30 |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 52 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 53 | def repr(self, x): |
| 54 | return self.repr1(x, self.maxlevel) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 55 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 56 | def repr1(self, x, level): |
Guido van Rossum | a995c91 | 2001-08-09 18:56:27 +0000 | [diff] [blame] | 57 | typename = type(x).__name__ |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 58 | if ' ' in typename: |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 59 | parts = typename.split() |
| 60 | typename = '_'.join(parts) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 61 | if hasattr(self, 'repr_' + typename): |
| 62 | return getattr(self, 'repr_' + typename)(x, level) |
| 63 | else: |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 64 | return self.repr_instance(x, level) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 65 | |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 66 | def _repr_iterable(self, x, level, left, right, maxiter, trail=''): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 67 | n = len(x) |
| 68 | if level <= 0 and n: |
| 69 | s = '...' |
| 70 | else: |
| 71 | newlevel = level - 1 |
| 72 | repr1 = self.repr1 |
| 73 | pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)] |
| 74 | if n > maxiter: pieces.append('...') |
| 75 | s = ', '.join(pieces) |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 76 | if n == 1 and trail: right = trail + right |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 77 | return '%s%s%s' % (left, s, right) |
| 78 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 79 | def repr_tuple(self, x, level): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 80 | return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',') |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 81 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 82 | def repr_list(self, x, level): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 83 | return self._repr_iterable(x, level, '[', ']', self.maxlist) |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 84 | |
| 85 | def repr_array(self, x, level): |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 86 | header = "array('%s', [" % x.typecode |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 87 | return self._repr_iterable(x, level, header, '])', self.maxarray) |
| 88 | |
| 89 | def repr_set(self, x, level): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 90 | x = _possibly_sorted(x) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 91 | return self._repr_iterable(x, level, 'set([', '])', self.maxset) |
| 92 | |
| 93 | def repr_frozenset(self, x, level): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 94 | x = _possibly_sorted(x) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 95 | return self._repr_iterable(x, level, 'frozenset([', '])', |
| 96 | self.maxfrozenset) |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 97 | |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 98 | def repr_deque(self, x, level): |
| 99 | return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque) |
| 100 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 101 | def repr_dict(self, x, level): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 102 | n = len(x) |
| 103 | if n == 0: return '{}' |
| 104 | if level <= 0: return '{...}' |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 105 | newlevel = level - 1 |
| 106 | repr1 = self.repr1 |
| 107 | pieces = [] |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 108 | for key in islice(_possibly_sorted(x), self.maxdict): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 109 | keyrepr = repr1(key, newlevel) |
| 110 | valrepr = repr1(x[key], newlevel) |
| 111 | pieces.append('%s: %s' % (keyrepr, valrepr)) |
| 112 | if n > self.maxdict: pieces.append('...') |
| 113 | s = ', '.join(pieces) |
| 114 | return '{%s}' % (s,) |
| 115 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 116 | def repr_str(self, x, level): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 117 | s = builtins.repr(x[:self.maxstring]) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 118 | if len(s) > self.maxstring: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 119 | i = max(0, (self.maxstring-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 120 | j = max(0, self.maxstring-3-i) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 121 | s = builtins.repr(x[:i] + x[len(x)-j:]) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 122 | s = s[:i] + '...' + s[len(s)-j:] |
| 123 | return s |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 4c9695a | 2007-01-15 00:14:39 +0000 | [diff] [blame] | 125 | def repr_int(self, x, level): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 126 | s = builtins.repr(x) # XXX Hope this isn't too slow... |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 127 | if len(s) > self.maxlong: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 128 | i = max(0, (self.maxlong-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 129 | j = max(0, self.maxlong-3-i) |
| 130 | s = s[:i] + '...' + s[len(s)-j:] |
| 131 | return s |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 132 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 133 | def repr_instance(self, x, level): |
| 134 | try: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 135 | s = builtins.repr(x) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 136 | # Bugs in x.__repr__() can cause arbitrary |
| 137 | # exceptions -- then make up something |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 138 | except Exception: |
Fred Drake | 83a6430 | 2004-07-01 20:28:47 +0000 | [diff] [blame] | 139 | return '<%s instance at %x>' % (x.__class__.__name__, id(x)) |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 140 | if len(s) > self.maxother: |
| 141 | i = max(0, (self.maxother-3)//2) |
| 142 | j = max(0, self.maxother-3-i) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 143 | s = s[:i] + '...' + s[len(s)-j:] |
| 144 | return s |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 145 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 146 | |
| 147 | def _possibly_sorted(x): |
| 148 | # Since not all sequences of items can be sorted and comparison |
| 149 | # functions may raise arbitrary exceptions, return an unsorted |
| 150 | # sequence in that case. |
| 151 | try: |
| 152 | return sorted(x) |
| 153 | except Exception: |
| 154 | return list(x) |
| 155 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 156 | aRepr = Repr() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 157 | repr = aRepr.repr |