Add str, repr and IPython's pretty repr, close #7

This commit is contained in:
Lucidiot 2019-09-30 21:27:53 +02:00
parent 834d4dc8a1
commit 2f7e069377
Signed by: lucidiot
GPG Key ID: 3358C1CA6906FB8D
1 changed files with 25 additions and 0 deletions

View File

@ -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)