Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 1 | # Redo the `...` (representation) but with limits on most sizes. |
| 2 | |
| 3 | import string |
| 4 | |
| 5 | class Repr: |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 6 | def __init__(self): |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 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 |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 14 | def repr(self, x): |
| 15 | return self.repr1(x, self.maxlevel) |
| 16 | def repr1(self, x, level): |
| 17 | typename = `type(x)`[7:-2] # "<type '......'>" |
| 18 | if ' ' in typename: |
| 19 | parts = string.split(typename) |
| 20 | typename = string.joinfields(parts, '_') |
Guido van Rossum | 8d2080d | 1995-01-12 12:29:17 +0000 | [diff] [blame] | 21 | if hasattr(self, 'repr_' + typename): |
| 22 | return getattr(self, 'repr_' + typename)(x, level) |
| 23 | else: |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 24 | s = `x` |
| 25 | if len(s) > self.maxother: |
| 26 | i = max(0, (self.maxother-3)/2) |
| 27 | j = max(0, self.maxother-3-i) |
| 28 | s = s[:i] + '...' + s[len(s)-j:] |
| 29 | return s |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 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 + ']' |
| 51 | def repr_dictionary(self, x, level): |
| 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) |
Guido van Rossum | ff3a278 | 1999-06-23 23:27:05 +0000 | [diff] [blame] | 63 | if n > self.maxdict: s = s + ', ...' |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 64 | return '{' + s + '}' |
| 65 | def repr_string(self, x, level): |
| 66 | s = `x[:self.maxstring]` |
| 67 | if len(s) > self.maxstring: |
| 68 | i = max(0, (self.maxstring-3)/2) |
| 69 | j = max(0, self.maxstring-3-i) |
Guido van Rossum | 8d2080d | 1995-01-12 12:29:17 +0000 | [diff] [blame] | 70 | s = `x[:i] + x[len(x)-j:]` |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 71 | s = s[:i] + '...' + s[len(s)-j:] |
| 72 | return s |
| 73 | def repr_long_int(self, x, level): |
| 74 | s = `x` # XXX Hope this isn't too slow... |
| 75 | if len(s) > self.maxlong: |
| 76 | i = max(0, (self.maxlong-3)/2) |
| 77 | j = max(0, self.maxlong-3-i) |
| 78 | s = s[:i] + '...' + s[len(s)-j:] |
| 79 | return s |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 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: |
| 89 | i = max(0, (self.maxstring-3)/2) |
| 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 |