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