Merge "Provide usable __repr__ for serializable objects"
This commit is contained in:
@@ -24,6 +24,11 @@ class Serializable(object):
|
|||||||
"""Turn this object into a dict."""
|
"""Turn this object into a dict."""
|
||||||
return dict((f, getattr(self, f)) for f in self.serializable_fields)
|
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):
|
class SerializableComparable(Serializable):
|
||||||
"""A Serializable class which supports some comparison operators
|
"""A Serializable class which supports some comparison operators
|
||||||
|
@@ -43,6 +43,8 @@ class TestSerializable(base.IronicAgentTest):
|
|||||||
expected = {'jack': 'hello', 'jill': 'world'}
|
expected = {'jack': 'hello', 'jill': 'world'}
|
||||||
obj = SerializableTesting('hello', 'world')
|
obj = SerializableTesting('hello', 'world')
|
||||||
self.assertEqual(expected, obj.serialize())
|
self.assertEqual(expected, obj.serialize())
|
||||||
|
self.assertEqual(
|
||||||
|
"<SerializableTesting jack='hello' jill='world'>", repr(obj))
|
||||||
|
|
||||||
|
|
||||||
class TestSerializableComparable(base.IronicAgentTest):
|
class TestSerializableComparable(base.IronicAgentTest):
|
||||||
|
Reference in New Issue
Block a user