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"]
|
|
|
|
|
2004-02-12 13:35:32 -04:00
|
|
|
import __builtin__
|
2004-05-21 07:00:15 -03:00
|
|
|
from itertools import islice
|
2004-02-12 13:35:32 -04:00
|
|
|
|
1992-01-22 18:21:31 -04:00
|
|
|
class Repr:
|
2004-05-21 07:00:15 -03:00
|
|
|
|
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
|
2004-05-21 07:00:15 -03:00
|
|
|
self.maxset = 6
|
|
|
|
self.maxfrozenset = 6
|
2004-05-21 20:01:18 -03:00
|
|
|
self.maxdeque = 6
|
2001-01-14 21:18:21 -04:00
|
|
|
self.maxstring = 30
|
|
|
|
self.maxlong = 40
|
|
|
|
self.maxother = 20
|
2004-05-21 07:00:15 -03:00
|
|
|
|
2001-01-14 21:18:21 -04:00
|
|
|
def repr(self, x):
|
|
|
|
return self.repr1(x, self.maxlevel)
|
2004-05-21 07:00:15 -03:00
|
|
|
|
2001-01-14 21:18:21 -04:00
|
|
|
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:
|
2004-02-12 13:35:32 -04:00
|
|
|
s = __builtin__.repr(x)
|
2001-01-14 21:18:21 -04:00
|
|
|
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
|
2004-05-21 07:00:15 -03:00
|
|
|
|
2004-05-21 20:01:18 -03:00
|
|
|
def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
|
2001-01-14 21:18:21 -04:00
|
|
|
n = len(x)
|
2004-05-21 07:00:15 -03:00
|
|
|
if level <= 0 and n:
|
|
|
|
s = '...'
|
|
|
|
else:
|
|
|
|
newlevel = level - 1
|
|
|
|
repr1 = self.repr1
|
|
|
|
pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
|
|
|
|
if n > maxiter: pieces.append('...')
|
|
|
|
s = ', '.join(pieces)
|
2004-05-21 20:01:18 -03:00
|
|
|
if n == 1 and trail: right = trail + right
|
2004-05-21 07:00:15 -03:00
|
|
|
return '%s%s%s' % (left, s, right)
|
|
|
|
|
|
|
|
def repr_tuple(self, x, level):
|
|
|
|
return self._repr_iterable(x, level, '(', ')', self.maxlist, ',')
|
|
|
|
|
2001-01-14 21:18:21 -04:00
|
|
|
def repr_list(self, x, level):
|
2004-05-21 07:00:15 -03:00
|
|
|
return self._repr_iterable(x, level, '[', ']', self.maxlist)
|
2003-02-05 14:29:34 -04:00
|
|
|
|
|
|
|
def repr_array(self, x, level):
|
|
|
|
header = "array('%s', [" % x.typecode
|
2004-05-21 07:00:15 -03:00
|
|
|
return self._repr_iterable(x, level, header, '])', self.maxarray)
|
|
|
|
|
|
|
|
def repr_set(self, x, level):
|
2004-05-22 16:37:21 -03:00
|
|
|
x = sorted(x)
|
2004-05-21 07:00:15 -03:00
|
|
|
return self._repr_iterable(x, level, 'set([', '])', self.maxset)
|
|
|
|
|
|
|
|
def repr_frozenset(self, x, level):
|
2004-05-22 16:37:21 -03:00
|
|
|
x = sorted(x)
|
2004-05-21 07:00:15 -03:00
|
|
|
return self._repr_iterable(x, level, 'frozenset([', '])',
|
|
|
|
self.maxfrozenset)
|
2003-02-05 14:29:34 -04:00
|
|
|
|
2004-05-21 20:01:18 -03:00
|
|
|
def repr_deque(self, x, level):
|
|
|
|
return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
|
|
|
|
|
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 '{...}'
|
2004-05-21 07:00:15 -03:00
|
|
|
newlevel = level - 1
|
|
|
|
repr1 = self.repr1
|
|
|
|
pieces = []
|
|
|
|
for key in islice(sorted(x), self.maxdict):
|
|
|
|
keyrepr = repr1(key, newlevel)
|
|
|
|
valrepr = repr1(x[key], newlevel)
|
|
|
|
pieces.append('%s: %s' % (keyrepr, valrepr))
|
|
|
|
if n > self.maxdict: pieces.append('...')
|
|
|
|
s = ', '.join(pieces)
|
|
|
|
return '{%s}' % (s,)
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
def repr_str(self, x, level):
|
2004-02-12 13:35:32 -04:00
|
|
|
s = __builtin__.repr(x[:self.maxstring])
|
2001-01-14 21:18:21 -04:00
|
|
|
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)
|
2004-02-12 13:35:32 -04:00
|
|
|
s = __builtin__.repr(x[:i] + x[len(x)-j:])
|
2001-01-14 21:18:21 -04:00
|
|
|
s = s[:i] + '...' + s[len(s)-j:]
|
|
|
|
return s
|
2004-05-21 07:00:15 -03:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
def repr_long(self, x, level):
|
2004-02-12 13:35:32 -04:00
|
|
|
s = __builtin__.repr(x) # XXX Hope this isn't too slow...
|
2001-01-14 21:18:21 -04:00
|
|
|
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
|
2004-05-21 07:00:15 -03:00
|
|
|
|
2001-01-14 21:18:21 -04:00
|
|
|
def repr_instance(self, x, level):
|
|
|
|
try:
|
2004-02-12 13:35:32 -04:00
|
|
|
s = __builtin__.repr(x)
|
2001-01-14 21:18:21 -04:00
|
|
|
# Bugs in x.__repr__() can cause arbitrary
|
|
|
|
# exceptions -- then make up something
|
|
|
|
except:
|
2004-07-01 17:28:47 -03:00
|
|
|
return '<%s instance at %x>' % (x.__class__.__name__, id(x))
|
2001-01-14 21:18:21 -04:00
|
|
|
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
|