Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Redo the `...` (representation) but with limits on most sizes.""" |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 2 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 3 | __all__ = ["Repr","repr"] |
| 4 | |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 5 | import __builtin__ |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 6 | from itertools import islice |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 8 | class Repr: |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 9 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 10 | def __init__(self): |
| 11 | self.maxlevel = 6 |
| 12 | self.maxtuple = 6 |
| 13 | self.maxlist = 6 |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 14 | self.maxarray = 5 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 15 | self.maxdict = 4 |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 16 | self.maxset = 6 |
| 17 | self.maxfrozenset = 6 |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 18 | self.maxdeque = 6 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 19 | self.maxstring = 30 |
| 20 | self.maxlong = 40 |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 21 | self.maxother = 30 |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 22 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 23 | def repr(self, x): |
| 24 | return self.repr1(x, self.maxlevel) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 25 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 26 | def repr1(self, x, level): |
Guido van Rossum | a995c91 | 2001-08-09 18:56:27 +0000 | [diff] [blame] | 27 | typename = type(x).__name__ |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 28 | if ' ' in typename: |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 29 | parts = typename.split() |
| 30 | typename = '_'.join(parts) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 31 | if hasattr(self, 'repr_' + typename): |
| 32 | return getattr(self, 'repr_' + typename)(x, level) |
| 33 | else: |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 34 | return self.repr_instance(x, level) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 35 | |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 36 | def _repr_iterable(self, x, level, left, right, maxiter, trail=''): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 37 | n = len(x) |
| 38 | if level <= 0 and n: |
| 39 | s = '...' |
| 40 | else: |
| 41 | newlevel = level - 1 |
| 42 | repr1 = self.repr1 |
| 43 | pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)] |
| 44 | if n > maxiter: pieces.append('...') |
| 45 | s = ', '.join(pieces) |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 46 | if n == 1 and trail: right = trail + right |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 47 | return '%s%s%s' % (left, s, right) |
| 48 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 49 | def repr_tuple(self, x, level): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 50 | return self._repr_iterable(x, level, '(', ')', self.maxlist, ',') |
| 51 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 52 | def repr_list(self, x, level): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 53 | return self._repr_iterable(x, level, '[', ']', self.maxlist) |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 54 | |
| 55 | def repr_array(self, x, level): |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 56 | header = "array('%s', [" % x.typecode |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 57 | return self._repr_iterable(x, level, header, '])', self.maxarray) |
| 58 | |
| 59 | def repr_set(self, x, level): |
Raymond Hettinger | ddc819c | 2004-05-22 19:37:21 +0000 | [diff] [blame] | 60 | x = sorted(x) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 61 | return self._repr_iterable(x, level, 'set([', '])', self.maxset) |
| 62 | |
| 63 | def repr_frozenset(self, x, level): |
Raymond Hettinger | ddc819c | 2004-05-22 19:37:21 +0000 | [diff] [blame] | 64 | x = sorted(x) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 65 | return self._repr_iterable(x, level, 'frozenset([', '])', |
| 66 | self.maxfrozenset) |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 67 | |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 68 | def repr_deque(self, x, level): |
| 69 | return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque) |
| 70 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 71 | def repr_dict(self, x, level): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 72 | n = len(x) |
| 73 | if n == 0: return '{}' |
| 74 | if level <= 0: return '{...}' |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 75 | newlevel = level - 1 |
| 76 | repr1 = self.repr1 |
| 77 | pieces = [] |
| 78 | for key in islice(sorted(x), self.maxdict): |
| 79 | keyrepr = repr1(key, newlevel) |
| 80 | valrepr = repr1(x[key], newlevel) |
| 81 | pieces.append('%s: %s' % (keyrepr, valrepr)) |
| 82 | if n > self.maxdict: pieces.append('...') |
| 83 | s = ', '.join(pieces) |
| 84 | return '{%s}' % (s,) |
| 85 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 86 | def repr_str(self, x, level): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 87 | s = __builtin__.repr(x[:self.maxstring]) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 88 | if len(s) > self.maxstring: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 89 | i = max(0, (self.maxstring-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 90 | j = max(0, self.maxstring-3-i) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 91 | s = __builtin__.repr(x[:i] + x[len(x)-j:]) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 92 | s = s[:i] + '...' + s[len(s)-j:] |
| 93 | return s |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 4c9695a | 2007-01-15 00:14:39 +0000 | [diff] [blame] | 95 | def repr_int(self, x, level): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 96 | s = __builtin__.repr(x) # XXX Hope this isn't too slow... |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 97 | if len(s) > self.maxlong: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 98 | i = max(0, (self.maxlong-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 99 | j = max(0, self.maxlong-3-i) |
| 100 | s = s[:i] + '...' + s[len(s)-j:] |
| 101 | return s |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 102 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 103 | def repr_instance(self, x, level): |
| 104 | try: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 105 | s = __builtin__.repr(x) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 106 | # Bugs in x.__repr__() can cause arbitrary |
| 107 | # exceptions -- then make up something |
| 108 | except: |
Fred Drake | 83a6430 | 2004-07-01 20:28:47 +0000 | [diff] [blame] | 109 | return '<%s instance at %x>' % (x.__class__.__name__, id(x)) |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 110 | if len(s) > self.maxother: |
| 111 | i = max(0, (self.maxother-3)//2) |
| 112 | j = max(0, self.maxother-3-i) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 113 | s = s[:i] + '...' + s[len(s)-j:] |
| 114 | return s |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 116 | aRepr = Repr() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 117 | repr = aRepr.repr |