blob: 3c26cc39a2190ccdeebd6fb6a93738b6f4a50fdd [file] [log] [blame]
Georg Brandlfed6bb72007-09-12 19:00:10 +00001"""Redo the builtin repr() (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
Walter Dörwald70a6b492004-02-12 17:35:32 +00005import __builtin__
Raymond Hettingerba6cd362004-05-21 10:00:15 +00006from itertools import islice
Walter Dörwald70a6b492004-02-12 17:35:32 +00007
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00008class Repr:
Raymond Hettingerba6cd362004-05-21 10:00:15 +00009
Tim Peters0c9886d2001-01-15 01:18:21 +000010 def __init__(self):
11 self.maxlevel = 6
12 self.maxtuple = 6
13 self.maxlist = 6
Tim Peters6ee04802003-02-05 18:29:34 +000014 self.maxarray = 5
Tim Peters0c9886d2001-01-15 01:18:21 +000015 self.maxdict = 4
Raymond Hettingerba6cd362004-05-21 10:00:15 +000016 self.maxset = 6
17 self.maxfrozenset = 6
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000018 self.maxdeque = 6
Tim Peters0c9886d2001-01-15 01:18:21 +000019 self.maxstring = 30
20 self.maxlong = 40
21 self.maxother = 20
Raymond Hettingerba6cd362004-05-21 10:00:15 +000022
Tim Peters0c9886d2001-01-15 01:18:21 +000023 def repr(self, x):
24 return self.repr1(x, self.maxlevel)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000025
Tim Peters0c9886d2001-01-15 01:18:21 +000026 def repr1(self, x, level):
Guido van Rossuma995c912001-08-09 18:56:27 +000027 typename = type(x).__name__
Tim Peters0c9886d2001-01-15 01:18:21 +000028 if ' ' in typename:
Eric S. Raymond6b71e742001-02-09 08:56:30 +000029 parts = typename.split()
30 typename = '_'.join(parts)
Tim Peters0c9886d2001-01-15 01:18:21 +000031 if hasattr(self, 'repr_' + typename):
32 return getattr(self, 'repr_' + typename)(x, level)
33 else:
Walter Dörwald70a6b492004-02-12 17:35:32 +000034 s = __builtin__.repr(x)
Tim Peters0c9886d2001-01-15 01:18:21 +000035 if len(s) > self.maxother:
Guido van Rossum6fd0f0a2001-09-05 02:27:04 +000036 i = max(0, (self.maxother-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000037 j = max(0, self.maxother-3-i)
38 s = s[:i] + '...' + s[len(s)-j:]
39 return s
Raymond Hettingerba6cd362004-05-21 10:00:15 +000040
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000041 def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
Raymond Hettingerba6cd362004-05-21 10:00:15 +000042 n = len(x)
43 if level <= 0 and n:
44 s = '...'
45 else:
46 newlevel = level - 1
47 repr1 = self.repr1
48 pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
49 if n > maxiter: pieces.append('...')
50 s = ', '.join(pieces)
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000051 if n == 1 and trail: right = trail + right
Raymond Hettingerba6cd362004-05-21 10:00:15 +000052 return '%s%s%s' % (left, s, right)
53
Tim Peters0c9886d2001-01-15 01:18:21 +000054 def repr_tuple(self, x, level):
Neal Norwitze99cb232007-06-11 07:34:07 +000055 return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',')
Raymond Hettingerba6cd362004-05-21 10:00:15 +000056
Tim Peters0c9886d2001-01-15 01:18:21 +000057 def repr_list(self, x, level):
Raymond Hettingerba6cd362004-05-21 10:00:15 +000058 return self._repr_iterable(x, level, '[', ']', self.maxlist)
Tim Peters6ee04802003-02-05 18:29:34 +000059
60 def repr_array(self, x, level):
Tim Peters6ee04802003-02-05 18:29:34 +000061 header = "array('%s', [" % x.typecode
Raymond Hettingerba6cd362004-05-21 10:00:15 +000062 return self._repr_iterable(x, level, header, '])', self.maxarray)
63
64 def repr_set(self, x, level):
Georg Brandlfed6bb72007-09-12 19:00:10 +000065 x = _possibly_sorted(x)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000066 return self._repr_iterable(x, level, 'set([', '])', self.maxset)
67
68 def repr_frozenset(self, x, level):
Georg Brandlfed6bb72007-09-12 19:00:10 +000069 x = _possibly_sorted(x)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000070 return self._repr_iterable(x, level, 'frozenset([', '])',
71 self.maxfrozenset)
Tim Peters6ee04802003-02-05 18:29:34 +000072
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000073 def repr_deque(self, x, level):
74 return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
75
Tim Petersa427a2b2001-10-29 22:25:45 +000076 def repr_dict(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +000077 n = len(x)
78 if n == 0: return '{}'
79 if level <= 0: return '{...}'
Raymond Hettingerba6cd362004-05-21 10:00:15 +000080 newlevel = level - 1
81 repr1 = self.repr1
82 pieces = []
Georg Brandlfed6bb72007-09-12 19:00:10 +000083 for key in islice(_possibly_sorted(x), self.maxdict):
Raymond Hettingerba6cd362004-05-21 10:00:15 +000084 keyrepr = repr1(key, newlevel)
85 valrepr = repr1(x[key], newlevel)
86 pieces.append('%s: %s' % (keyrepr, valrepr))
87 if n > self.maxdict: pieces.append('...')
88 s = ', '.join(pieces)
89 return '{%s}' % (s,)
90
Tim Peters6d6c1a32001-08-02 04:15:00 +000091 def repr_str(self, x, level):
Walter Dörwald70a6b492004-02-12 17:35:32 +000092 s = __builtin__.repr(x[:self.maxstring])
Tim Peters0c9886d2001-01-15 01:18:21 +000093 if len(s) > self.maxstring:
Guido van Rossum54e54c62001-09-04 19:14:14 +000094 i = max(0, (self.maxstring-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000095 j = max(0, self.maxstring-3-i)
Walter Dörwald70a6b492004-02-12 17:35:32 +000096 s = __builtin__.repr(x[:i] + x[len(x)-j:])
Tim Peters0c9886d2001-01-15 01:18:21 +000097 s = s[:i] + '...' + s[len(s)-j:]
98 return s
Raymond Hettingerba6cd362004-05-21 10:00:15 +000099
Tim Peters6d6c1a32001-08-02 04:15:00 +0000100 def repr_long(self, x, level):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000101 s = __builtin__.repr(x) # XXX Hope this isn't too slow...
Tim Peters0c9886d2001-01-15 01:18:21 +0000102 if len(s) > self.maxlong:
Guido van Rossum54e54c62001-09-04 19:14:14 +0000103 i = max(0, (self.maxlong-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +0000104 j = max(0, self.maxlong-3-i)
105 s = s[:i] + '...' + s[len(s)-j:]
106 return s
Raymond Hettingerba6cd362004-05-21 10:00:15 +0000107
Tim Peters0c9886d2001-01-15 01:18:21 +0000108 def repr_instance(self, x, level):
109 try:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000110 s = __builtin__.repr(x)
Tim Peters0c9886d2001-01-15 01:18:21 +0000111 # Bugs in x.__repr__() can cause arbitrary
112 # exceptions -- then make up something
Georg Brandlfed6bb72007-09-12 19:00:10 +0000113 except Exception:
Fred Drake83a64302004-07-01 20:28:47 +0000114 return '<%s instance at %x>' % (x.__class__.__name__, id(x))
Tim Peters0c9886d2001-01-15 01:18:21 +0000115 if len(s) > self.maxstring:
Guido van Rossum54e54c62001-09-04 19:14:14 +0000116 i = max(0, (self.maxstring-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +0000117 j = max(0, self.maxstring-3-i)
118 s = s[:i] + '...' + s[len(s)-j:]
119 return s
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000120
Georg Brandlfed6bb72007-09-12 19:00:10 +0000121
122def _possibly_sorted(x):
123 # Since not all sequences of items can be sorted and comparison
124 # functions may raise arbitrary exceptions, return an unsorted
125 # sequence in that case.
126 try:
127 return sorted(x)
128 except Exception:
129 return list(x)
130
Guido van Rossum7bc817d1993-12-17 15:25:27 +0000131aRepr = Repr()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000132repr = aRepr.repr