Skip to content

constant

constant #

The unified constant node — an inline value of any JSON-backed scalar type.

One Constant node replaces the old per-type constants: it carries any inline-JSON scalar value (Bool, Number, String, Time, Location, ...) as that type's JSON encoding, and its return type is the scalar's type, so any platform decodes the value through the return type. This is also the seam a server interceptor uses to compute a value of any type at generation time and drop it straight into the graph.

Constant #

Bases: Procedure[T]

An inline constant value of a JSON-backed scalar type T.

Construct it from a scalar instance — Constant(value=Number(value=42)) — or lift a raw value with [const][]. The value is stored as its JSON encoding and the node's return type is the scalar's own type.

Attributes:

Name Type Description
value Setting[JSONScalar | bool | int | float | str | dict | list]

A scalar instance at construction; replaced by its JSON encoding.

Source code in capturegraph-lib/capturegraph/procedures/nodes/process/constant.py
@make_procedure
class Constant[T: JSONScalar](Procedure[T]):
    """An inline constant value of a JSON-backed scalar type ``T``.

    Construct it from a scalar instance — ``Constant(value=Number(value=42))`` —
    or lift a raw value with [const][]. The value is stored as its JSON
    encoding and the node's return type is the scalar's own type.

    Attributes:
        value: A scalar instance at construction; replaced by its JSON encoding.
    """

    value: Setting[JSONScalar | bool | int | float | str | dict | list]

    def _resolve_type(self) -> type[CGType]:
        """The scalar's own type, consuming ``value`` into its JSON encoding."""
        raw = self.value
        if not isinstance(raw, JSONScalar):
            raise TypeError(
                f"Constant value must be a JSONScalar instance "
                f"(e.g. cg.Number(value=5)); got {type(raw).__name__}. "
                f"Use const(...) to lift a raw value."
            )
        cgtype = type(raw)
        object.__setattr__(self, "value", cgtype.encode(raw))
        return cgtype

as_scalar(value) #

The JSONScalar a raw bool / int / float / str stands for.

A scalar instance is returned unchanged. This is the value a Constant carries, without building the node — so a literal can be compared for equality (a map key naming a slot) before any node exists.

Raises:

Type Description
TypeError

If value is not a scalar or a liftable raw literal.

Source code in capturegraph-lib/capturegraph/procedures/nodes/process/constant.py
def as_scalar(value: object) -> JSONScalar:
    """The ``JSONScalar`` a raw ``bool`` / ``int`` / ``float`` / ``str`` stands for.

    A scalar instance is returned unchanged. This is the value a ``Constant``
    carries, without building the node — so a literal can be compared for
    equality (a map key naming a slot) before any node exists.

    Raises:
        TypeError: If ``value`` is not a scalar or a liftable raw literal.
    """
    if isinstance(value, JSONScalar):
        return value
    if isinstance(value, bool):
        return Bool(value=value)
    if isinstance(value, (int, float)):
        return Number(value=value)
    if isinstance(value, str):
        return String(value=value)
    raise TypeError(f"cannot lift {type(value).__name__} into a Constant")

const(value) #

Lift a scalar value or instance into a Constant node.

Accepts an existing Constant (returned as-is), any JSONScalar instance, or a raw bool / int / float / str.

Source code in capturegraph-lib/capturegraph/procedures/nodes/process/constant.py
def const(value: object) -> Constant:
    """Lift a scalar value or instance into a ``Constant`` node.

    Accepts an existing ``Constant`` (returned as-is), any ``JSONScalar``
    instance, or a raw ``bool`` / ``int`` / ``float`` / ``str``.
    """
    if isinstance(value, Constant):
        return value
    return Constant(value=as_scalar(value))