saveable_objects.SaveableObject¶
- class saveable_objects.SaveableObject(*args, **kwargs)[source]¶
Bases:
objectA utility class for saving objects to pickles and checkpointing.
Attributes
The file name of the object (without the file extension).
The current file path of the object.
Methods
Initialises an instance of the
SaveableObject.Loads a pickled instance.
Attempts to load from a specified path.
Attempts to
load()from a specified path.Pickles the current instance.
Attempts to
load()from the specified path.Pickles the current instance and retains the saved arguments if they exist.
- classmethod load(path: str, new_path: str | None = None, strict_typing: bool = True) SaveableObject[source]¶
Loads a pickled instance.
- Parameters:
path (str) – The path of the pickle.
new_path (str, optional) – The path to replace the previous path with. If
Nonethe path is not replaced. By defaultNone.strict_typing (bool, optional) – If
Truethen the loaded instance must be an instance of cls. By defaultTrue.
- Returns:
The loaded instance.
- Return type:
- Raises:
TypeError – If strict_typing and the loaded instance is not an instance of cls.
Notes
strict_typing=Trueacts as a safety guard. Settingstrict_typing=Falsemay increase the probability of unexpected or uncaught errors.
- classmethod loadif(*args, **kwargs) Tuple[SaveableObject, bool][source]¶
Attempts to load from a specified path. If the loading fails or no path is specified then a new instance of the object is generated with the specified *args and **kwargs.
- Parameters:
- Returns:
The loaded or initialised instance followed by
Trueif the instance was loaded andFalseif the instance was initialised.- Return type:
(SaveableObject, bool)
- classmethod loadifparams(*args, dependencies: dict = {}, **kwargs) Tuple[SaveableObject, bool][source]¶
Attempts to
load()from a specified path. If the loading fails or no path is specified or the parameters do not match the saved parameters then a new instance of the object is generated with the specified *args and **kwargs.- Parameters:
*args – The arguments to pass to the initialisation on a failed
load().path (str, optional) – The path of the pickle, by default the parameter is not specified.
dependencies (dict, optional, must be specified as a keyword argument) – A dictionary of additional dependencies to check.
**kwargs – The keyword arguments to pass to the initialisation on a failed
load().
- Returns:
The loaded or initialised instance followed by
Trueif the instance was loaded andFalseif the instance was initialised.- Return type:
(SaveableObject, bool)
- classmethod tryload(path: str | None, new_path: str | None = None, strict_typing: bool = True) SaveableObject | Literal[False][source]¶
Attempts to
load()from the specified path. If the loading fails thenFalseis returned.- Parameters:
path (str, optional) – The path of the pickle. If
NonethenFalseis returned.new_path (str, optional) – The path to replace the previous path with. If
Nonethe path is not replaced. By defaultNone.strict_typing (bool, optional) – If
Truethen the loaded instance must be an instance of cls. By defaultTrue.
- Returns:
If succeeded the loaded instance, else False.
- Return type:
SaveableObject | Literal[False]
Notes
strict_typing=Trueacts as a safety guard. Settingstrict_typing=Falsemay increase the probability of unexpected or uncaught errors.
- __init__(path: str | None = None)[source]¶
Initialises an instance of the
SaveableObject. If a path is specified the saveable object is automatically saved after initialisation.- Parameters:
path (str, optional) – The file
pathto save the object to. IfNonethen the object is not saved. By defaultNone.
Notes
If no file extension is provided for path then the class name and the
.pklextension are appended to the file name.
- save(path: str | None = None)[source]¶
Pickles the current instance.
- Parameters:
path (str, optional) – The path to pickle the instance to. If
Noneis specified then the attributepathis used instead. By defaultNone.- Raises:
ValueError – Raised if no path specified either by the parameter path or the attribute
path.
Notes
If no file extension is provided then the class name and the
.pklextension are appended to the file name.
- update_save(path: str | None = None) bool[source]¶
Pickles the current instance and retains the saved arguments if they exist.
- Parameters:
path (str, optional) – The path to pickle the instance to. If
Noneis specified then the attributepathis used instead. By defaultNone.- Returns:
Trueif there was an argument pickle to retain.Falseif there was not an argument pickle to retain.- Return type:
bool
- Raises:
ValueError – Raised if no path specified.
Notes
If no file extension is provided then the class name and the
.pklextension are appended to the file name.
- property name: str | None¶
The file name of the object (without the file extension). Note that name is read only.
- property path: str | None¶
The current file path of the object.
Notes
On setting the value, if no file extension is provided then the class name and the
.pklextension are appended to the file name.