Skip to content

JsonDumpReporter

The JsonDumpReporter reports the capsule in JSON format. It can be created using the capsula.JsonDumpReporter.builder method or the capsula.JsonDumpReporter.__init__ method.

capsula.JsonDumpReporter.builder classmethod

builder(
    *, option: int | None = None
) -> Callable[[CapsuleParams], JsonDumpReporter]
PARAMETER DESCRIPTION
option

Option to pass to orjson.dumps. If not provided, orjson.OPT_INDENT_2 will be used.

TYPE: int | None DEFAULT: None

Source code in capsula/_reporter/_json.py
@classmethod
def builder(
    cls,
    *,
    option: Annotated[
        int | None,
        Doc("Option to pass to `orjson.dumps`. If not provided, `orjson.OPT_INDENT_2` will be used."),
    ] = None,
) -> Callable[[CapsuleParams], JsonDumpReporter]:
    def build(params: CapsuleParams) -> JsonDumpReporter:
        return cls(
            params.run_dir / f"{params.phase}-run-report.json",
            option=orjson.OPT_INDENT_2 if option is None else option,
        )

    return build

capsula.JsonDumpReporter.__init__

__init__(
    path: Path | str,
    *,
    default: Callable[[Any], Any] | None = None,
    option: int | None = None,
    mkdir: bool = True
)
PARAMETER DESCRIPTION
path

TYPE: Path | str

default

TYPE: Callable[[Any], Any] | None DEFAULT: None

option

TYPE: int | None DEFAULT: None

mkdir

TYPE: bool DEFAULT: True

Source code in capsula/_reporter/_json.py
def __init__(
    self,
    path: Path | str,
    *,
    default: Callable[[Any], Any] | None = None,
    option: int | None = None,
    mkdir: bool = True,
) -> None:
    self._path = Path(path)
    if mkdir:
        self._path.parent.mkdir(parents=True, exist_ok=True)

    if default is None:
        self._default_for_encoder = default_preset
    else:

        def _default(obj: Any) -> Any:
            try:
                return default_preset(obj)
            except TypeError:
                return default(obj)

        self._default_for_encoder = _default

    self._option = option

Configuration example

Via capsula.toml

[pre-run]
reporters = [{ type = "JsonDumpReporter" }]

[in-run]
reporters = [{ type = "JsonDumpReporter" }]

[post-run]
reporters = [{ type = "JsonDumpReporter" }]

Via @capsula.reporter decorator

import capsula

@capsula.run()
@capsula.reporter(capsula.JsonDumpReporter.builder(), mode="all")
def func(): ...

Output

It will output the pre-run, in-run, and post-run capsules to in-run-report.json, pre-run-report.json, and post-run-report.json in the run directory, respectively.