docarray.array.mixins.io.binary module#
- class docarray.array.mixins.io.binary.BinaryIOMixin[source]#
Bases:
objectSave/load an array to a binary file.
- classmethod load_binary(file, protocol='pickle-array', compress=None, _show_progress=False, streaming=False, *args, **kwargs)[source]#
Load array elements from a compressed binary file.
- Parameters:
file (
Union[str,BinaryIO,bytes,Path]) – File or filename or serialized bytes where the data is stored.protocol (
str) – protocol to usecompress (
Optional[str]) – compress algorithm to use_show_progress (
bool) – show progress bar, only works when protocol is pickle or protobufstreaming (
bool) – if True returns a generator over Document objects.
In case protocol is pickle the Documents are streamed from disk to save memory usage :rtype:
Union[DocumentArray,Generator[Document,None,None]] :return: a DocumentArray objectNote
If file is str it can specify protocol and compress as file extensions. This functionality assumes file=file_name.$protocol.$compress where $protocol and $compress refer to a string interpolation of the respective protocol and compress methods. For example if file=my_docarray.protobuf.lz4 then the binary data will be loaded assuming protocol=protobuf and compress=lz4.
- classmethod from_bytes(data, protocol='pickle-array', compress=None, _show_progress=False, *args, **kwargs)[source]#
- Return type:
T
- save_binary(file, protocol='pickle-array', compress=None)[source]#
Save array elements into a binary file.
- Parameters:
file (
Union[str,BinaryIO]) – File or filename to which the data is saved.protocol (
str) – protocol to usecompress (
Optional[str]) –compress algorithm to use
Note
If file is str it can specify protocol and compress as file extensions. This functionality assumes file=file_name.$protocol.$compress where $protocol and $compress refer to a string interpolation of the respective protocol and compress methods. For example if file=my_docarray.protobuf.lz4 then the binary data will be created using protocol=protobuf and compress=lz4.
Comparing to
save_json(), it is faster and the file is smaller, but not human-readable.Note
To get a binary presentation in memory, use
bytes(...).- Return type:
None
- to_bytes(protocol='pickle-array', compress=None, _file_ctx=None, _show_progress=False)[source]#
Serialize itself into bytes.
For more Pythonic code, please use
bytes(...).- Parameters:
_file_ctx (
Optional[BinaryIO]) – File or filename or serialized bytes where the data is stored.protocol (
str) – protocol to usecompress (
Optional[str]) – compress algorithm to use_show_progress (
bool) – show progress bar, only works when protocol is pickle or protobuf
- Return type:
bytes- Returns:
the binary serialization in bytes
- to_protobuf(ndarray_type=None)[source]#
Convert DocumentArray into a Protobuf message.
- Parameters:
ndarray_type (
Optional[str]) – can belistornumpy, if set it will force all ndarray-like object from all Documents toListornumpy.ndarray.- Return type:
DocumentArrayProto
- Returns:
the protobuf message