blob: c69ce28c450fec8bafaba28bd61cc60a14905000 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Redo the `...` (representation) but with limits on most sizes."""
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00002
Skip Montanaro0de65802001-02-15 22:15:14 +00003__all__ = ["Repr","repr"]
4
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00005class Repr:
Tim Peters0c9886d2001-01-15 01:18:21 +00006 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 Rossuma995c912001-08-09 18:56:27 +000017 typename = type(x).__name__
Tim Peters0c9886d2001-01-15 01:18:21 +000018 if ' ' in typename:
Eric S. Raymond6b71e742001-02-09 08:56:30 +000019 parts = typename.split()
20 typename = '_'.join(parts)
Tim Peters0c9886d2001-01-15 01:18:21 +000021 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 Rossum6fd0f0a2001-09-05 02:27:04 +000026 i = max(0, (self.maxother-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000027 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 Petersa427a2b2001-10-29 22:25:45 +000051 def repr_dict(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +000052 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 Peters6d6c1a32001-08-02 04:15:00 +000065 def repr_str(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +000066 s = `x[:self.maxstring]`
67 if len(s) > self.maxstring:
Guido van Rossum54e54c62001-09-04 19:14:14 +000068 i = max(0, (self.maxstring-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000069 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 Peters6d6c1a32001-08-02 04:15:00 +000073 def repr_long(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +000074 s = `x` # XXX Hope this isn't too slow...
75 if len(s) > self.maxlong:
Guido van Rossum54e54c62001-09-04 19:14:14 +000076 i = max(0, (self.maxlong-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000077 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 Rossum54e54c62001-09-04 19:14:14 +000089 i = max(0, (self.maxstring-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000090 j = max(0, self.maxstring-3-i)
91 s = s[:i] + '...' + s[len(s)-j:]
92 return s
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000093
Guido van Rossum7bc817d1993-12-17 15:25:27 +000094aRepr = Repr()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000095repr = aRepr.repr