-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.py
45 lines (40 loc) · 1.02 KB
/
pretty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Pretty. Base class for a simple pretty, properly indented representation of a class.
Example:
>>> class K(Pretty):
... def __init__(self):
... self.a = 5
... self.b = 6
...
>>> K()
K(
a=5,
b=6
)
"""
from typing import Set
class Pretty:
"""
Base class for a pretty, properly indented __repr__ method.
"""
__current_indent = 0
__hidden__: Set[str] = set()
def __repr__(self):
indent = self.__current_indent * 3 * ' '
if not self.__dict__:
return '%s()' % type(self).__name__
Pretty.__current_indent += 1
inner = ',\n'.join(
' %s%s=%r' % (indent, k, v)
if k not in self.__hidden__
else ' %s%s=...' % (indent, k)
for k, v in self.__dict__.items()
)
pattern = '%s(\n%s\n%s)'
result = pattern % (
type(self).__name__,
inner,
indent,
)
Pretty.__current_indent -= 1
return result