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