2000-02-04 11:28:42 -04:00
|
|
|
"""Redo the `...` (representation) but with limits on most sizes."""
|
1992-01-22 18:21:31 -04:00
|
|
|
|
2001-02-15 18:15:14 -04:00
|
|
|
__all__ = ["Repr","repr"]
|
|
|
|
|
1992-01-22 18:21:31 -04:00
|
|
|
class Repr:
|
2001-01-14 21:18:21 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.maxlevel = 6
|
|
|
|
self.maxtuple = 6
|
|
|
|
self.maxlist = 6
|
2003-02-05 14:29:34 -04:00
|
|
|
self.maxarray = 5
|
2001-01-14 21:18:21 -04:00
|
|
|
self.maxdict = 4
|
|
|
|
self.maxstring = 30
|
|
|
|
self.maxlong = 40
|
|
|
|
self.maxother = 20
|
|
|
|
def repr(self, x):
|
|
|
|
return self.repr1(x, self.maxlevel)
|
|
|
|
def repr1(self, x, level):
|
2001-08-09 15:56:27 -03:00
|
|
|
typename = type(x).__name__
|
2001-01-14 21:18:21 -04:00
|
|
|
if ' ' in typename:
|
2001-02-09 04:56:30 -04:00
|
|
|
parts = typename.split()
|
|
|
|
typename = '_'.join(parts)
|
2001-01-14 21:18:21 -04:00
|
|
|
if hasattr(self, 'repr_' + typename):
|
|
|
|
return getattr(self, 'repr_' + typename)(x, level)
|
|
|
|
else:
|
|
|
|
s = `x`
|
|
|
|
if len(s) > self.maxother:
|
2001-09-04 23:27:04 -03:00
|
|
|
i = max(0, (self.maxother-3)//2)
|
2001-01-14 21:18:21 -04:00
|
|
|
j = max(0, self.maxother-3-i)
|
|
|
|
s = s[:i] + '...' + s[len(s)-j:]
|
|
|
|
return s
|
|
|
|
def repr_tuple(self, x, level):
|
|
|
|
n = len(x)
|
|
|
|
if n == 0: return '()'
|
|
|
|
if level <= 0: return '(...)'
|
|
|
|
s = ''
|
|
|
|
for i in range(min(n, self.maxtuple)):
|
|
|
|
if s: s = s + ', '
|
|
|
|
s = s + self.repr1(x[i], level-1)
|
|
|
|
if n > self.maxtuple: s = s + ', ...'
|
|
|
|
elif n == 1: s = s + ','
|
|
|
|
return '(' + s + ')'
|
|
|
|
def repr_list(self, x, level):
|
|
|
|
n = len(x)
|
|
|
|
if n == 0: return '[]'
|
|
|
|
if level <= 0: return '[...]'
|
|
|
|
s = ''
|
|
|
|
for i in range(min(n, self.maxlist)):
|
|
|
|
if s: s = s + ', '
|
|
|
|
s = s + self.repr1(x[i], level-1)
|
|
|
|
if n > self.maxlist: s = s + ', ...'
|
|
|
|
return '[' + s + ']'
|
2003-02-05 14:29:34 -04:00
|
|
|
|
|
|
|
def repr_array(self, x, level):
|
|
|
|
n = len(x)
|
|
|
|
header = "array('%s', [" % x.typecode
|
|
|
|
if n == 0:
|
|
|
|
return header + "])"
|
|
|
|
if level <= 0:
|
|
|
|
return header + "...])"
|
|
|
|
s = ''
|
|
|
|
for i in range(min(n, self.maxarray)):
|
|
|
|
if s:
|
|
|
|
s += ', '
|
|
|
|
s += self.repr1(x[i], level-1)
|
|
|
|
if n > self.maxarray:
|
|
|
|
s += ', ...'
|
|
|
|
return header + s + "])"
|
|
|
|
|
2001-10-29 18:25:45 -04:00
|
|
|
def repr_dict(self, x, level):
|
2001-01-14 21:18:21 -04:00
|
|
|
n = len(x)
|
|
|
|
if n == 0: return '{}'
|
|
|
|
if level <= 0: return '{...}'
|
|
|
|
s = ''
|
|
|
|
keys = x.keys()
|
|
|
|
keys.sort()
|
|
|
|
for i in range(min(n, self.maxdict)):
|
|
|
|
if s: s = s + ', '
|
|
|
|
key = keys[i]
|
|
|
|
s = s + self.repr1(key, level-1)
|
|
|
|
s = s + ': ' + self.repr1(x[key], level-1)
|
|
|
|
if n > self.maxdict: s = s + ', ...'
|
|
|
|
return '{' + s + '}'
|
2001-08-02 01:15:00 -03:00
|
|
|
def repr_str(self, x, level):
|
2001-01-14 21:18:21 -04:00
|
|
|
s = `x[:self.maxstring]`
|
|
|
|
if len(s) > self.maxstring:
|
2001-09-04 16:14:14 -03:00
|
|
|
i = max(0, (self.maxstring-3)//2)
|
2001-01-14 21:18:21 -04:00
|
|
|
j = max(0, self.maxstring-3-i)
|
|
|
|
s = `x[:i] + x[len(x)-j:]`
|
|
|
|
s = s[:i] + '...' + s[len(s)-j:]
|
|
|
|
return s
|
2001-08-02 01:15:00 -03:00
|
|
|
def repr_long(self, x, level):
|
2001-01-14 21:18:21 -04:00
|
|
|
s = `x` # XXX Hope this isn't too slow...
|
|
|
|
if len(s) > self.maxlong:
|
2001-09-04 16:14:14 -03:00
|
|
|
i = max(0, (self.maxlong-3)//2)
|
2001-01-14 21:18:21 -04:00
|
|
|
j = max(0, self.maxlong-3-i)
|
|
|
|
s = s[:i] + '...' + s[len(s)-j:]
|
|
|
|
return s
|
|
|
|
def repr_instance(self, x, level):
|
|
|
|
try:
|
|
|
|
s = `x`
|
|
|
|
# Bugs in x.__repr__() can cause arbitrary
|
|
|
|
# exceptions -- then make up something
|
|
|
|
except:
|
|
|
|
return '<' + x.__class__.__name__ + ' instance at ' + \
|
|
|
|
hex(id(x))[2:] + '>'
|
|
|
|
if len(s) > self.maxstring:
|
2001-09-04 16:14:14 -03:00
|
|
|
i = max(0, (self.maxstring-3)//2)
|
2001-01-14 21:18:21 -04:00
|
|
|
j = max(0, self.maxstring-3-i)
|
|
|
|
s = s[:i] + '...' + s[len(s)-j:]
|
|
|
|
return s
|
1992-01-22 18:21:31 -04:00
|
|
|
|
1993-12-17 11:25:27 -04:00
|
|
|
aRepr = Repr()
|
1992-01-22 18:21:31 -04:00
|
|
|
repr = aRepr.repr
|