Merge "Provide usable __repr__ for serializable objects"

This commit is contained in:
Zuul
2025-07-22 01:26:55 +00:00
committed by Gerrit Code Review
2 changed files with 7 additions and 0 deletions

View File

@@ -24,6 +24,11 @@ class Serializable(object):
"""Turn this object into a dict."""
return dict((f, getattr(self, f)) for f in self.serializable_fields)
def __repr__(self):
fields = " ".join(f"{f}=" + repr(getattr(self, f))
for f in self.serializable_fields)
return f"<{self.__class__.__name__} {fields}>"
class SerializableComparable(Serializable):
"""A Serializable class which supports some comparison operators

View File

@@ -43,6 +43,8 @@ class TestSerializable(base.IronicAgentTest):
expected = {'jack': 'hello', 'jill': 'world'}
obj = SerializableTesting('hello', 'world')
self.assertEqual(expected, obj.serialize())
self.assertEqual(
"<SerializableTesting jack='hello' jill='world'>", repr(obj))
class TestSerializableComparable(base.IronicAgentTest):