Skip to content

afnio.serialization

afnio.serialization.save(obj, f, pickle_protocol=DEFAULT_PROTOCOL)

Saves an object to a disk file using zip compression and pickle serialization.

Parameters:

Name Type Description Default
obj object

The object to be saved.

required
f str | PathLike | BinaryIO | IO[bytes]

A file-like object (must implement write/flush) or a string or os.PathLike object containing a file name.

required
pickle_protocol int

Pickle protocol version.

DEFAULT_PROTOCOL
Note

A common Afnio convention is to save variables using .hf file extension.

The .hf extension is a naming convention inspired by the chemical symbol for Hafnium (Hf). "Afnio" is the Italian word for Hafnium.

Examples:

>>> # Save to file
>>> x = afnio.Variable(data="You are a doctor.", role="system prompt")
>>> afnio.save(x, 'variable.hf')
>>> # Save to io.BytesIO buffer
>>> buffer = io.BytesIO()
>>> afnio.save(x, buffer)
Source code in afnio/serialization.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def save(
    obj: object,
    f: FILE_LIKE,
    pickle_protocol: int = DEFAULT_PROTOCOL,
) -> None:
    """
    Saves an object to a disk file using zip compression and pickle serialization.

    Args:
        obj: The object to be saved.
        f (Union[str, os.PathLike, BinaryIO, IO[bytes]]): A file-like object (must
            implement write/flush) or a string or `os.PathLike` object containing
            a file name.
        pickle_protocol: Pickle protocol version.

    Note:
        A common Afnio convention is to save variables using `.hf` file extension.

        The `.hf` extension is a naming convention inspired by the chemical symbol
        for [Hafnium (Hf)](https://en.wikipedia.org/wiki/Hafnium). "Afnio" is the
        Italian word for Hafnium.

    Examples:
        >>> # Save to file
        >>> x = afnio.Variable(data="You are a doctor.", role="system prompt")
        >>> afnio.save(x, 'variable.hf')
        >>> # Save to io.BytesIO buffer
        >>> buffer = io.BytesIO()
        >>> afnio.save(x, buffer)
    """
    _check_save_filelike(f)

    with _open_zipfile_writer(f) as opened_zipfile:
        _save(
            obj,
            opened_zipfile,
            pickle_protocol,
        )
        return

afnio.serialization.load(f)

Loads an object from a disk file using zip compression and pickle serialization.

Parameters:

Name Type Description Default
f str | PathLike | BinaryIO | IO[bytes]

A file-like object (must implement read) or a string or os.PathLike object containing a file name.

required

Returns:

Type Description
Any

The deserialized object.

Examples:

>>> # Load from file
>>> obj = afnio.load('model.hf')
>>> # Load from io.BytesIO buffer
>>> buffer = io.BytesIO()
>>> obj = afnio.load(buffer)
Source code in afnio/serialization.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
def load(f: FILE_LIKE) -> Any:
    """
    Loads an object from a disk file using zip compression and pickle serialization.

    Args:
        f (Union[str, os.PathLike, BinaryIO, IO[bytes]]): A file-like object (must
            implement `read`) or a string or `os.PathLike` object containing
            a file name.

    Returns:
        The deserialized object.

    Examples:
        >>> # Load from file
        >>> obj = afnio.load('model.hf')
        >>> # Load from io.BytesIO buffer
        >>> buffer = io.BytesIO()
        >>> obj = afnio.load(buffer)
    """
    with _open_zipfile_reader(f) as zip_reader:
        if "data.pkl" not in zip_reader.namelist():
            raise RuntimeError(
                "Missing 'data.pkl' in archive. File might be corrupted."
            )

        # Read the serialized object
        with zip_reader.open("data.pkl", "r") as f:
            obj = pickle.load(f)

    return obj