Skip to content

raplan.yaml

RaPlan input/output module

from_yaml

from_yaml(cls: type[T], path: str | Path) -> T

Load any supported object from a YAML file.

Parameters:

Name Type Description Default
cls type[T]

Class or type of object to import.

required
path str | Path

Path to the YAML file.

required

Returns:

Type Description
T

An instance of type cls.

Note

If you wish to import from a YAML string instead, see raplan.io.from_yaml, which is an alias of the excellent serde.yaml.from_yaml

Source code in src/raplan/yaml.py
def from_yaml(cls: type[T], path: str | Path) -> T:
    """Load any supported object from a YAML file.

    Arguments:
        cls: Class or type of object to import.
        path: Path to the YAML file.

    Returns:
        An instance of type `cls`.

    Note:
        If you wish to import from a YAML string instead, see `raplan.io.from_yaml`, which is an
        alias of the excellent `serde.yaml.from_yaml`
    """

    ser = Path(path).read_text(encoding="utf-8")
    return serde.yaml.from_yaml(cls, ser)

to_yaml

to_yaml(
    obj: Any, path: str | Path | None = None
) -> str | None

Save an object to a YAML file.

Parameters:

Name Type Description Default
obj Any

Any object from the raplan.classes or raplan.distributions modules.

required
path str | Path | None

Path to YAML file to export to.

None
Note

If you wish to serialize to a YAML string instead, see raplan.io.to_yaml, which is an alias of the excellent serde.yaml.to_yaml.

Source code in src/raplan/yaml.py
def to_yaml(obj: Any, path: str | Path | None = None) -> str | None:
    """Save an object to a YAML file.

    Arguments:
        obj: Any object from the `raplan.classes` or `raplan.distributions` modules.
        path: Path to YAML file to export to.

    Note:
        If you wish to serialize to a YAML string instead, see `raplan.io.to_yaml`, which is an
        alias of the excellent `serde.yaml.to_yaml`.
    """

    ser = serde.yaml.to_yaml(obj)
    if path is None:
        return ser
    else:
        Path(path).write_text(ser, encoding="utf-8")