Skip to content

README

liblaf.cherries.utils

Classes:

  • GitUrlParsed

    Subset of attributes returned by giturlparse.parse.

Functions:

  • flatten_dict

    Flatten nested mappings into slash-delimited keys.

  • giturlparse

    Parse a Git remote URL with the attributes Cherries needs.

  • pretty_yaml

    Serialize data as YAML, converting path-like objects to strings.

  • relative_or_absolute

    Return path relative to prefix, or as an absolute path.

  • relative_or_name

    Return path relative to prefix, or just its file name.

  • unflatten_dict

    Expand slash-delimited keys into nested dictionaries.

GitUrlParsed

Bases: Protocol


              flowchart TD
              liblaf.cherries.utils.GitUrlParsed[GitUrlParsed]

              

              click liblaf.cherries.utils.GitUrlParsed href "" "liblaf.cherries.utils.GitUrlParsed"
            

Subset of attributes returned by giturlparse.parse.

References
  1. https://github.com/nephila/giturlparse/blob/master/README.rst

Attributes:

host property

host: str

owner property

owner: str

platform property

platform: str

repo property

repo: str

flatten_dict

flatten_dict(
    mapping: Mapping[str, Any],
    prefix: str = "",
    sep: str = "/",
) -> dict[str, Any]

Flatten nested mappings into slash-delimited keys.

Parameters:

  • mapping (Mapping[str, Any]) –

    Mapping to flatten.

  • prefix (str, default: '' ) –

    Prefix already accumulated by a recursive call.

  • sep (str, default: '/' ) –

    Separator inserted between nested key segments.

Returns:

  • dict[str, Any]

    A one-level dictionary whose keys preserve the nested path.

Examples:

>>> flatten_dict({"train": {"loss": 0.4}, "epoch": 3})
{'train/loss': 0.4, 'epoch': 3}
Source code in src/liblaf/cherries/utils/_dict.py
def flatten_dict(
    mapping: Mapping[str, Any], prefix: str = "", sep: str = "/"
) -> dict[str, Any]:
    """Flatten nested mappings into slash-delimited keys.

    Args:
        mapping: Mapping to flatten.
        prefix: Prefix already accumulated by a recursive call.
        sep: Separator inserted between nested key segments.

    Returns:
        A one-level dictionary whose keys preserve the nested path.

    Examples:
        >>> flatten_dict({"train": {"loss": 0.4}, "epoch": 3})
        {'train/loss': 0.4, 'epoch': 3}
    """
    result: dict[str, Any] = {}
    for key, value in mapping.items():
        key_flat: str = f"{prefix}{sep}{key}" if prefix else key
        if isinstance(value, Mapping):
            result.update(flatten_dict(value, key_flat, sep))
        else:
            result[key_flat] = value
    return result

giturlparse

giturlparse(url: str) -> GitUrlParsed

Parse a Git remote URL with the attributes Cherries needs.

Examples:

>>> info = giturlparse("https://github.com/liblaf/cherries.git")
>>> (info.platform, info.owner, info.repo)
('github', 'liblaf', 'cherries')
Source code in src/liblaf/cherries/utils/_git.py
def giturlparse(url: str) -> GitUrlParsed:
    """Parse a Git remote URL with the attributes Cherries needs.

    Examples:
        >>> info = giturlparse("https://github.com/liblaf/cherries.git")
        >>> (info.platform, info.owner, info.repo)
        ('github', 'liblaf', 'cherries')
    """
    info: GitUrlParsed = _giturlparse.parse(url)  # pyright: ignore[reportAssignmentType]
    return info

pretty_yaml

pretty_yaml(data: dict[str, Any]) -> str

Serialize data as YAML, converting path-like objects to strings.

Source code in src/liblaf/cherries/utils/_yaml.py
def pretty_yaml(data: dict[str, Any]) -> str:
    """Serialize `data` as YAML, converting path-like objects to strings."""
    return msgspec.yaml.encode(data, enc_hook=_enc_hook).decode()

relative_or_absolute

relative_or_absolute(path: Path, prefix: Path) -> Path

Return path relative to prefix, or as an absolute path.

