blob: 0827428ba7fb5d16b7bf104bbbed3c76ac234d74 [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
3import string
4
5class Repr:
Guido van Rossum7bc817d1993-12-17 15:25:27 +00006 def __init__(self):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00007 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 Rossumbabe2bf1992-01-22 22:21:31 +000014 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 Rossum8d2080d1995-01-12 12:29:17 +000021 if hasattr(self, 'repr_' + typename):
22 return getattr(self, 'repr_' + typename)(x, level)
23 else:
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000024 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 Rossumbabe2bf1992-01-22 22:21:31 +000030 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 Rossumff3a2781999-06-23 23:27:05 +000063 if n > self.maxdict: s = s + ', ...'
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000064 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 Rossum8d2080d1995-01-12 12:29:17 +000070 s = `x[:i] + x[len(x)-j:]`
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000071 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 Rossumb6775db1994-08-01 11:34:53 +000080 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 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