Skip to content

Pinned data

data

Data files of an experiment: pinned content hashes and readers.

A data clause names a file and pins its content hash. The pin is part of the experiment's hash, so the hash of an experiment is known without reading its data; reading checks the file against the pin, and a file whose content does not match is refused, as an import with a mismatched integrity check is refused in Dhall. A file is read by the reader registered for its suffix (register_reader); JSON and CSV readers are built in.

PIN_ALGORITHM module-attribute

PIN_ALGORITHM = 'blake3'

The hash algorithm a data pin names: BLAKE3, the digest every content identity of the language is built from.

DataReader

DataReader = Callable[[bytes], JsonValue]

Reads a data file's bytes as a JSON-shaped value.

file_digest

file_digest(data: bytes) -> str

The digest a data pin records for a file's bytes.

PARAMETER DESCRIPTION
data

The file's content.

TYPE: bytes

RETURNS DESCRIPTION
str

Its 64-character lowercase hexadecimal BLAKE3 digest.

Source code in src/quivers/experiments/data.py
def file_digest(data: bytes) -> str:
    """The digest a data pin records for a file's bytes.

    Parameters
    ----------
    data : bytes
        The file's content.

    Returns
    -------
    str
        Its 64-character lowercase hexadecimal BLAKE3 digest.
    """
    return content_digest(data)

render_pin

render_pin(digest: str) -> str

The pin to write for a digest.

PARAMETER DESCRIPTION
digest

A BLAKE3 digest.

TYPE: str

RETURNS DESCRIPTION
str

@blake3:<digest>.

Source code in src/quivers/experiments/data.py
def render_pin(digest: str) -> str:
    """The pin to write for a digest.

    Parameters
    ----------
    digest : str
        A BLAKE3 digest.

    Returns
    -------
    str
        ``@blake3:<digest>``.
    """
    return ContentPin(algorithm=PIN_ALGORITHM, digest=digest).render()

register_reader

register_reader(suffix: str, reader: DataReader) -> None

Teach experiments to read data files with another suffix.

PARAMETER DESCRIPTION
suffix

The file suffix, such as ".parquet".

TYPE: str

reader

Reads a file's bytes as a JSON-shaped value (numbers, strings, Booleans, lists, and string-keyed objects).

TYPE: DataReader

RAISES DESCRIPTION
ValueError

If a reader for the suffix is already registered.

Source code in src/quivers/experiments/data.py
def register_reader(suffix: str, reader: DataReader) -> None:
    """Teach experiments to read data files with another suffix.

    Parameters
    ----------
    suffix : str
        The file suffix, such as ``".parquet"``.
    reader : DataReader
        Reads a file's bytes as a JSON-shaped value (numbers, strings,
        Booleans, lists, and string-keyed objects).

    Raises
    ------
    ValueError
        If a reader for the suffix is already registered.
    """
    if suffix in _READERS:
        raise ValueError(f"a data reader for {suffix!r} is already registered")
    _READERS[suffix] = reader

read_pinned

read_pinned(path: Path, pin: ContentPin | None, *, origin: str, line: int, column: int) -> tuple[str, JsonValue]

Read a pinned data file, checking its content hash.

PARAMETER DESCRIPTION
path

The file.

TYPE: Path

pin

The pin written in the experiment.

TYPE: ContentPin | None

origin

The experiment's file, for the refusal's location.

TYPE: str

line

The clause's line.

TYPE: int

column

The clause's column.

TYPE: int

RETURNS DESCRIPTION
tuple[str, JsonValue]

The file's digest and its value.

RAISES DESCRIPTION
ExperimentError

With code experiment-data-pin if the clause pins nothing, pins another algorithm, or pins a digest the file does not have (the message gives the pin to write); experiment-data-read if the file cannot be read or no reader knows its suffix.

