Skip to content

cache

cache #

The single cache node — store a value at a path and return it.

CacheProcedure replaces the old file/bundle variants: the destination is a Path[T] position, which already encodes whether it is a file or a directory bundle, so one node covers both. It type-checks the value against the position by structure, runs the value procedure, stores the result, and returns it (so the cached value can flow on as T).

CacheProcedure #

Bases: Procedure[T]

Store value at destination and return the cached value.

The destination is a Path[T]; the value must match its target type by structure (Array[Image] and ImageSequence interchange, Image into a Thumbnail slot is rejected). The |= / &= operators lower path |= value (skip if exists) and path &= value (overwrite) to this node.

Attributes:

Name Type Description
destination Procedure[Path]

The Path position to store at.

value Procedure[CGType]

The procedure producing the value to store.

skip_if_exists Setting[bool]

If true, keep an existing stored value instead of re-running value and overwriting it.

Source code in capturegraph-lib/capturegraph/procedures/nodes/destination/cache.py
@make_procedure
class CacheProcedure[T: CGType](Procedure[T]):
    """Store ``value`` at ``destination`` and return the cached value.

    The destination is a ``Path[T]``; the value must match its target type by
    structure (``Array[Image]`` and ``ImageSequence`` interchange, ``Image`` into
    a ``Thumbnail`` slot is rejected). The ``|=`` / ``&=`` operators lower
    ``path |= value`` (skip if exists) and ``path &= value`` (overwrite) to this
    node.

    Attributes:
        destination: The ``Path`` position to store at.
        value: The procedure producing the value to store.
        skip_if_exists: If true, keep an existing stored value instead of
            re-running ``value`` and overwriting it.
    """

    destination: Procedure[Path]
    value: Procedure[CGType]
    skip_if_exists: Setting[bool] = False

    def __post_init__(self) -> None:
        """Resolve ``_return_type`` to the value's type, validating it matches."""
        super().__post_init__()
        target = path_target(self.destination)
        value_type = self.value._return_type
        if not structural_eq(value_type, target):
            raise TypeError(
                f"CacheProcedure: cannot store "
                f"{getattr(value_type, '__name__', value_type)} at a "
                f"{getattr(target, '__name__', target)} slot"
            )
        object.__setattr__(self, "_return_type", value_type)

__post_init__() #

Resolve _return_type to the value's type, validating it matches.

Source code in capturegraph-lib/capturegraph/procedures/nodes/destination/cache.py
def __post_init__(self) -> None:
    """Resolve ``_return_type`` to the value's type, validating it matches."""
    super().__post_init__()
    target = path_target(self.destination)
    value_type = self.value._return_type
    if not structural_eq(value_type, target):
        raise TypeError(
            f"CacheProcedure: cannot store "
            f"{getattr(value_type, '__name__', value_type)} at a "
            f"{getattr(target, '__name__', target)} slot"
        )
    object.__setattr__(self, "_return_type", value_type)