blob: 9893c71cb82b9112cdc6de87aabbaf8d4ca7de04 [file] [log] [blame]
Thomas Wouters1b7f8912007-09-19 03:06:30 +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
Georg Brandl1a3284e2007-12-02 09:40:06 +00005import builtins
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
Guido van Rossuma48a3b42006-04-20 16:07:39 +000021 self.maxother = 30
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:
Guido van Rossuma48a3b42006-04-20 16:07:39 +000034 return self.repr_instance(x, level)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000035
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000036 def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
Raymond Hettingerba6cd362004-05-21 10:00:15 +000037 n = len(x)
38 if level <= 0 and n:
39 s = '...'
40 else:
41 newlevel = level - 1
42 repr1 = self.repr1
43 pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
44 if n > maxiter: pieces.append('...')
45 s = ', '.join(pieces)
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000046 if n == 1 and trail: right = trail + right
Raymond Hettingerba6cd362004-05-21 10:00:15 +000047 return '%s%s%s' % (left, s, right)
48
Tim Peters0c9886d2001-01-15 01:18:21 +000049 def repr_tuple(self, x, level):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000050 return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',')
Raymond Hettingerba6cd362004-05-21 10:00:15 +000051
Tim Peters0c9886d2001-01-15 01:18:21 +000052 def repr_list(self, x, level):
Raymond Hettingerba6cd362004-05-21 10:00:15 +000053 return self._repr_iterable(x, level, '[', ']', self.maxlist)
Tim Peters6ee04802003-02-05 18:29:34 +000054
55 def repr_array(self, x, level):
Tim Peters6ee04802003-02-05 18:29:34 +000056 header = "array('%s', [" % x.typecode
Raymond Hettingerba6cd362004-05-21 10:00:15 +000057 return self._repr_iterable(x, level, header, '])', self.maxarray)
58
59 def repr_set(self, x, level):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000060 x = _possibly_sorted(x)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000061 return self._repr_iterable(x, level, 'set([', '])', self.maxset)
62
63 def repr_frozenset(self, x, level):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000064 x = _possibly_sorted(x)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000065 return self._repr_iterable(x, level, 'frozenset([', '])',
66 self.maxfrozenset)
Tim Peters6ee04802003-02-05 18:29:34 +000067
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000068 def repr_deque(self, x, level):
69 return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
70
Tim Petersa427a2b2001-10-29 22:25:45 +000071 def repr_dict(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +000072 n = len(x)
73 if n == 0: return '{}'
74 if level <= 0: return '{...}'
Raymond Hettingerba6cd362004-05-21 10:00:15 +000075 newlevel = level - 1
76 repr1 = self.repr1
77 pieces = []
Thomas Wouters1b7f8912007-09-19 03:06:30 +000078 for key in islice(_possibly_sorted(x), self.maxdict):
Raymond Hettingerba6cd362004-05-21 10:00:15 +000079 keyrepr = repr1(key, newlevel)
80 valrepr = repr1(x[key], newlevel)
81 pieces.append('%s: %s' % (keyrepr, valrepr))
82 if n > self.maxdict: pieces.append('...')
83 s = ', '.join(pieces)
84 return '{%s}' % (s,)
85
Tim Peters6d6c1a32001-08-02 04:15:00 +000086 def repr_str(self, x, level):
Georg Brandl1a3284e2007-12-02 09:40:06 +000087 s = builtins.repr(x[:self.maxstring])
Tim Peters0c9886d2001-01-15 01:18:21 +000088 if len(s) > self.maxstring:
Guido van Rossum54e54c62001-09-04 19:14:14 +000089 i = max(0, (self.maxstring-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000090 j = max(0, self.maxstring-3-i)
Georg Brandl1a3284e2007-12-02 09:40:06 +000091 s = builtins.repr(x[:i] + x[len(x)-j:])
Tim Peters0c9886d2001-01-15 01:18:21 +000092 s = s[:i] + '...' + s[len(s)-j:]
93 return s
Raymond Hettingerba6cd362004-05-21 10:00:15 +000094
Guido van Rossum4c9695a2007-01-15 00:14:39 +000095 def repr_int(self, x, level):
Georg Brandl1a3284e2007-12-02 09:40:06 +000096 s = builtins.repr(x) # XXX Hope this isn't too slow...
Tim Peters0c9886d2001-01-15 01:18:21 +000097 if len(s) > self.maxlong:
Guido van Rossum54e54c62001-09-04 19:14:14 +000098 i = max(0, (self.maxlong-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +000099 j = max(0, self.maxlong-3-i)
100 s = s[:i] + '...' + s[len(s)-j:]
101 return s
Raymond Hettingerba6cd362004-05-21 10:00:15 +0000102
Tim Peters0c9886d2001-01-15 01:18:21 +0000103 def repr_instance(self, x, level):
104 try:
Georg Brandl1a3284e2007-12-02 09:40:06 +0000105 s = builtins.repr(x)
Tim Peters0c9886d2001-01-15 01:18:21 +0000106 # Bugs in x.__repr__() can cause arbitrary
107 # exceptions -- then make up something
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000108 except Exception:
Fred Drake83a64302004-07-01 20:28:47 +0000109 return '<%s instance at %x>' % (x.__class__.__name__, id(x))
Guido van Rossuma48a3b42006-04-20 16:07:39 +0000110 if len(s) > self.maxother:
111 i = max(0, (self.maxother-3)//2)
112 j = max(0, self.maxother-3-i)
Tim Peters0c9886d2001-01-15 01:18:21 +0000113 s = s[:i] + '...' + s[len(s)-j:]
114 return s
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000115
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000116
117def _possibly_sorted(x):
118 # Since not all sequences of items can be sorted and comparison
119 # functions may raise arbitrary exceptions, return an unsorted
120 # sequence in that case.
121 try:
122 return sorted(x)
123 except Exception:
124 return list(x)
125
Guido van Rossum7bc817d1993-12-17 15:25:27 +0000126aRepr = Repr()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000127repr = aRepr.repr