I/O#
This module contains mixin classes for saving and loading data from files.
Supported formats#
Pickle: save and load Python objects to and from pickle files.
Joblib: save and load Python objects to and from joblib files.
Module contains classes that allow performing I/O operations on objects.
This module defines classes that serve as mixins for other classes. These classes extensions inject methods that allow performing I/O operations on objects.
Classes#
This module defines the following classes:
PicklerMixin
: Mixin class that adds methods for pickling objects.JoblibIOMixin
: Mixin class that adds methods for saving objects as joblib files.
Examples
The following example shows how to use the PicklerMixin
class to
extend other classes, adding methods for pickling objects:
class MyClass(PicklerMixin):pass
my_object = MyClass()
# Save classe instance as pickle file
my_object.to_pickle('my_object.pkl')
# Load class instance from pickle file
MyClass.from_pickle('my_object.pkl')
The same logic applies to the JoblibIOMixin
class.
Notes
All I/O classes from this module implement the saving method, using the to_
prefix, and the loading method, using the from_
prefix.
The loading methods are class methods, so they can be called directly from the
class, without the need to instantiate it first.
- class JoblibIOMixin[source]#
Mixin class for joblib I/O.
This class allows parent objects to perform I/O operations using
joblib
. The methods implemented in this class are:JoblibIOMixin.to_joblib()
: Save the object to a joblib file.JoblibIOMixin.from_joblib()
: Load the object from a joblib file.
- to_joblib(path: str)[source]#
Save the object to a joblib file.
- Parameters:
path (str) –
- Return type:
None
- from_joblib(path: str)[source]#
Load the object from a joblib file.
- Parameters:
path (str) –
- Return type:
Examples
Define a class that inherits from
JoblibIOMixin
: >>> class MyClass(JoblibIOMixin):pass >>> obj = MyClass()Save the object as a
joblib
file: >>> obj.to_joblib(“path/to/file”)Load the object from a
joblib
file: >>> obj.from_joblib(“path/to/file”)- classmethod from_joblib(path)[source]#
Load the object from a
joblib
file.- Parameters:
path (
str
) – Path to thejoblib
file.- Returns:
Object loaded from the
joblib
file.- Return type:
- Raises:
ValueError – If the path is not a file.
- to_joblib(path)[source]#
Save the object to a
joblib
file.- Parameters:
path (
str
) – Path to thejoblib
file.- Raises:
ValueError – If the path refers to a directory.
- Return type:
None
- class PicklerMixin[source]#
Mixin class for pickle I/O.
This class allows parent objects to perform I/O operations using pickle. The methods implemented in this class are:
PicklerMixin.to_pickle()
: Save the object to a pickle file.PicklerMixin.from_pickle()
: Load the object from a pickle file.
Examples
>>> class MyClass(PicklerMixin):pass >>> obj = MyClass()
>>> # To save the object to a pickle file: >>> obj.to_pickle("path/to/file")
>>> # To load the object from a pickle file: >>> obj.from_pickle("path/to/file")
- __getstate__()[source]#
Get the state of the object for pickling.
- Returns:
State of the object for pickling.
- Return type:
Dict[str
,Any]
- __setstate__(state)[source]#
Set the state of the object after unpickling.
- Parameters:
state (
Dict[str
,Any]
) – State of the object after unpickling.
- classmethod from_pickle(path)[source]#
Load the object from a pickle file.
- Parameters:
path (
str
) – Path to the pickle file.- Returns:
Object loaded from the pickle file.
- Return type:
- Raises:
ValueError – If the path is not a file.
- to_pickle(path)[source]#
Save the object to a pickle file.
- Parameters:
path (
str
) – Path to the pickle file.- Raises:
ValueError – If the path refers to a directory.