Skip to content

Commit

Permalink
Added renamed() method to Serializable.
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Prediger <lukas.prediger@rwth-aachen.de>
  • Loading branch information
lumip committed Jul 12, 2018
1 parent dfc72ef commit f2cd50d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions qctoolkit/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ def deserialize(cls, serializer: Optional['Serializer']=None, **kwargs) -> 'Seri

return cls(**kwargs)

def renamed(self, new_identifier: str, registry: Optional[Dict]=None) -> 'Serializable':
"""Returns a copy of the Serializable with its identifier set to new_identifier."""
data = self.get_serialization_data()
data.pop(Serializable.type_identifier_name)
data.pop(Serializable.identifier_name)
return self.deserialize(registry=registry, identifier=new_identifier, **data)


class AnonymousSerializable:
"""Any object that can be converted into a serialized representation for storage and back which NEVER has an
Expand Down
6 changes: 6 additions & 0 deletions tests/serialization_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def test_duplication_error(self):
with self.assertRaises(RuntimeError):
self.make_instance('blub', registry=registry)

def test_renamed(self) -> None:
registry = dict()
instance = self.make_instance('hugo', registry=registry)
renamed_instance = instance.renamed('ilse', registry=registry)
self.assert_equal_instance_except_id(instance, renamed_instance)


class DummySerializableTests(SerializableTests, unittest.TestCase):
@property
Expand Down

0 comments on commit f2cd50d

Please sign in to comment.