From 484ae879c3ac2e0a80e096621373267e80d1a681 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Mon, 9 Dec 2024 22:07:15 +0100 Subject: [PATCH] ProcessListener recreate_from --- src/plumpy/process_listener.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/plumpy/process_listener.py b/src/plumpy/process_listener.py index 8e1acf94..166a811a 100644 --- a/src/plumpy/process_listener.py +++ b/src/plumpy/process_listener.py @@ -3,7 +3,8 @@ from typing import TYPE_CHECKING, Any, Dict, Optional from . import persistence -from .utils import SAVED_STATE_TYPE, protected +from .utils import SAVED_STATE_TYPE +from plumpy.persistence import LoadSaveContext, _ensure_object_loader __all__ = ['ProcessListener'] @@ -22,12 +23,21 @@ def __init__(self) -> None: def init(self, **kwargs: Any) -> None: self._params = kwargs - @protected - def load_instance_state( - self, saved_state: SAVED_STATE_TYPE, load_context: Optional[persistence.LoadSaveContext] - ) -> None: - super().load_instance_state(saved_state, load_context) - self.init(**saved_state['_params']) + @classmethod + def recreate_from(cls, saved_state: SAVED_STATE_TYPE, load_context: Optional[LoadSaveContext] = None) -> 'Savable': + """ + Recreate a :class:`Savable` from a saved state using an optional load context. + + :param saved_state: The saved state + :param load_context: An optional load context + + :return: The recreated instance + + """ + load_context = _ensure_object_loader(load_context, saved_state) + obj = cls.__new__(cls) + obj.init(**saved_state['_params']) + return obj # endregion