Skip to content

Files

neps.utils.files #

Utilities for file operations.

deserialize #

deserialize(path: Path | str) -> dict[str, Any]

Deserialize data from a yaml file.

Source code in neps/utils/files.py
def deserialize(path: Path | str) -> dict[str, Any]:
    """Deserialize data from a yaml file."""
    with Path(path).open("r") as file_stream:
        return yaml.full_load(file_stream)  # type: ignore

empty_file #

empty_file(file_path: Path) -> bool

Check if a file does not exist, or if it does, if it is empty.

Source code in neps/utils/files.py
def empty_file(file_path: Path) -> bool:
    """Check if a file does not exist, or if it does, if it is empty."""
    return not file_path.exists() or file_path.stat().st_size <= 0

serialize #

serialize(
    data: Any, path: Path | str, *, sort_keys: bool = True
) -> None

Serialize data to a yaml file.

Source code in neps/utils/files.py
def serialize(data: Any, path: Path | str, *, sort_keys: bool = True) -> None:
    """Serialize data to a yaml file."""
    data = _serializable_format(data)
    path = Path(path)
    with path.open("w") as file_stream:
        try:
            return yaml.safe_dump(data, file_stream, sort_keys=sort_keys)
        except yaml.representer.RepresenterError as e:
            raise TypeError(
                "Could not serialize to yaml! The object "
                f"{e.args[1]} of type {type(e.args[1])} is not."
            ) from e