Path loaders

Loaders for PathBuckets.

The Loaders in this module are used to load and save objects identified by a unique Path. For saving objects, these loaders rely on checking the type of the object for can_save and save methods. For loading objects, these loaders rely on checking the file extension of the path for can_load and load methods.

class PathLoader #

Bases: Loader[Path, T]

A Loader for loading and saving objects indentified by a Path.

name: str
classvar
#

The name of the loader.

def can_load(key, /, *, check=None)
abstractmethod classmethod
#

Return True if this loader supports the resource at key.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
key

The key used to identify the resource

TYPE: Path

check

If the loader can support loading a specific type of object.

TYPE: type[T] | None DEFAULT: None

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_load(cls, key: Path, /, *, check: type[T] | None = None) -> bool:
    """Return True if this loader supports the resource at key.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        key: The key used to identify the resource
        check: If the loader can support loading a specific type
            of object.
    """
    ...

def can_save(obj, key)
abstractmethod classmethod
#

Return True if this loader can save this object.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key used to identify the resource

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """Return True if this loader can save this object.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        obj: The object to save.
        key: The key used to identify the resource
    """
    ...

def save(obj, key)
abstractmethod classmethod
#

Save an object to under the given key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key to save the object under.

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """Save an object to under the given key.

    Args:
        obj: The object to save.
        key: The key to save the object under.
    """
    ...

def load(key)
abstractmethod classmethod
#

Load an object from the given key.

PARAMETER DESCRIPTION
key

The key to load the object from.

TYPE: Path

RETURNS DESCRIPTION
T

The loaded object.

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def load(cls, key: Path, /) -> T:
    """Load an object from the given key.

    Args:
        key: The key to load the object from.

    Returns:
        The loaded object.
    """
    ...

class NPYLoader #

Bases: PathLoader[ndarray]

A Loader for loading and saving np.ndarrays.

This loader supports the following file extensions:

  • ".npy"

This loader supports the following types:

name: ClassVar
classvar attr
#

The name of the loader.

def can_save(obj, key)
classmethod
#

Return True if this loader can save this object.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key used to identify the resource

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """Return True if this loader can save this object.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        obj: The object to save.
        key: The key used to identify the resource
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_save"""  # noqa: D415
    return isinstance(obj, np.ndarray) and key.suffix in {".npy"}

def can_load(key, /, *, check=None)
classmethod
#

Return True if this loader supports the resource at key.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
key

The key used to identify the resource

TYPE: Path

check

If the loader can support loading a specific type of object.

TYPE: type[T] | None DEFAULT: None

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_load(cls, key: Path, /, *, check: type[T] | None = None) -> bool:
    """Return True if this loader supports the resource at key.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        key: The key used to identify the resource
        check: If the loader can support loading a specific type
            of object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_load(cls, key: Path, /, *, check: type | None = None) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_load"""  # noqa: D415
    return key.suffix in {".npy"} and check in (np.ndarray, None)

def load(key)
classmethod
#

Load an object from the given key.

PARAMETER DESCRIPTION
key

The key to load the object from.

TYPE: Path

RETURNS DESCRIPTION
T

The loaded object.

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def load(cls, key: Path, /) -> T:
    """Load an object from the given key.

    Args:
        key: The key to load the object from.

    Returns:
        The loaded object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def load(cls, key: Path, /) -> np.ndarray:
    """::: amltk.store.paths.path_loaders.PathLoader.load"""  # noqa: D415
    logger.debug(f"Loading {key=}")
    item = np.load(key, allow_pickle=False)
    if not isinstance(item, np.ndarray):
        msg = f"Expected `np.ndarray` from {key=} but got `{type(item).__name__}`."
        raise TypeError(msg)

    return item

def save(obj, key)
classmethod
#

Save an object to under the given key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key to save the object under.

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """Save an object to under the given key.

    Args:
        obj: The object to save.
        key: The key to save the object under.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def save(cls, obj: np.ndarray, key: Path, /) -> None:
    """::: amltk.store.paths.path_loaders.PathLoader.save"""  # noqa: D415
    logger.debug(f"Saving {key=}")
    np.save(key, obj, allow_pickle=False)