Source code in src/quivers/experiments/data.py
def read_pinned(
    path: Path, pin: ContentPin | None, *, origin: str, line: int, column: int
) -> tuple[str, JsonValue]:
    """Read a pinned data file, checking its content hash.

    Parameters
    ----------
    path : Path
        The file.
    pin : ContentPin | None
        The pin written in the experiment.
    origin : str
        The experiment's file, for the refusal's location.
    line : int
        The clause's line.
    column : int
        The clause's column.

    Returns
    -------
    tuple[str, JsonValue]
        The file's digest and its value.

    Raises
    ------
    ExperimentError
        With code ``experiment-data-pin`` if the clause pins nothing, pins
        another algorithm, or pins a digest the file does not have (the
        message gives the pin to write); ``experiment-data-read`` if the
        file cannot be read or no reader knows its suffix.
    """
    try:
        data = path.read_bytes()
    except OSError as error:
        raise ExperimentError(
            "experiment-data-read",
            f"cannot read data file {path}: {error.strerror or error}",
            file=origin,
            line=line,
            column=column,
        ) from error
    digest = file_digest(data)
    check_pin(path, pin, digest, origin=origin, line=line, column=column)
    reader = next(
        (
            _READERS[suffix]
            for suffix in sorted(_READERS, key=len, reverse=True)
            if path.name.endswith(suffix)
        ),
        None,
    )
    if reader is None:
        raise ExperimentError(
            "experiment-data-read",
            f"no data reader for {path.name}; readers exist for "
            + ", ".join(sorted(_READERS)),
            file=origin,
            line=line,
            column=column,
        )
    try:
        return digest, reader(data)
    except (ValueError, UnicodeDecodeError) as error:
        raise ExperimentError(
            "experiment-data-read",
            f"data file {path} does not read: {error}",
            file=origin,
            line=line,
            column=column,
        ) from error

check_pin

check_pin(path: Path, pin: ContentPin | None, digest: str, *, origin: str, line: int, column: int) -> None

Refuse a file whose digest is not the one pinned.

PARAMETER DESCRIPTION
path

The file.

TYPE: Path

pin

The pin written in the experiment.

TYPE: ContentPin | None

digest

The file's BLAKE3 digest.

TYPE: str

origin

The experiment's file.

TYPE: str

line

The clause's line.

TYPE: int

column

The clause's column.

TYPE: int

RAISES DESCRIPTION
ExperimentError

With code experiment-data-pin when the pin is missing, names another algorithm, or differs from the digest.

Source code in src/quivers/experiments/data.py
def check_pin(
    path: Path,
    pin: ContentPin | None,
    digest: str,
    *,
    origin: str,
    line: int,
    column: int,
) -> None:
    """Refuse a file whose digest is not the one pinned.

    Parameters
    ----------
    path : Path
        The file.
    pin : ContentPin | None
        The pin written in the experiment.
    digest : str
        The file's BLAKE3 digest.
    origin : str
        The experiment's file.
    line : int
        The clause's line.
    column : int
        The clause's column.

    Raises
    ------
    ExperimentError
        With code ``experiment-data-pin`` when the pin is missing, names
        another algorithm, or differs from the digest.
    """
    if pin is None:
        raise ExperimentError(
            "experiment-data-pin",
            f"{path.name} is not pinned, so the experiment's hash would not "
            f"cover its content; write {render_pin(digest)} after the path",
            file=origin,
            line=line,
            column=column,
        )
    if pin.algorithm != PIN_ALGORITHM:
        raise ExperimentError(
            "experiment-data-pin",
            f"{path.name} is pinned with {pin.algorithm}, and pins are "
            f"{PIN_ALGORITHM} digests; write {render_pin(digest)}",
            file=origin,
            line=line,
            column=column,
        )
    if pin.digest != digest:
        raise ExperimentError(
            "experiment-data-pin",
            f"{path.name} has content hash {PIN_ALGORITHM}:{digest}, not the "
            f"pinned {PIN_ALGORITHM}:{pin.digest}; the file changed since it "
            f"was pinned (pin the new content with {render_pin(digest)} if "
            "the change is intended)",
            file=origin,
            line=line,
            column=column,
        )