Skip to content

Serializers

Module containing all Serializers

Serializer #

Bases: ABC

Abstract class from which all Serializer inherit from.

Source code in scrat/serializer/base.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Serializer(ABC):
    """
    Abstract class from which all Serializer inherit from.
    """

    @abstractmethod
    def dump(self, obj: T.Any, path: Path):
        """
        Save an object to disk.

        Parameters
        ----------
        obj
            The result to save.
        path
            The target location in the filesystem.
        """
        return NotImplemented

    @abstractmethod
    def load(self, path: Path) -> T.Any:
        """
        Load a saved object from disk.

        Parameters
        ----------
        path
            Location of the stored result.

        Returns
        -------
            The object loaded into memory.
        """
        return NotImplemented

dump(obj, path) abstractmethod #

Save an object to disk.

Parameters:

Name Type Description Default
obj Any

The result to save.

required
path Path

The target location in the filesystem.

required
Source code in scrat/serializer/base.py
11
12
13
14
15
16
17
18
19
20
21
22
23
@abstractmethod
def dump(self, obj: T.Any, path: Path):
    """
    Save an object to disk.

    Parameters
    ----------
    obj
        The result to save.
    path
        The target location in the filesystem.
    """
    return NotImplemented

load(path) abstractmethod #

Load a saved object from disk.

Parameters:

Name Type Description Default
path Path

Location of the stored result.

required

Returns:

Type Description
The object loaded into memory.
Source code in scrat/serializer/base.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@abstractmethod
def load(self, path: Path) -> T.Any:
    """
    Load a saved object from disk.

    Parameters
    ----------
    path
        Location of the stored result.

    Returns
    -------
        The object loaded into memory.
    """
    return NotImplemented

DillSerializer #

Serializer using dill.

In order to use this Serializer dill needs to be installed.

Parameters:

Name Type Description Default
dump_kwargs Optional[Dict[str, Any]]

extra arguments for dill.dump, by default None

None
load_kwargs Optional[Dict[str, Any]]

extra arguments for dill.load, by default None

None
Source code in scrat/serializer/dill.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class DillSerializer:
    """
    Serializer using dill.

    In order to use this Serializer dill needs to be installed.

    Parameters
    ----------
    dump_kwargs
        extra arguments for dill.dump, by default None
    load_kwargs
        extra arguments for dill.load, by default None
    """

    def __init__(
        self,
        dump_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
        load_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
    ) -> None:
        # test that dill is installed
        import dill  # noqa: F401

        self.dump_kwargs = dump_kwargs if dump_kwargs is not None else {}
        self.load_kwargs = load_kwargs if load_kwargs is not None else {}

    def dump(self, obj: T.Any, path: Path):
        import dill

        with open(path, "wb") as f:
            dill.dump(obj, f, **self.dump_kwargs)

    def load(self, path: Path) -> T.Any:
        import dill

        with open(path, "rb") as f:
            return dill.load(f, **self.load_kwargs)

JsonSerializer #

Serializer that uses json from the python standard library.

Parameters:

Name Type Description Default
dump_kwargs Optional[Dict[str, Any]]

Extra arguments for json.dump, by default None

None
load_kwargs Optional[Dict[str, Any]]

Extra arguments for json.load, by default None

None
Source code in scrat/serializer/json.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class JsonSerializer:
    """
    Serializer that uses json from the python standard library.

    Parameters
    ----------
    dump_kwargs
        Extra arguments for json.dump, by default None
    load_kwargs
        Extra arguments for json.load, by default None
    """

    def __init__(
        self,
        dump_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
        load_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
    ) -> None:
        self.dump_kwargs = dump_kwargs if dump_kwargs is not None else {}
        self.load_kwargs = load_kwargs if load_kwargs is not None else {}

    def dump(self, obj: T.Any, path: Path):
        with open(path, "w") as f:
            json.dump(obj, f, **self.dump_kwargs)

    def load(self, path: Path) -> T.Any:
        with open(path, "r") as f:
            return json.load(f, **self.load_kwargs)

NumpySerializer #

Serializer for numpy arrays.

In order to use this Serializer numpy needs to be installed.

Parameters:

Name Type Description Default
save_kwargs Optional[Dict[str, Any]]

Extra arguments for numpy.save, by default None

None
load_kwargs Optional[Dict[str, Any]]

Extra arguments for numpy.load, by default None

