saveable_objects.extensions.SaveableWrapper¶
- class saveable_objects.extensions.SaveableWrapper(class_to_wrap: type | None = None, path: str | None = None)[source]¶
Bases:
Generic[T],SaveableObjectA template class for converting a general class to a
SaveableObject. For example a classTcan be made into a new Saveable classSaveableTin any of the following ways:SaveableT = SaveableWrapper[T]; SaveableT = SaveableWrapper(T, path="default_path.pkl") SaveableT = SaveableWrapper(path="default_path.pkl")[T];
A default path for the
SaveableObjectcan be set with thepathargument if parentheses are used.The new class
SaveableTwill inherited from bothTandSaveableObject.Attributes
The file name of the object (without the file extension).
The current file path of the object.
Methods
- classmethod load(path: str, new_path: str | None = None, strict_typing: bool = True) SaveableObject¶
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]¶
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]¶
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]¶
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.
- save(path: str | None = None)¶
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¶
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.