forked from ManageIQ/integration_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty.py
36 lines (26 loc) · 968 Bytes
/
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
def _name(o):
cls = o.__class__
return "{}.{}".format(getattr(cls, '__module__', "module"),
getattr(cls, '__name__', "name"))
def attr_repr(o, attr):
"""Return the string repr of the attribute attr on the object o"""
try:
return repr(getattr(o, attr, None))
except BaseException:
return None
def pretty_repr(attrs, o):
pairs = zip(attrs, [attr_repr(o, attr) for attr in attrs])
return "<{} {}>".format(_name(o),
", ".join(["{}={}".format(i[0], i[1]) for i in pairs]))
def pr_obj(attrs):
def x(o):
return pretty_repr(attrs, o)
return x
class Pretty(object):
"""A mixin that prints repr as <MyClass field1=..., field2=...>. The
fields that will be printed should be stored in the class's
pretty_attrs attribute (none by default).
"""
pretty_attrs = []
def __repr__(self):
return pretty_repr(self.pretty_attrs, self)