class PDLoader #

Bases: PathLoader[DataFrame | Series]

A Loader for loading and saving pd.DataFrames.

This loader supports the following file extensions:

  • ".csv"
  • ".parquet"
  • ".pdpickle"

This loader supports the following types:

Multiindex support

There is currently no multi-index support as we explicitly use index_col=0 when loading a ".csv" file. This is because we assume that the first column is the index to prevent Unamed columns from being created.

Series support

There is currently limited support for pandas series as once written to csv/parquet, they are converted to a dataframe with a single column. See this issue

Please consider using ".pdpickle" instead.

name: ClassVar
classvar attr
#

The name of the loader.

def can_load(key, /, *, check=None)
classmethod
#

Return True if this loader supports the resource at key.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
key

The key used to identify the resource

TYPE: Path

check

If the loader can support loading a specific type of object.

TYPE: type[T] | None DEFAULT: None

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_load(cls, key: Path, /, *, check: type[T] | None = None) -> bool:
    """Return True if this loader supports the resource at key.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        key: The key used to identify the resource
        check: If the loader can support loading a specific type
            of object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_load(cls, key: Path, /, *, check: type | None = None) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_load"""  # noqa: D415
    if key.suffix in (".pdpickle", None):
        return check in (pd.Series, pd.DataFrame, None)

    if key.suffix in (".csv", ".parquet"):
        return check in (pd.DataFrame, None)

    return False

def can_save(obj, key)
classmethod
#

Return True if this loader can save this object.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key used to identify the resource

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """Return True if this loader can save this object.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        obj: The object to save.
        key: The key used to identify the resource
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_save"""  # noqa: D415
    if key.suffix == ".pdpickle":
        return isinstance(obj, pd.Series | pd.DataFrame)

    if key.suffix == ".parquet":
        return isinstance(obj, pd.DataFrame)

    if key.suffix == ".csv":
        # TODO: https://github.com/automl/amltk/issues/4
        return isinstance(obj, pd.DataFrame) and obj.index.nlevels == 1

    return False

def load(key)
classmethod
#

Load an object from the given key.

PARAMETER DESCRIPTION
key

The key to load the object from.

TYPE: Path

RETURNS DESCRIPTION
T

The loaded object.

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def load(cls, key: Path, /) -> T:
    """Load an object from the given key.

    Args:
        key: The key to load the object from.

    Returns:
        The loaded object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def load(cls, key: Path, /) -> pd.DataFrame | pd.Series:
    """::: amltk.store.paths.path_loaders.PathLoader.load"""  # noqa: D415
    logger.debug(f"Loading {key=}")
    if key.suffix == ".csv":
        return pd.read_csv(key, index_col=0)

    if key.suffix == ".parquet":
        return pd.read_parquet(key)

    if key.suffix == ".pdpickle":
        obj = pd.read_pickle(key)  # noqa: S301
        if not isinstance(obj, pd.Series | pd.DataFrame):
            msg = (
                f"Expected `pd.Series | pd.DataFrame` from {key=}"
                f" but got `{type(obj).__name__}`."
            )
            raise TypeError(msg)

        return obj

    raise ValueError(f"Unknown file extension {key.suffix}")

def save(obj, key)
classmethod
#

Save an object to under the given key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key to save the object under.

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """Save an object to under the given key.

    Args:
        obj: The object to save.
        key: The key to save the object under.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def save(cls, obj: pd.Series | pd.DataFrame, key: Path, /) -> None:
    """::: amltk.store.paths.path_loaders.PathLoader.save"""  # noqa: D415
    # Most pandas methods only seem to support dataframes
    logger.debug(f"Saving {key=}")

    if key.suffix == ".pdpickle":
        obj.to_pickle(key)
        return

    if key.suffix == ".csv":
        if obj.index.name is None and obj.index.nlevels == 1:
            obj.index.name = "index"

        obj.to_csv(key, index=True)
        return

    if key.suffix == ".parquet":
        obj.to_parquet(key)
        return

    raise ValueError(f"Unknown extension {key.suffix=}")

class JSONLoader #

