Skip to content

Saving

Saving a VIPicklable object

VIPicklable objects have a save method for saving an object instance :

>>> class MyClass(VIPicklable):
...    PICKLE_BLACKLIST = ["unpicklable_attribute"]
...
...    def __init__(self):
...        self.unpicklable_attribute = "do_not_pickle"
...
>>> obj = MyClass()
>>> obj.save("folder")
VIPicklable.save

Save the object instance in a dedicated directory.

Parameters:

Name Type Description Default
path Union[str, Path]

Path to the directory

required
pickle_dump_kwargs dict

kwargs to be passed to pickle.dump method. Defaults to None.

None
json_dump_kwargs dict

kwargs to be passed to json.dump method. Defaults to None.

None
overwrite bool

If True, overwrite the folder if it exists.

True
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/vipickle/mixin.py
def save(
    self,
    path: Union[str, Path],
    pickle_dump_kwargs: dict = None,
    json_dump_kwargs: dict = None,
    overwrite: bool = True,
):
    """Save the object instance in a dedicated directory.

    Args:
        path (Union[str, Path]): Path to the directory
        pickle_dump_kwargs (dict, optional): kwargs to be passed to
            pickle.dump method. Defaults to None.
        json_dump_kwargs (dict, optional): kwargs to be passed to json.dump method.
            Defaults to None.
        overwrite (bool, optional): If True, overwrite the folder if it exists.
    """
    # Before save hook
    self.before_save()

    path = create_folder(path)

    if pickle_dump_kwargs is None:
        pickle_dump_kwargs = {}

    if json_dump_kwargs is None:
        json_dump_kwargs = {}

    self.save_instance(path, overwrite=overwrite, **json_dump_kwargs)
    self.save_config(path, overwrite=overwrite, **json_dump_kwargs)
    self.save_pickle_blacklisted(path, overwrite=overwrite)

    # After save hook
    self.after_save()

Save hooks

Under the hood, the save method does the following :

stateDiagram-v2
    [*] --> self.before_save()
    self.before_save() --> self.save_instance()
    self.save_instance() --> self.save_config()
    self.save_config() --> self.save_pickle_blacklisted()
    self.save_pickle_blacklisted() --> self.after_save()
    self.after_save() --> [*]

One can redefine any of these methods to personnalise the saving process :