iden.io
iden.io ¶
Contain data loaders and savers.
iden.io.BaseFileSaver ¶
Bases: BaseSaver[T]
Define the base class to implement a file saver.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import JsonSaver, JsonLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... JsonSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JsonLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.BaseFileSaver.save ¶
save(
to_save: T, path: Path, *, exist_ok: bool = False
) -> None
Save the data into the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
T
|
The data to save. The data should be compatible with the saving engine. |
required |
path
|
Path
|
The path where to save the data. |
required |
exist_ok
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import JsonSaver, JsonLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... JsonSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JsonLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.BaseLoader ¶
Bases: ABC, Generic[T]
Define the base class to implement a data loader.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_json, JsonLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JsonLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.BaseLoader.equal
abstractmethod
¶
equal(other: Any, equal_nan: bool = False) -> bool
Indicate if two objects are equal or not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The object to compare with. |
required |
equal_nan
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from iden.io import JsonLoader, YamlLoader
>>> JsonLoader().equal(JsonLoader())
True
>>> JsonLoader().equal(YamlLoader())
False
iden.io.BaseLoader.load
abstractmethod
¶
load(path: Path) -> T
Load the data from the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path with the data to load. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The data |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_json, JsonLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JsonLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.BaseSaver ¶
Bases: ABC, Generic[T]
Define the base class to implement a data saver.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import JsonSaver, JsonLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... JsonSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JsonLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.BaseSaver.equal
abstractmethod
¶
equal(other: Any, equal_nan: bool = False) -> bool
Indicate if two objects are equal or not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
The object to compare with. |
required |
equal_nan
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from iden.io import JsonSaver, YamlSaver
>>> JsonSaver().equal(JsonSaver())
True
>>> JsonSaver().equal(YamlSaver())
False
iden.io.BaseSaver.save
abstractmethod
¶
save(
to_save: T, path: Path, *, exist_ok: bool = False
) -> None
Save the data into the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
T
|
The data to save. The data should be compatible with the saving engine. |
required |
path
|
Path
|
The path where to save the data. |
required |
exist_ok
|
bool
|
If |
False
|
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import JsonSaver, JsonLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... JsonSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JsonLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.CloudpickleLoader ¶
Bases: BaseLoader[Any]
Implement a data loader to load data in a pickle file with cloudpickle.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_cloudpickle, CloudpickleLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pkl")
... save_cloudpickle({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = CloudpickleLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.CloudpickleSaver ¶
Bases: BaseFileSaver[Any]
Implement a file saver to save data with a pickle file with cloudpickle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import CloudpickleSaver, CloudpickleLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pkl")
... CloudpickleSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = CloudpickleLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.JoblibLoader ¶
Bases: BaseLoader[T]
Implement a data loader to load data in a pickle file with joblib.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_joblib, JoblibLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.joblib")
... save_joblib({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JoblibLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.JoblibSaver ¶
Bases: BaseFileSaver[T]
Implement a file saver to save data with a pickle file with joblib.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import JoblibSaver, JoblibLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.joblib")
... JoblibSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JoblibLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.JsonLoader ¶
Bases: BaseLoader[T]
Implement a data loader to load data in a JSON file.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_json, JsonLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JsonLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.JsonSaver ¶
Bases: BaseFileSaver[T]
Implement a file saver to save data with a JSON file.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import JsonSaver, JsonLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... JsonSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = JsonLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.LoaderRegistry ¶
Bases: BaseLoader[Any]
Registry that manages and dispatches loaders based on file extension.
This registry maps file extensions (e.g., "json", "txt") to loader instances that handle loading files with those extensions. It provides automatic dispatching to the appropriate loader based on a file's extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
dict[str, BaseLoader[Any]] | None
|
Optional initial mapping of extensions to loaders. If provided, the registry is copied to prevent external mutations. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
_registry |
dict[str, BaseLoader[Any]]
|
Internal mapping of registered extensions to loaders |
Example
Basic usage with JSON and text loaders:
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_json, LoaderRegistry, JsonLoader, TextLoader
>>> registry = LoaderRegistry({"json": JsonLoader(), "txt": TextLoader()})
>>> registry
LoaderRegistry(
(json): JsonLoader()
(txt): TextLoader()
)
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = registry.load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.LoaderRegistry.find_loader ¶
find_loader(extension: str) -> BaseLoader[Any]
Find the appropriate loader for a given file extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extension
|
str
|
The file extension to find a loader for (e.g., "json", "txt") |
required |
Returns:
| Type | Description |
|---|---|
BaseLoader[Any]
|
The loader registered for the given file extension |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no loader is registered for the extension |
Example
>>> from iden.io import LoaderRegistry, JsonLoader
>>> registry = LoaderRegistry()
>>> registry.register("json", JsonLoader())
>>> loader = registry.find_loader("json")
>>> loader
JsonLoader()
iden.io.LoaderRegistry.has_loader ¶
has_loader(extension: str) -> bool
Check if a loader is registered for the given extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extension
|
str
|
The file extension to check (e.g., "json", "txt") |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a loader is registered for this extension, False otherwise |
Example
>>> from iden.io import LoaderRegistry, JsonLoader
>>> registry = LoaderRegistry()
>>> registry.register("json", JsonLoader())
>>> registry.has_loader("json")
True
>>> registry.has_loader("txt")
False
iden.io.LoaderRegistry.register ¶
register(
extension: str,
loader: BaseLoader[Any],
exist_ok: bool = False,
) -> None
Register a loader for a given file extension.
This method associates a loader instance with a specific file extension. When loading files with this extension, the registered loader will be used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extension
|
str
|
The file extension to register (e.g., "json", "txt") |
required |
loader
|
BaseLoader[Any]
|
The loader instance that handles files with this extension |
required |
exist_ok
|
bool
|
If False (default), raises an error if the extension is already registered. If True, overwrites the existing registration silently. |
False
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the extension is already registered and exist_ok is False |
Example
>>> from iden.io import LoaderRegistry, JsonLoader
>>> registry = LoaderRegistry()
>>> registry.register("json", JsonLoader())
>>> registry
LoaderRegistry(
(json): JsonLoader()
)
iden.io.LoaderRegistry.register_many ¶
register_many(
mapping: Mapping[str, BaseLoader[Any]],
exist_ok: bool = False,
) -> None
Register multiple loaders at once.
This is a convenience method for bulk registration that internally calls register() for each extension-loader pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[str, BaseLoader[Any]]
|
Dictionary mapping file extensions to loader instances |
required |
exist_ok
|
bool
|
If False (default), raises an error if any extension is already registered. If True, overwrites existing registrations silently. |
False
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If any extension is already registered and exist_ok is False |
Example
>>> from iden.io import LoaderRegistry, JsonLoader, TextLoader
>>> registry = LoaderRegistry()
>>> registry.register_many({"json": JsonLoader(), "txt": TextLoader()})
>>> registry
LoaderRegistry(
(json): JsonLoader()
(txt): TextLoader()
)
iden.io.PickleLoader ¶
Bases: BaseLoader[T]
Implement a data loader to load data in a pickle file.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_pickle, PickleLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pkl")
... save_pickle({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = PickleLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.PickleSaver ¶
Bases: BaseFileSaver[T]
Implement a file saver to save data with a pickle file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import PickleSaver, PickleLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pkl")
... PickleSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = PickleLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.TextLoader ¶
Bases: BaseLoader[str]
Implement a data loader to load data in a text file.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_text, TextLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.txt")
... save_text("hello", path)
... data = TextLoader().load(path)
... data
...
'hello'
iden.io.TextSaver ¶
Bases: BaseFileSaver[str]
Implement a file saver to save data with a text file.
Note
If the data to save is not a string, it is converted to
a string before to be saved by using str.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import TextSaver, TextLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.txt")
... TextSaver().save("hello", path)
... data = TextLoader().load(path)
... data
...
'hello'
iden.io.TorchLoader ¶
Bases: BaseLoader[T]
Implement a data loader to load data in a PyTorch file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_torch, TorchLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pt")
... save_torch({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = TorchLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.TorchSaver ¶
Bases: BaseFileSaver[T]
Implement a file saver to save data with a PyTorch file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import TorchSaver, TorchLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pt")
... TorchSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = TorchLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.YamlLoader ¶
Bases: BaseLoader[T]
Implement a data loader to load data in a YAML file.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_yaml, YamlLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.yaml")
... save_yaml({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = YamlLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.YamlSaver ¶
Bases: BaseFileSaver[T]
Implement a file saver to save data with a YAML file.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import YamlSaver, YamlLoader
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.yaml")
... YamlSaver().save({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = YamlLoader().load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.get_default_loader_registry ¶
get_default_loader_registry() -> LoaderRegistry
Get or create the default global registry.
Returns:
| Type | Description |
|---|---|
LoaderRegistry
|
A LoaderRegistry instance with default loaders registered for |
LoaderRegistry
|
common file formats (json, pkl, pickle, txt, yaml, yml, and |
LoaderRegistry
|
optionally joblib and pt if their dependencies are available). |
Notes
The singleton pattern means modifications to the returned registry affect all future calls to this function. An isolated registry can be created by instantiating a new LoaderRegistry directly.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_json, get_default_loader_registry
>>> registry = get_default_loader_registry()
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = registry.load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.is_loader_config ¶
is_loader_config(config: dict[Any, Any]) -> bool
Indicate if the input configuration is a configuration for a
BaseLoader.
This function only checks if the value of the key _target_
is valid. It does not check the other values. If _target_
indicates a function, the returned type hint is used to check
the class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[Any, Any]
|
The configuration to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from iden.io import is_loader_config
>>> is_loader_config({"_target_": "iden.io.JsonLoader"})
True
iden.io.is_saver_config ¶
is_saver_config(config: dict[Any, Any]) -> bool
Indicate if the input configuration is a configuration for a
BaseSaver.
This function only checks if the value of the key _target_
is valid. It does not check the other values. If _target_
indicates a function, the returned type hint is used to check
the class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[Any, Any]
|
The configuration to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from iden.io import is_saver_config
>>> is_saver_config({"_target_": "iden.io.JsonSaver"})
True
iden.io.load ¶
load(
path: Path, registry: LoaderRegistry | None = None
) -> Any
Load the data from the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path with the data to load. |
required |
registry
|
LoaderRegistry | None
|
Registry to load data. If |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The data |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_json, load
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.load_cloudpickle ¶
load_cloudpickle(path: Path) -> Any
Load the data from a given pickle file with cloudpickle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to the pickle file. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The data from the pickle file. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_cloudpickle, load_cloudpickle
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pkl")
... save_cloudpickle({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_cloudpickle(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.load_joblib ¶
load_joblib(path: Path) -> Any
Load the data from a given pickle file with joblib.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to the pickle file. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The data from the pickle file. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_joblib, load_joblib
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.joblib")
... save_joblib({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_joblib(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.load_json ¶
load_json(path: Path) -> Any
Load the data from a given JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to the JSON file. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The data from the JSON file. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_json, load_json
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_json(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.load_pickle ¶
load_pickle(path: Path) -> Any
Load the data from a given pickle file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to the pickle file. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The data from the pickle file. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_pickle, load_pickle
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pkl")
... save_pickle({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_pickle(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.load_text ¶
load_text(path: Path) -> str
Load the data from a given text file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path where to the text file. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The data from the text file. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_text, load_text
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.txt")
... save_text("hello", path)
... data = load_text(path)
... data
...
'hello'
iden.io.load_torch ¶
load_torch(path: Path, **kwargs: Any) -> Any
Load the data from a given PyTorch file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to the PyTorch file. |
required |
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The data from the PyTorch file. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_torch, load_torch
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pt")
... save_torch({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_torch(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.load_yaml ¶
load_yaml(path: Path) -> Any
Load the data from a given YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The path to the YAML file. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The data from the YAML file. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import load_yaml, save_yaml
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.yaml")
... save_yaml({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_yaml(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.register_loaders ¶
register_loaders(
mapping: Mapping[str, BaseLoader[Any]],
exist_ok: bool = False,
) -> None
Register custom loaders to the default global registry.
This allows users to add support for custom file extensions without modifying global state directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[str, BaseLoader[Any]]
|
Dictionary mapping file extensions to loader instances |
required |
exist_ok
|
bool
|
If |
False
|
Example
>>> from iden.io import register_loaders, TextLoader
>>> register_loaders({"longtext": TextLoader()})
iden.io.save_cloudpickle ¶
save_cloudpickle(
to_save: Any,
path: Path,
*,
exist_ok: bool = False,
**kwargs: Any
) -> None
Save the given data in a pickle file with cloudpickle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
Any
|
The data to write in a pickle file. |
required |
path
|
Path
|
The path where to write the pickle file. |
required |
exist_ok
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_cloudpickle, load_cloudpickle
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pkl")
... save_cloudpickle({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_cloudpickle(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.save_joblib ¶
save_joblib(
to_save: Any,
path: Path,
*,
exist_ok: bool = False,
**kwargs: Any
) -> None
Save the given data in a pickle file with joblib.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
Any
|
The data to write in a pickle file. |
required |
path
|
Path
|
The path where to write the pickle file. |
required |
exist_ok
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_joblib, load_joblib
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.joblib")
... save_joblib({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_joblib(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.save_json ¶
save_json(
to_save: Any, path: Path, *, exist_ok: bool = False
) -> None
Save the given data in a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
Any
|
The data to write in a JSON file. |
required |
path
|
Path
|
The path where to write the JSON file. |
required |
exist_ok
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_json, load_json
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.json")
... save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_json(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.save_pickle ¶
save_pickle(
to_save: Any,
path: Path,
*,
exist_ok: bool = False,
**kwargs: Any
) -> None
Save the given data in a pickle file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
Any
|
The data to write in a pickle file. |
required |
path
|
Path
|
The path where to write the pickle file. |
required |
exist_ok
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_pickle, load_pickle
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pkl")
... save_pickle({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_pickle(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.save_text ¶
save_text(
to_save: Any, path: Path, *, exist_ok: bool = False
) -> None
Save the given data in a text file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
Any
|
The data to write in a text file. |
required |
path
|
Path
|
The path where to write the text file. |
required |
exist_ok
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Note
If the data to save is not a string, it is converted to
a string before to be saved by using str.
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_text, load_text
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.txt")
... save_text("hello", path)
... data = load_text(path)
... data
...
'hello'
iden.io.save_torch ¶
save_torch(
to_save: Any,
path: Path,
*,
exist_ok: bool = False,
**kwargs: Any
) -> None
Save the given data in a PyTorch file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
Any
|
The data to write in a PyTorch file. |
required |
path
|
Path
|
The path where to write the PyTorch file. |
required |
exist_ok
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import save_torch, load_torch
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.pt")
... save_torch({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_torch(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.save_yaml ¶
save_yaml(
to_save: Any, path: Path, *, exist_ok: bool = False
) -> None
Save the given data in a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_save
|
Any
|
The data to write in a YAML file. |
required |
path
|
Path
|
The path where to write the YAML file. |
required |
exist_ok
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
FileExistsError
|
if the file already exists. |
Example
>>> import tempfile
>>> from pathlib import Path
>>> from iden.io import load_yaml, save_yaml
>>> with tempfile.TemporaryDirectory() as tmpdir:
... path = Path(tmpdir).joinpath("data.yaml")
... save_yaml({"key1": [1, 2, 3], "key2": "abc"}, path)
... data = load_yaml(path)
... data
...
{'key1': [1, 2, 3], 'key2': 'abc'}
iden.io.setup_loader ¶
setup_loader(
loader: BaseLoader[T] | dict[Any, Any],
) -> BaseLoader[T]
Set up a data loader.
The data loader is instantiated from its configuration by using the
BaseLoader factory function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
BaseLoader[T] | dict[Any, Any]
|
The data loader or its configuration. |
required |
Returns:
| Type | Description |
|---|---|
BaseLoader[T]
|
The instantiated data loader. |
Example
>>> from iden.io import setup_loader
>>> loader = setup_loader({"_target_": "iden.io.JsonLoader"})
>>> loader
JsonLoader()
iden.io.setup_saver ¶
Set up a data saver.
The data saver is instantiated from its configuration by using the
BaseSaver factory function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
saver
|
BaseSaver[T] | dict[Any, Any]
|
The data saver or its configuration. |
required |
Returns:
| Type | Description |
|---|---|
BaseSaver[T]
|
The instantiated data saver. |
Example
>>> from iden.io import setup_saver
>>> saver = setup_saver({"_target_": "iden.io.JsonSaver"})
>>> saver
JsonSaver()
iden.io.safetensors ¶
Contain safetensors data loaders and savers.
iden.io.safetensors.NumpyLoader ¶
Bases: BaseLoader[dict[str, ndarray]]
Implement a file loader to load numpy.ndarrays in the
safetensors format.
Link: https://huggingface.co/docs/safetensors/en/index
iden.io.safetensors.NumpySafetensorsLoader ¶
Bases: BaseLoader[dict[str, ndarray]]
Implement a file loader to load numpy.ndarrays in the
safetensors format.
Link: https://huggingface.co/docs/safetensors/en/index
iden.io.safetensors.NumpySafetensorsSaver ¶
Bases: BaseFileSaver[dict[str, ndarray]]
Implement a file saver to save numpy.ndarrays with the
safetensors format.
This saver can only save a dictionary of numpy.ndarrays.
Link: https://huggingface.co/docs/safetensors/en/index
iden.io.safetensors.NumpySaver ¶
Bases: BaseFileSaver[dict[str, ndarray]]
Implement a file saver to save numpy.ndarrays with the
safetensors format.
This saver can only save a dictionary of numpy.ndarrays.
Link: https://huggingface.co/docs/safetensors/en/index
iden.io.safetensors.TorchLoader ¶
Bases: BaseLoader[dict[str, Tensor]]
Implement a file loader to load torch.Tensors in the
safetensors format.
Link: https://huggingface.co/docs/safetensors/en/index
iden.io.safetensors.TorchSafetensorsLoader ¶
Bases: BaseLoader[dict[str, Tensor]]
Implement a file loader to load torch.Tensors in the
safetensors format.
Link: https://huggingface.co/docs/safetensors/en/index
iden.io.safetensors.TorchSafetensorsSaver ¶
Bases: BaseFileSaver[dict[str, Tensor]]
Implement a file saver to save torch.Tensors with the
safetensors format.
This saver can only save a dictionary of torch.Tensors.
Link: https://huggingface.co/docs/safetensors/en/index
iden.io.safetensors.TorchSaver ¶
Bases: BaseFileSaver[dict[str, Tensor]]
Implement a file saver to save torch.Tensors with the
safetensors format.
This saver can only save a dictionary of torch.Tensors.
Link: https://huggingface.co/docs/safetensors/en/index