Bases: PathLoader[dict | list]

A Loader for loading and saving dicts and lists to JSON.

This loader supports the following file extensions:

  • ".json"

This loader supports the following types:

name: ClassVar
classvar attr
#

The name of the loader.

def can_load(key, /, *, check=None)
classmethod
#

Return True if this loader supports the resource at key.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
key

The key used to identify the resource

TYPE: Path

check

If the loader can support loading a specific type of object.

TYPE: type[T] | None DEFAULT: None

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_load(cls, key: Path, /, *, check: type[T] | None = None) -> bool:
    """Return True if this loader supports the resource at key.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        key: The key used to identify the resource
        check: If the loader can support loading a specific type
            of object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_load(cls, key: Path, /, *, check: type | None = None) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_load"""  # noqa: D415
    return key.suffix == ".json" and check in (dict, list, None)

def can_save(obj, key)
classmethod
#

Return True if this loader can save this object.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key used to identify the resource

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """Return True if this loader can save this object.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        obj: The object to save.
        key: The key used to identify the resource
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_save"""  # noqa: D415
    return isinstance(obj, dict | list) and key.suffix == ".json"

def load(key)
classmethod
#

Load an object from the given key.

PARAMETER DESCRIPTION
key

The key to load the object from.

TYPE: Path

RETURNS DESCRIPTION
T

The loaded object.

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def load(cls, key: Path, /) -> T:
    """Load an object from the given key.

    Args:
        key: The key to load the object from.

    Returns:
        The loaded object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def load(cls, key: Path, /) -> dict | list:
    """::: amltk.store.paths.path_loaders.PathLoader.load"""  # noqa: D415
    logger.debug(f"Loading {key=}")
    with key.open("r") as f:
        item = json.load(f)

    if not isinstance(item, dict | list):
        msg = f"Expected `dict | list` from {key=} but got `{type(item).__name__}`"
        raise TypeError(msg)

    return item

def save(obj, key)
classmethod
#

Save an object to under the given key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key to save the object under.

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """Save an object to under the given key.

    Args:
        obj: The object to save.
        key: The key to save the object under.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def save(cls, obj: dict | list, key: Path, /) -> None:
    """::: amltk.store.paths.path_loaders.PathLoader.save"""  # noqa: D415
    logger.debug(f"Saving {key=}")
    with key.open("w") as f:
        json.dump(obj, f)

class YAMLLoader #

Bases: PathLoader[dict | list]

A Loader for loading and saving dicts and lists to YAML.

This loader supports the following file extensions:

  • ".yaml"
  • ".yml"

This loader supports the following types:

name: ClassVar
classvar attr
#

The name of the loader.

def can_load(key, /, *, check=None)
classmethod
#

Return True if this loader supports the resource at key.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
key

The key used to identify the resource

TYPE: Path

check

If the loader can support loading a specific type of object.

TYPE: type[T] | None DEFAULT: None

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_load(cls, key: Path, /, *, check: type[T] | None = None) -> bool:
    """Return True if this loader supports the resource at key.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        key: The key used to identify the resource
        check: If the loader can support loading a specific type
            of object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_load(cls, key: Path, /, *, check: type | None = None) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_load"""  # noqa: D415
    return key.suffix in (".yaml", ".yml") and check in (dict, list, None)

def can_save(obj, key)
classmethod
#

Return True if this loader can save this object.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key used to identify the resource

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """Return True if this loader can save this object.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        obj: The object to save.
        key: The key used to identify the resource
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_save"""  # noqa: D415
    return isinstance(obj, dict | list) and key.suffix in (".yaml", ".yml")

def load(key)
classmethod
#

Load an object from the given key.

PARAMETER DESCRIPTION
key

The key to load the object from.

TYPE: Path

RETURNS DESCRIPTION
T

