Skip to content

autora.serializer

A submodule which handles importing supported serializers.

SupportedSerializer

Bases: str, Enum

Listing of allowed serializers.

Source code in temp_dir/core/src/autora/serializer/__init__.py
29
30
31
32
33
34
class SupportedSerializer(str, Enum):
    """Listing of allowed serializers."""

    pickle = "pickle"
    dill = "dill"
    yaml = "yaml"

dump_state(state_, path, dumper=default_serializer)

Write a State object to a path.

Source code in temp_dir/core/src/autora/serializer/__init__.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def dump_state(
    state_: State,
    path: Optional[pathlib.Path],
    dumper: SupportedSerializer = default_serializer,
) -> None:
    """Write a State object to a path."""
    serializer = load_serializer(dumper)
    if path is not None:
        _logger.debug(f"dump_state: dumping to {path=}")
        path.parent.mkdir(parents=True, exist_ok=True)
        with open(path, f"w{serializer.file_mode}") as f:
            serializer.module.dump(state_, f)
    else:
        _logger.debug(f"dump_state: {path=} so writing to stdout")
        print(serializer.module.dumps(state_))
    return

load_serializer(serializer=default_serializer)

Load a serializer, returning an object which includes data on the file mode it expects (regular or binary).

Examples:

The default serializer is pickle:

>>> load_serializer()
LOADED_SERIALIZER(module=<module 'pickle' from '...'>, file_mode='b')

All supported serializers can be loaded explictly:

>>> p_ = load_serializer("pickle")
>>> p_
LOADED_SERIALIZER(module=<module 'pickle' from '...'>, file_mode='b')
>>> d_ = load_serializer("dill")
>>> d_
LOADED_SERIALIZER(module=<module 'dill' from '...'>, file_mode='b')
>>> y_ = load_serializer("yaml")
>>> y_
LOADED_SERIALIZER(module=<module 'autora.serializer.yaml_' from '...'>, file_mode='')

Note that the yaml serializer is a wrapped version of the pyyaml package, which conforms to the same interface as pickle.

This function loads the modules lazily and caches them, and returns the same object if the same serializer is loaded more than once.

>>> p_ is load_serializer("pickle")
True
>>> d_ is load_serializer("dill")
True
>>> y_ is load_serializer("yaml")
True

The Serializer can be specified by the SerializersSupported enum:

>>> load_serializer(SupportedSerializer.pickle)
LOADED_SERIALIZER(module=<module 'pickle'...
>>> load_serializer(SupportedSerializer.dill)
LOADED_SERIALIZER(module=<module 'dill'...
>>> load_serializer(SupportedSerializer.yaml)
LOADED_SERIALIZER(module=<module 'autora.serializer.yaml_'...
Source code in temp_dir/core/src/autora/serializer/__init__.py
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def load_serializer(
    serializer: Union[SupportedSerializer, str] = default_serializer
) -> LOADED_SERIALIZER:
    """
    Load a serializer, returning an object which includes data on the file mode it expects
    (regular or binary).

    Examples:
        The default serializer is pickle:
        >>> load_serializer()  # doctest: +ELLIPSIS
        LOADED_SERIALIZER(module=<module 'pickle' from '...'>, file_mode='b')

        All supported serializers can be loaded explictly:
        >>> p_ = load_serializer("pickle")
        >>> p_  # doctest: +ELLIPSIS
        LOADED_SERIALIZER(module=<module 'pickle' from '...'>, file_mode='b')

        >>> d_ = load_serializer("dill")
        >>> d_  # doctest: +ELLIPSIS
        LOADED_SERIALIZER(module=<module 'dill' from '...'>, file_mode='b')

        >>> y_ = load_serializer("yaml")
        >>> y_  # doctest: +ELLIPSIS
        LOADED_SERIALIZER(module=<module 'autora.serializer.yaml_' from '...'>, file_mode='')

        Note that the yaml serializer is a wrapped version of the `pyyaml` package,
        which conforms to the same interface as `pickle`.

        This function loads the modules lazily and caches them, and returns the same object if the
        same serializer is loaded more than once.
        >>> p_ is load_serializer("pickle")
        True
        >>> d_ is load_serializer("dill")
        True
        >>> y_ is load_serializer("yaml")
        True

        The Serializer can be specified by the SerializersSupported enum:
        >>> load_serializer(SupportedSerializer.pickle)  # doctest: +ELLIPSIS
        LOADED_SERIALIZER(module=<module 'pickle'...

        >>> load_serializer(SupportedSerializer.dill)  # doctest: +ELLIPSIS
        LOADED_SERIALIZER(module=<module 'dill'...

        >>> load_serializer(SupportedSerializer.yaml)  # doctest: +ELLIPSIS
        LOADED_SERIALIZER(module=<module 'autora.serializer.yaml_'...

    """
    serializer = SupportedSerializer(serializer)
    try:
        serializer_def = _LOADED_SERIALIZERS[serializer]

    except KeyError:
        serializer_info = _SERIALIZER_INFO[serializer]
        module_ = importlib.import_module(serializer_info.fully_qualified_module_name)
        serializer_def = LOADED_SERIALIZER(module_, serializer_info.file_mode)
        _LOADED_SERIALIZERS[serializer] = serializer_def

    return serializer_def

load_state(path, loader=default_serializer)

Load a State object from a path.

Source code in temp_dir/core/src/autora/serializer/__init__.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def load_state(
    path: Optional[pathlib.Path],
    loader: SupportedSerializer = default_serializer,
) -> Union[State, None]:
    """Load a State object from a path."""
    serializer = load_serializer(loader)
    if path is not None:
        _logger.debug(f"load_state: loading from {path=}")
        with open(path, f"r{serializer.file_mode}") as f:
            state_ = serializer.module.load(f)
    else:
        _logger.debug(f"load_state: {path=} -> returning None")
        state_ = None
    return state_