Better __repr__() for data model

This greatly enhances debugability of data model objects as repr() and
str() of objects contain now all attributes.

TODO: Check for secret attributes
Change-Id: I2babd600516cfe98673627c68e97cb581ee54834
This commit is contained in:
Tom Weininger 2023-04-27 10:55:15 +02:00
parent 53200266b3
commit cc7797ed33
1 changed files with 5 additions and 0 deletions

View File

@ -76,6 +76,11 @@ class BaseDataModel():
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
attrs = " ".join(
"{}={!r}".format(k, v) for k, v in self.__dict__.items())
return f"{self.__class__.__name__}({attrs})"
@classmethod
def from_dict(cls, dict):
return cls(**dict)