The loaded object.

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def load(cls, key: Path, /) -> T:
    """Load an object from the given key.

    Args:
        key: The key to load the object from.

    Returns:
        The loaded object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def load(cls, key: Path, /) -> dict | list:
    """::: amltk.store.paths.path_loaders.PathLoader.load"""  # noqa: D415
    logger.debug(f"Loading {key=}")
    if yaml is None:
        raise ModuleNotFoundError("PyYAML is not installed")

    with key.open("r") as f:
        item = yaml.safe_load(f)

    if not isinstance(item, dict | list):
        msg = f"Expected `dict | list` from {key=} but got `{type(item).__name__}`"
        raise TypeError(msg)

    return item

def save(obj, key)
classmethod
#

Save an object to under the given key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key to save the object under.

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """Save an object to under the given key.

    Args:
        obj: The object to save.
        key: The key to save the object under.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def save(cls, obj: dict | list, key: Path, /) -> None:
    """::: amltk.store.paths.path_loaders.PathLoader.save"""  # noqa: D415
    logger.debug(f"Saving {key=}")
    if yaml is None:
        raise ModuleNotFoundError("PyYAML is not installed")

    with key.open("w") as f:
        yaml.dump(obj, f)

class PickleLoader #

Bases: PathLoader[Any]

A Loader for loading and saving any object to a pickle file.

This loader supports the following file extensions:

  • ".pkl"

This loader supports the following types:

  • object
Picklability

This loader uses Python's built-in pickle module to save and load objects. This means that the object must be picklable in order to be saved and loaded. If the object is not picklable, then an error will be raised when attempting to save or load the object.

name: ClassVar
classvar attr
#

The name of the loader.

def can_load(key, /, *, check=None)
classmethod
#

Return True if this loader supports the resource at key.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
key

The key used to identify the resource

TYPE: Path

check

If the loader can support loading a specific type of object.

TYPE: type[T] | None DEFAULT: None

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_load(cls, key: Path, /, *, check: type[T] | None = None) -> bool:
    """Return True if this loader supports the resource at key.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        key: The key used to identify the resource
        check: If the loader can support loading a specific type
            of object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_load(
    cls,
    key: Path,
    /,
    *,
    check: type | None = None,
) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_load"""  # noqa: D415
    return key.suffix in (".pkl", ".pickle")

def can_save(obj, key)
classmethod
#

Return True if this loader can save this object.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key used to identify the resource

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """Return True if this loader can save this object.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        obj: The object to save.
        key: The key used to identify the resource
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_save"""  # noqa: D415
    return True  # Anything can be attempted to be pickled

def load(key)
classmethod
#

Load an object from the given key.

PARAMETER DESCRIPTION
key

The key to load the object from.

TYPE: Path

RETURNS DESCRIPTION
T

The loaded object.

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def load(cls, key: Path, /) -> T:
    """Load an object from the given key.

    Args:
        key: The key to load the object from.

    Returns:
        The loaded object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def load(cls, key: Path, /) -> Any:
    """::: amltk.store.paths.path_loaders.PathLoader.load"""  # noqa: D415
    logger.debug(f"Loading {key=}")
    with key.open("rb") as f:
        return pickle.load(f)  # noqa: S301

def save(obj, key)
classmethod
#

Save an object to under the given key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key to save the object under.

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """Save an object to under the given key.

    Args:
        obj: The object to save.
        key: The key to save the object under.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """::: amltk.store.paths.path_loaders.PathLoader.save"""  # noqa: D415
    logger.debug(f"Saving {key=}")
    with key.open("wb") as f:
        pickle.dump(obj, f)

class TxtLoader #

Bases: PathLoader[str]

A Loader for loading and saving strs to text files.

This loader supports the following file extensions:

  • ".text"
  • ".txt"

This loader supports the following types:

name: ClassVar
classvar attr
#

The name of the loader.

def can_load(key, /, *, check=None)
classmethod
#

Return True if this loader supports the resource at key.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
key

The key used to identify the resource

TYPE: Path

check

If the loader can support loading a specific type of object.

TYPE: type[T] | None DEFAULT: None

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_load(cls, key: Path, /, *, check: type[T] | None = None) -> bool:
    """Return True if this loader supports the resource at key.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        key: The key used to identify the resource
        check: If the loader can support loading a specific type
            of object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_load(cls, key: Path, /, *, check: type | None = None) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_load"""  # noqa: D415
    return key.suffix in (".text", ".txt") and check in (str, None)