None
Source code in scrat/serializer/numpy.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class NumpySerializer:
    """
    Serializer for numpy arrays.

    In order to use this Serializer numpy needs to be installed.


    Parameters
    ----------
    save_kwargs
        Extra arguments for numpy.save, by default None
    load_kwargs
        Extra arguments for numpy.load, by default None
    """

    def __init__(
        self,
        save_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
        load_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
    ) -> None:
        # test that numpy is installed
        import numpy  # noqa: F401

        self.save_kwargs = save_kwargs if save_kwargs is not None else {}
        self.load_kwargs = load_kwargs if load_kwargs is not None else {}

    def dump(self, obj: "np.ndarray", path: Path):
        import numpy

        numpy.save(path, obj, **self.save_kwargs)
        obj.save(path)

    def load(self, path: Path) -> "np.ndarray":
        import numpy

        return numpy.load(path, **self.load_kwargs)

PandasSerializer #

Serializer for Pandas Series and DataFrames.

In order to use this Serializer pandas needs to be installed, some formats might need aditional libraries.

Parameters:

Name Type Description Default
format Union[str, Format]

Serialization method from the ones supported by pandas, by default Format.parquet

parquet
to_kwargs Optional[Dict[str, Any]]

Extra arguments for the corresponding pandas.read_ function, by default None

None
read_kwargs Optional[Dict[str, Any]]

Extra arguments for the corresponding pandas.to_ function, by default None

None
Source code in scrat/serializer/pandas.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class PandasSerializer:
    """
    Serializer for Pandas Series and DataFrames.

    In order to use this Serializer pandas needs to be installed,
    some formats might need aditional libraries.

    Parameters
    ----------
    format
        Serialization method from the ones supported by pandas,
        by default Format.parquet
    to_kwargs
        Extra arguments for the corresponding pandas.read_<format> function,
        by default None
    read_kwargs
        Extra arguments for the corresponding pandas.to_<format> function,
        by default None
    """

    def __init__(
        self,
        format: T.Union[str, Format] = Format.parquet,
        to_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
        read_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
    ) -> None:
        # test that pandas is installed
        import pandas  # noqa: F401

        self.format = format if isinstance(format, Format) else Format(format)

        self.to_kwargs = to_kwargs if to_kwargs is not None else {}
        self.read_kwargs = read_kwargs if read_kwargs is not None else {}

    def dump(self, obj: "pd.DataFrame", path: Path):
        to_method = getattr(obj, f"to_{self.format.value}")
        to_method(path, **self.to_kwargs)

    def load(self, path: Path) -> "pd.DataFrame":
        import pandas

        read_method = getattr(pandas, f"read_{self.format.value}")
        return read_method(path, **self.read_kwargs)

PickleSerializer #

Pickle serializer.

Parameters:

Name Type Description Default
dump_kwargs Optional[Dict[str, Any]]

Extra arguments for pickle.dump, by default None

None
load_kwargs Optional[Dict[str, Any]]

Extra arguments for pickle.load, by default None

None
Source code in scrat/serializer/pickle.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class PickleSerializer:
    """
    Pickle serializer.

    Parameters
    ----------
    dump_kwargs
        Extra arguments for pickle.dump, by default None
    load_kwargs
        Extra arguments for pickle.load, by default None
    """

    def __init__(
        self,
        dump_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
        load_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
    ) -> None:
        self.dump_kwargs = dump_kwargs if dump_kwargs is not None else {}
        self.load_kwargs = load_kwargs if load_kwargs is not None else {}

    def dump(self, obj: T.Any, path: Path):
        with open(path, "wb") as f:
            pickle.dump(obj, f, **self.dump_kwargs)

    def load(self, path: Path) -> T.Any:
        with open(path, "rb") as f:
            return pickle.load(f, **self.load_kwargs)

get_default_serializer(func) #

Try to find a sane serializer using the function's typehint.

Defaults to PickleSerializer

Parameters:

Name Type Description Default
func Callable

The user function to be called

required

Returns:

Type Description
An instance of the chosen Serializer
Source code in scrat/serializer/__init__.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def get_default_serializer(func: T.Callable) -> Serializer:
    """
    Try to find a sane serializer using the function's typehint.

    Defaults to `PickleSerializer`

    Parameters
    ----------
    func
        The user function to be called

    Returns
    -------
        An instance of the chosen Serializer
    """
    import inspect

    sign = inspect.signature(func)
    if hasattr(sign, "return_annotations"):
        return DEFAULT_SERIALIZERS.get(
            sign.return_annotation.__name__, PickleSerializer
        )()

    return PickleSerializer()