Skip to content

conditional

conditional #

cg.when(condition) — collect a block into the true branch of an IfThenElse.

The block runs only when condition is true; the else branch does nothing. The condition node is built by the caller (before the with), so it is recorded in the enclosing context and caught by the IfThenElse. A path cached inside the block stays assigned and readable for the rest of the procedure.

ConditionalContext #

Bases: ProcedureContext

Reduce a block's roots to the true branch of an IfThenElse.

Source code in capturegraph-lib/capturegraph/procedures/procedure/authoring/contexts/conditional.py
class ConditionalContext(ProcedureContext):
    """Reduce a block's roots to the true branch of an ``IfThenElse``."""

    def __init__(self, condition: Procedure[Bool]) -> None:
        """Reduce the block into the true branch guarded by ``condition``."""
        super().__init__()
        self._condition = condition

    def _finish(self) -> None:
        from capturegraph.procedures.nodes.process.control import (
            DoNothing,
            IfThenElse,
        )
        from capturegraph.procedures.nodes.process.structural import (
            ProcedureSequence,
        )

        IfThenElse(
            condition=self._condition,
            true_branch=ProcedureSequence(procedures=self._step_roots()),
            false_branch=DoNothing(),
        )

__init__(condition) #

Reduce the block into the true branch guarded by condition.

Source code in capturegraph-lib/capturegraph/procedures/procedure/authoring/contexts/conditional.py
def __init__(self, condition: Procedure[Bool]) -> None:
    """Reduce the block into the true branch guarded by ``condition``."""
    super().__init__()
    self._condition = condition

when(condition) #

Run the with block only when condition is true (else do nothing).

Pairs naturally with path.exists() to guard a block on a value's presence: with cg.when(root.gps_bounds.exists()):.

Source code in capturegraph-lib/capturegraph/procedures/procedure/authoring/contexts/conditional.py
@contextmanager
def when(condition: Procedure[Bool]) -> Iterator[None]:
    """Run the ``with`` block only when ``condition`` is true (else do nothing).

    Pairs naturally with ``path.exists()`` to guard a block on a value's presence:
    ``with cg.when(root.gps_bounds.exists()):``.
    """
    with ConditionalContext(condition).collect():
        yield