1 | n/a | """Redo the builtin repr() (representation) but with limits on most sizes.""" |
---|
2 | n/a | |
---|
3 | n/a | __all__ = ["Repr", "repr", "recursive_repr"] |
---|
4 | n/a | |
---|
5 | n/a | import builtins |
---|
6 | n/a | from itertools import islice |
---|
7 | n/a | try: |
---|
8 | n/a | from _thread import get_ident |
---|
9 | n/a | except ImportError: |
---|
10 | n/a | from _dummy_thread import get_ident |
---|
11 | n/a | |
---|
12 | n/a | def recursive_repr(fillvalue='...'): |
---|
13 | n/a | 'Decorator to make a repr function return fillvalue for a recursive call' |
---|
14 | n/a | |
---|
15 | n/a | def decorating_function(user_function): |
---|
16 | n/a | repr_running = set() |
---|
17 | n/a | |
---|
18 | n/a | def wrapper(self): |
---|
19 | n/a | key = id(self), get_ident() |
---|
20 | n/a | if key in repr_running: |
---|
21 | n/a | return fillvalue |
---|
22 | n/a | repr_running.add(key) |
---|
23 | n/a | try: |
---|
24 | n/a | result = user_function(self) |
---|
25 | n/a | finally: |
---|
26 | n/a | repr_running.discard(key) |
---|
27 | n/a | return result |
---|
28 | n/a | |
---|
29 | n/a | # Can't use functools.wraps() here because of bootstrap issues |
---|
30 | n/a | wrapper.__module__ = getattr(user_function, '__module__') |
---|
31 | n/a | wrapper.__doc__ = getattr(user_function, '__doc__') |
---|
32 | n/a | wrapper.__name__ = getattr(user_function, '__name__') |
---|
33 | n/a | wrapper.__qualname__ = getattr(user_function, '__qualname__') |
---|
34 | n/a | wrapper.__annotations__ = getattr(user_function, '__annotations__', {}) |
---|
35 | n/a | return wrapper |
---|
36 | n/a | |
---|
37 | n/a | return decorating_function |
---|
38 | n/a | |
---|
39 | n/a | class Repr: |
---|
40 | n/a | |
---|
41 | n/a | def __init__(self): |
---|
42 | n/a | self.maxlevel = 6 |
---|
43 | n/a | self.maxtuple = 6 |
---|
44 | n/a | self.maxlist = 6 |
---|
45 | n/a | self.maxarray = 5 |
---|
46 | n/a | self.maxdict = 4 |
---|
47 | n/a | self.maxset = 6 |
---|
48 | n/a | self.maxfrozenset = 6 |
---|
49 | n/a | self.maxdeque = 6 |
---|
50 | n/a | self.maxstring = 30 |
---|
51 | n/a | self.maxlong = 40 |
---|
52 | n/a | self.maxother = 30 |
---|
53 | n/a | |
---|
54 | n/a | def repr(self, x): |
---|
55 | n/a | return self.repr1(x, self.maxlevel) |
---|
56 | n/a | |
---|
57 | n/a | def repr1(self, x, level): |
---|
58 | n/a | typename = type(x).__name__ |
---|
59 | n/a | if ' ' in typename: |
---|
60 | n/a | parts = typename.split() |
---|
61 | n/a | typename = '_'.join(parts) |
---|
62 | n/a | if hasattr(self, 'repr_' + typename): |
---|
63 | n/a | return getattr(self, 'repr_' + typename)(x, level) |
---|
64 | n/a | else: |
---|
65 | n/a | return self.repr_instance(x, level) |
---|
66 | n/a | |
---|
67 | n/a | def _repr_iterable(self, x, level, left, right, maxiter, trail=''): |
---|
68 | n/a | n = len(x) |
---|
69 | n/a | if level <= 0 and n: |
---|
70 | n/a | s = '...' |
---|
71 | n/a | else: |
---|
72 | n/a | newlevel = level - 1 |
---|
73 | n/a | repr1 = self.repr1 |
---|
74 | n/a | pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)] |
---|
75 | n/a | if n > maxiter: pieces.append('...') |
---|
76 | n/a | s = ', '.join(pieces) |
---|
77 | n/a | if n == 1 and trail: right = trail + right |
---|
78 | n/a | return '%s%s%s' % (left, s, right) |
---|
79 | n/a | |
---|
80 | n/a | def repr_tuple(self, x, level): |
---|
81 | n/a | return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',') |
---|
82 | n/a | |
---|
83 | n/a | def repr_list(self, x, level): |
---|
84 | n/a | return self._repr_iterable(x, level, '[', ']', self.maxlist) |
---|
85 | n/a | |
---|
86 | n/a | def repr_array(self, x, level): |
---|
87 | n/a | if not x: |
---|
88 | n/a | return "array('%s')" % x.typecode |
---|
89 | n/a | header = "array('%s', [" % x.typecode |
---|
90 | n/a | return self._repr_iterable(x, level, header, '])', self.maxarray) |
---|
91 | n/a | |
---|
92 | n/a | def repr_set(self, x, level): |
---|
93 | n/a | if not x: |
---|
94 | n/a | return 'set()' |
---|
95 | n/a | x = _possibly_sorted(x) |
---|
96 | n/a | return self._repr_iterable(x, level, '{', '}', self.maxset) |
---|
97 | n/a | |
---|
98 | n/a | def repr_frozenset(self, x, level): |
---|
99 | n/a | if not x: |
---|
100 | n/a | return 'frozenset()' |
---|
101 | n/a | x = _possibly_sorted(x) |
---|
102 | n/a | return self._repr_iterable(x, level, 'frozenset({', '})', |
---|
103 | n/a | self.maxfrozenset) |
---|
104 | n/a | |
---|
105 | n/a | def repr_deque(self, x, level): |
---|
106 | n/a | return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque) |
---|
107 | n/a | |
---|
108 | n/a | def repr_dict(self, x, level): |
---|
109 | n/a | n = len(x) |
---|
110 | n/a | if n == 0: return '{}' |
---|
111 | n/a | if level <= 0: return '{...}' |
---|
112 | n/a | newlevel = level - 1 |
---|
113 | n/a | repr1 = self.repr1 |
---|
114 | n/a | pieces = [] |
---|
115 | n/a | for key in islice(_possibly_sorted(x), self.maxdict): |
---|
116 | n/a | keyrepr = repr1(key, newlevel) |
---|
117 | n/a | valrepr = repr1(x[key], newlevel) |
---|
118 | n/a | pieces.append('%s: %s' % (keyrepr, valrepr)) |
---|
119 | n/a | if n > self.maxdict: pieces.append('...') |
---|
120 | n/a | s = ', '.join(pieces) |
---|
121 | n/a | return '{%s}' % (s,) |
---|
122 | n/a | |
---|
123 | n/a | def repr_str(self, x, level): |
---|
124 | n/a | s = builtins.repr(x[:self.maxstring]) |
---|
125 | n/a | if len(s) > self.maxstring: |
---|
126 | n/a | i = max(0, (self.maxstring-3)//2) |
---|
127 | n/a | j = max(0, self.maxstring-3-i) |
---|
128 | n/a | s = builtins.repr(x[:i] + x[len(x)-j:]) |
---|
129 | n/a | s = s[:i] + '...' + s[len(s)-j:] |
---|
130 | n/a | return s |
---|
131 | n/a | |
---|
132 | n/a | def repr_int(self, x, level): |
---|
133 | n/a | s = builtins.repr(x) # XXX Hope this isn't too slow... |
---|
134 | n/a | if len(s) > self.maxlong: |
---|
135 | n/a | i = max(0, (self.maxlong-3)//2) |
---|
136 | n/a | j = max(0, self.maxlong-3-i) |
---|
137 | n/a | s = s[:i] + '...' + s[len(s)-j:] |
---|
138 | n/a | return s |
---|
139 | n/a | |
---|
140 | n/a | def repr_instance(self, x, level): |
---|
141 | n/a | try: |
---|
142 | n/a | s = builtins.repr(x) |
---|
143 | n/a | # Bugs in x.__repr__() can cause arbitrary |
---|
144 | n/a | # exceptions -- then make up something |
---|
145 | n/a | except Exception: |
---|
146 | n/a | return '<%s instance at %#x>' % (x.__class__.__name__, id(x)) |
---|
147 | n/a | if len(s) > self.maxother: |
---|
148 | n/a | i = max(0, (self.maxother-3)//2) |
---|
149 | n/a | j = max(0, self.maxother-3-i) |
---|
150 | n/a | s = s[:i] + '...' + s[len(s)-j:] |
---|
151 | n/a | return s |
---|
152 | n/a | |
---|
153 | n/a | |
---|
154 | n/a | def _possibly_sorted(x): |
---|
155 | n/a | # Since not all sequences of items can be sorted and comparison |
---|
156 | n/a | # functions may raise arbitrary exceptions, return an unsorted |
---|
157 | n/a | # sequence in that case. |
---|
158 | n/a | try: |
---|
159 | n/a | return sorted(x) |
---|
160 | n/a | except Exception: |
---|
161 | n/a | return list(x) |
---|
162 | n/a | |
---|
163 | n/a | aRepr = Repr() |
---|
164 | n/a | repr = aRepr.repr |
---|