def can_save(obj, key)
classmethod
#

Return True if this loader can save this object.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key used to identify the resource

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """Return True if this loader can save this object.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        obj: The object to save.
        key: The key used to identify the resource
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_save"""  # noqa: D415
    return isinstance(obj, str) and key.suffix in (".text", ".txt")

def load(key)
classmethod
#

Load an object from the given key.

PARAMETER DESCRIPTION
key

The key to load the object from.

TYPE: Path

RETURNS DESCRIPTION
T

The loaded object.

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def load(cls, key: Path, /) -> T:
    """Load an object from the given key.

    Args:
        key: The key to load the object from.

    Returns:
        The loaded object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def load(cls, key: Path, /) -> str:
    """::: amltk.store.paths.path_loaders.PathLoader.load"""  # noqa: D415
    logger.debug(f"Loading {key=}")
    with key.open("r") as f:
        return f.read()

def save(obj, key)
classmethod
#

Save an object to under the given key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key to save the object under.

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """Save an object to under the given key.

    Args:
        obj: The object to save.
        key: The key to save the object under.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def save(cls, obj: str, key: Path, /) -> None:
    """::: amltk.store.paths.path_loaders.PathLoader.save"""  # noqa: D415
    logger.debug(f"Saving {key=}")
    with key.open("w") as f:
        f.write(obj)

class ByteLoader #

Bases: PathLoader[bytes]

A Loader for loading and saving bytes to binary files.

This loader supports the following file extensions:

  • ".bin"
  • ".bytes"

This loader supports the following types:

def can_load(key, /, *, check=None)
classmethod
#

Return True if this loader supports the resource at key.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
key

The key used to identify the resource

TYPE: Path

check

If the loader can support loading a specific type of object.

TYPE: type[T] | None DEFAULT: None

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_load(cls, key: Path, /, *, check: type[T] | None = None) -> bool:
    """Return True if this loader supports the resource at key.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        key: The key used to identify the resource
        check: If the loader can support loading a specific type
            of object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_load(cls, key: Path, /, *, check: type | None = None) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_load"""  # noqa: D415
    return key.suffix in (".bin", ".bytes") and check in (bytes, None)

def can_save(obj, key)
classmethod
#

Return True if this loader can save this object.

This is used to determine which loader to use when loading a resource from a key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key used to identify the resource

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """Return True if this loader can save this object.

    This is used to determine which loader to use when loading a
    resource from a key.

    Args:
        obj: The object to save.
        key: The key used to identify the resource
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def can_save(cls, obj: Any, key: Path, /) -> bool:
    """::: amltk.store.paths.path_loaders.PathLoader.can_save"""  # noqa: D415
    return isinstance(obj, dict | list) and key.suffix in (".bin", ".bytes")

def load(key)
classmethod
#

Load an object from the given key.

PARAMETER DESCRIPTION
key

The key to load the object from.

TYPE: Path

RETURNS DESCRIPTION
T

The loaded object.

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def load(cls, key: Path, /) -> T:
    """Load an object from the given key.

    Args:
        key: The key to load the object from.

    Returns:
        The loaded object.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def load(cls, key: Path, /) -> bytes:
    """::: amltk.store.paths.path_loaders.PathLoader.load"""  # noqa: D415
    logger.debug(f"Loading {key=}")
    with key.open("rb") as f:
        return f.read()

def save(obj, key)
classmethod
#

Save an object to under the given key.

PARAMETER DESCRIPTION
obj

The object to save.

TYPE: Any

key

The key to save the object under.

TYPE: Path

Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
@abstractmethod
def save(cls, obj: Any, key: Path, /) -> None:
    """Save an object to under the given key.

    Args:
        obj: The object to save.
        key: The key to save the object under.
    """
    ...
Source code in src/amltk/store/paths/path_loaders.py
@override
@classmethod
def save(cls, obj: bytes, key: Path, /) -> None:
    """::: amltk.store.paths.path_loaders.PathLoader.save"""  # noqa: D415
    logger.debug(f"Saving {key=}")
    with key.open("wb") as f:
        f.write(obj)