blob: 0431857bbe675a9a5fae487d129f7ea184ced01c [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
Tim Peters6ee04802003-02-05 18:29:34 +000010 self.maxarray = 5
Tim Peters0c9886d2001-01-15 01:18:21 +000011 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 Rossuma995c912001-08-09 18:56:27 +000018 typename = type(x).__name__
Tim Peters0c9886d2001-01-15 01:18:21 +000019 if ' ' in typename:
Eric S. Raymond6b71e742001-02-09 08:56:30 +000020 parts = typename.split()
21 typename = '_'.join(parts)
Tim Peters0c9886d2001-01-15 01:18:21 +000022 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 Rossum6fd0f0a2001-09-05 02:27:04 +000027 i = max(0, (self.maxother-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000028 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 Peters6ee04802003-02-05 18:29:34 +000052
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 Petersa427a2b2001-10-29 22:25:45 +000069 def repr_dict(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +000070 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 Peters6d6c1a32001-08-02 04:15:00 +000083 def repr_str(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +000084 s = `x[:self.maxstring]`
85 if len(s) > self.maxstring:
Guido van Rossum54e54c62001-09-04 19:14:14 +000086 i = max(0, (self.maxstring-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000087 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 Peters6d6c1a32001-08-02 04:15:00 +000091 def repr_long(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +000092 s = `x` # XXX Hope this isn't too slow...
93 if len(s) > self.maxlong:
Guido van Rossum54e54c62001-09-04 19:14:14 +000094 i = max(0, (self.maxlong-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000095 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 Rossum54e54c62001-09-04 19:14:14 +0000107 i = max(0, (self.maxstring-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +0000108 j = max(0, self.maxstring-3-i)
109 s = s[:i] + '...' + s[len(s)-j:]
110 return s
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000111
Guido van Rossum7bc817d1993-12-17 15:25:27 +0000112aRepr = Repr()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000113repr = aRepr.repr