2016-05-31 05:16:31 +00:00
|
|
|
import abc
|
|
|
|
|
2016-05-31 07:58:28 +00:00
|
|
|
|
2016-10-17 02:15:22 +00:00
|
|
|
class Serializable(metaclass=abc.ABCMeta):
|
2016-05-31 05:16:31 +00:00
|
|
|
"""
|
|
|
|
Abstract Base Class that defines an API to save an object's state and restore it later on.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@abc.abstractmethod
|
|
|
|
def from_state(cls, state):
|
|
|
|
"""
|
|
|
|
Create a new object from the given state.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def get_state(self):
|
|
|
|
"""
|
|
|
|
Retrieve object state.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def set_state(self, state):
|
|
|
|
"""
|
|
|
|
Set object state to the given state.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
return self.from_state(self.get_state())
|