aurora-multiphysics / cyclops

Cyclops is a sensor suite optimisation program, designed for fusion engineering experiments.
GNU Lesser General Public License v2.1
2 stars 1 forks source link

Replace PickleManager with pickle methods on classes #20

Open lukethehuman opened 12 months ago

lukethehuman commented 12 months ago

Suggestion: replace PickleManager class with pickling methods assigned to classes. Also consider which classes require serialisation i.e. top level objects may contain lower level objects. Ideally use inheritance to avoid multiple instances of the following code block.

import dill

class MyClass:
"""A class which can be pickled and unpickled."""

    def pickle(self, filepath):
        """Save object to file."""
        with open(filepath, "wb") as file:
            dill.dump(self, file)

    @classmethod
    def unpickle(cls, filepath):
        with open(filepath, "rb") as f:
            obj = dill.load(f)
        if isinstance(obj, cls):
            return obj
        raise TypeError(
            f"Unpickled object is not an instance of {cls.__name__}."
        )