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 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 5 | class Repr: |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 6 | def __init__(self): |
| 7 | self.maxlevel = 6 |
| 8 | self.maxtuple = 6 |
| 9 | self.maxlist = 6 |
| 10 | self.maxdict = 4 |
| 11 | self.maxstring = 30 |
| 12 | self.maxlong = 40 |
| 13 | self.maxother = 20 |
| 14 | def repr(self, x): |
| 15 | return self.repr1(x, self.maxlevel) |
| 16 | def repr1(self, x, level): |
Guido van Rossum | a995c91 | 2001-08-09 18:56:27 +0000 | [diff] [blame] | 17 | typename = type(x).__name__ |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 18 | if ' ' in typename: |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 19 | parts = typename.split() |
| 20 | typename = '_'.join(parts) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 21 | if hasattr(self, 'repr_' + typename): |
| 22 | return getattr(self, 'repr_' + typename)(x, level) |
| 23 | else: |
| 24 | s = `x` |
| 25 | if len(s) > self.maxother: |
Guido van Rossum | 6fd0f0a | 2001-09-05 02:27:04 +0000 | [diff] [blame] | 26 | i = max(0, (self.maxother-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 27 | j = max(0, self.maxother-3-i) |
| 28 | s = s[:i] + '...' + s[len(s)-j:] |
| 29 | return s |
| 30 | def repr_tuple(self, x, level): |
| 31 | n = len(x) |
| 32 | if n == 0: return '()' |
| 33 | if level <= 0: return '(...)' |
| 34 | s = '' |
| 35 | for i in range(min(n, self.maxtuple)): |
| 36 | if s: s = s + ', ' |
| 37 | s = s + self.repr1(x[i], level-1) |
| 38 | if n > self.maxtuple: s = s + ', ...' |
| 39 | elif n == 1: s = s + ',' |
| 40 | return '(' + s + ')' |
| 41 | def repr_list(self, x, level): |
| 42 | n = len(x) |
| 43 | if n == 0: return '[]' |
| 44 | if level <= 0: return '[...]' |
| 45 | s = '' |
| 46 | for i in range(min(n, self.maxlist)): |
| 47 | if s: s = s + ', ' |
| 48 | s = s + self.repr1(x[i], level-1) |
| 49 | if n > self.maxlist: s = s + ', ...' |
| 50 | return '[' + s + ']' |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 51 | def repr_dict(self, x, level): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 52 | n = len(x) |
| 53 | if n == 0: return '{}' |
| 54 | if level <= 0: return '{...}' |
| 55 | s = '' |
| 56 | keys = x.keys() |
| 57 | keys.sort() |
| 58 | for i in range(min(n, self.maxdict)): |
| 59 | if s: s = s + ', ' |
| 60 | key = keys[i] |
| 61 | s = s + self.repr1(key, level-1) |
| 62 | s = s + ': ' + self.repr1(x[key], level-1) |
| 63 | if n > self.maxdict: s = s + ', ...' |
| 64 | return '{' + s + '}' |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 65 | def repr_str(self, x, level): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 66 | s = `x[:self.maxstring]` |
| 67 | if len(s) > self.maxstring: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 68 | i = max(0, (self.maxstring-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 69 | j = max(0, self.maxstring-3-i) |
| 70 | s = `x[:i] + x[len(x)-j:]` |
| 71 | s = s[:i] + '...' + s[len(s)-j:] |
| 72 | return s |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 73 | def repr_long(self, x, level): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 74 | s = `x` # XXX Hope this isn't too slow... |
| 75 | if len(s) > self.maxlong: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 76 | i = max(0, (self.maxlong-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 77 | j = max(0, self.maxlong-3-i) |
| 78 | s = s[:i] + '...' + s[len(s)-j:] |
| 79 | return s |
| 80 | def repr_instance(self, x, level): |
| 81 | try: |
| 82 | s = `x` |
| 83 | # Bugs in x.__repr__() can cause arbitrary |
| 84 | # exceptions -- then make up something |
| 85 | except: |
| 86 | return '<' + x.__class__.__name__ + ' instance at ' + \ |
| 87 | hex(id(x))[2:] + '>' |
| 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) |
| 91 | s = s[:i] + '...' + s[len(s)-j:] |
| 92 | return s |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 94 | aRepr = Repr() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 95 | repr = aRepr.repr |