Add str, repr and IPython's pretty repr, close #7
This commit is contained in:
parent
834d4dc8a1
commit
2f7e069377
|
@ -60,6 +60,31 @@ class Component(metaclass=ComponentMetaclass, register=False):
|
|||
Useful to perform some checks after parsing.
|
||||
"""
|
||||
|
||||
def __str__(self):
|
||||
return str(self.serialize())
|
||||
|
||||
def __repr__(self):
|
||||
return '{}({})'.format(
|
||||
self.__class__.__name__,
|
||||
', '.join(
|
||||
'{}={!r}'.format(k, v)
|
||||
for k, v in self.serialize().items()
|
||||
),
|
||||
)
|
||||
|
||||
def _repr_pretty_(self, p, cycle):
|
||||
if cycle:
|
||||
p.text('{}(...)'.format(self.__class__.__name__))
|
||||
else:
|
||||
with p.group(2, '{}('.format(self.__class__.__name__), ')'):
|
||||
p.breakable('')
|
||||
for k, v in self.serialize().items():
|
||||
p.text(k)
|
||||
p.text('=')
|
||||
p.pretty(v)
|
||||
p.text(',')
|
||||
p.breakable()
|
||||
|
||||
@classmethod
|
||||
def from_path(cls, path, fmt=FileFormat.YAML):
|
||||
assert isinstance(fmt, FileFormat)
|
||||
|
|
Loading…
Reference in New Issue