Skip to content

Commit

Permalink
ProcessListener recreate_from
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Dec 9, 2024
1 parent ce6beae commit 484ae87
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/plumpy/process_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand All @@ -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

Expand Down

0 comments on commit 484ae87

Please sign in to comment.