-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathcommon.py
More file actions
49 lines (41 loc) · 2 KB
/
common.py
File metadata and controls
49 lines (41 loc) · 2 KB
1
2
3
4
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
41
42
43
44
45
46
47
48
49
from typing import Union, TextIO, BinaryIO, TYPE_CHECKING, Type
if TYPE_CHECKING:
from ....types import T
class CommonIOMixin:
"""The common IO helper function for arrays. """
def save(
self, file: Union[str, TextIO, BinaryIO], file_format: str = 'binary'
) -> None:
"""Save array elements into a JSON, a binary file or a CSV file.
:param file: File or filename to which the data is saved.
:param file_format: `json` or `binary` or `csv`. JSON and CSV files are human-readable,
but binary format gives much smaller size and faster save/load speed. Note that, CSV file has very limited
compatability, complex DocumentArray with nested structure can not be restored from a CSV file.
"""
if file_format == 'json':
self.save_json(file)
elif file_format == 'binary':
self.save_binary(file)
elif file_format == 'csv':
self.save_csv(file)
else:
raise ValueError('`format` must be one of [`json`, `binary`, `csv`]')
@classmethod
def load(
cls: Type['T'], file: Union[str, TextIO, BinaryIO], file_format: str = 'binary'
) -> 'T':
"""Load array elements from a JSON or a binary file, or a CSV file.
:param file: File or filename to which the data is saved.
:param file_format: `json` or `binary` or `csv`. JSON and CSV files are human-readable,
but binary format gives much smaller size and faster save/load speed. CSV file has very limited compatability,
complex DocumentArray with nested structure can not be restored from a CSV file.
:return: the loaded DocumentArray object
"""
if file_format == 'json':
return cls.load_json(file)
elif file_format == 'binary':
return cls.load_binary(file)
elif file_format == 'csv':
return cls.load_csv(file)
else:
raise ValueError('`format` must be one of [`json`, `binary`, `csv`]')