Parameters:

  • path (Path) –

    Path to display or serialize.

  • prefix (Path) –

    Directory that should be stripped when path is inside it.

Returns:

  • Path

    path.relative_to(prefix) when possible; otherwise path.resolve().

Examples:

>>> relative_or_absolute(
...     Path("/tmp/cherries/data/out.txt"), Path("/tmp/cherries")
... )
PosixPath('data/out.txt')
>>> relative_or_absolute(Path("/var/tmp/out.txt"), Path("/tmp/cherries"))
PosixPath('/var/tmp/out.txt')
Source code in src/liblaf/cherries/utils/_path.py
def relative_or_absolute(path: Path, prefix: Path) -> Path:
    """Return `path` relative to `prefix`, or as an absolute path.

    Args:
        path: Path to display or serialize.
        prefix: Directory that should be stripped when `path` is inside it.

    Returns:
        `path.relative_to(prefix)` when possible; otherwise `path.resolve()`.

    Examples:
        >>> relative_or_absolute(
        ...     Path("/tmp/cherries/data/out.txt"), Path("/tmp/cherries")
        ... )
        PosixPath('data/out.txt')
        >>> relative_or_absolute(Path("/var/tmp/out.txt"), Path("/tmp/cherries"))
        PosixPath('/var/tmp/out.txt')
    """
    if path.is_relative_to(prefix):
        return path.relative_to(prefix)
    return path.resolve()

relative_or_name

relative_or_name(path: Path, prefix: Path) -> Path

Return path relative to prefix, or just its file name.

This keeps copied artifact names short even when a user logs a file outside the experiment working directory.

Parameters:

  • path (Path) –

    Path to display or copy.

  • prefix (Path) –

    Directory that should be stripped when path is inside it.

Returns:

  • Path

    path.relative_to(prefix) when possible; otherwise Path(path.name).

Examples:

>>> relative_or_name(Path("/tmp/cherries/data/out.txt"), Path("/tmp/cherries"))
PosixPath('data/out.txt')
>>> relative_or_name(Path("/var/tmp/out.txt"), Path("/tmp/cherries"))
PosixPath('out.txt')
Source code in src/liblaf/cherries/utils/_path.py
def relative_or_name(path: Path, prefix: Path) -> Path:
    """Return `path` relative to `prefix`, or just its file name.

    This keeps copied artifact names short even when a user logs a file outside
    the experiment working directory.

    Args:
        path: Path to display or copy.
        prefix: Directory that should be stripped when `path` is inside it.

    Returns:
        `path.relative_to(prefix)` when possible; otherwise `Path(path.name)`.

    Examples:
        >>> relative_or_name(Path("/tmp/cherries/data/out.txt"), Path("/tmp/cherries"))
        PosixPath('data/out.txt')
        >>> relative_or_name(Path("/var/tmp/out.txt"), Path("/tmp/cherries"))
        PosixPath('out.txt')
    """
    if path.is_relative_to(prefix):
        return path.relative_to(prefix)
    return Path(path.name)

unflatten_dict

unflatten_dict(
    mapping: Mapping[str, Any], sep: str = "/"
) -> dict[str, Any]

Expand slash-delimited keys into nested dictionaries.

Parameters:

  • mapping (Mapping[str, Any]) –

    Flat mapping to expand.

  • sep (str, default: '/' ) –

    Separator used between nested key segments.

Returns:

Examples:

>>> unflatten_dict({"train/loss": 0.4, "epoch": 3})
{'train': {'loss': 0.4}, 'epoch': 3}
Source code in src/liblaf/cherries/utils/_dict.py
def unflatten_dict(mapping: Mapping[str, Any], sep: str = "/") -> dict[str, Any]:
    """Expand slash-delimited keys into nested dictionaries.

    Args:
        mapping: Flat mapping to expand.
        sep: Separator used between nested key segments.

    Returns:
        A nested dictionary.

    Examples:
        >>> unflatten_dict({"train/loss": 0.4, "epoch": 3})
        {'train': {'loss': 0.4}, 'epoch': 3}
    """
    result: dict[str, Any] = {}
    _insert(result, [], mapping, sep=sep)
    return result