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 _return_type is taken from the scalar's 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 ``_return_type`` is taken from the scalar's type.

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

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

    def __post_init__(self) -> None:
        """Resolve ``_return_type`` from ``value`` and encode it as JSON."""
        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, "_return_type", cgtype)
        object.__setattr__(self, "value", cgtype.encode(raw))
        super().__post_init__()

__post_init__() #

Resolve _return_type from value and encode it as JSON.

Source code in capturegraph-lib/capturegraph/procedures/nodes/process/constant.py
def __post_init__(self) -> None:
    """Resolve ``_return_type`` from ``value`` and encode it as JSON."""
    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, "_return_type", cgtype)
    object.__setattr__(self, "value", cgtype.encode(raw))
    super().__post_init__()

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
    if isinstance(value, JSONScalar):
        return Constant(value=value)
    if isinstance(value, bool):
        return Constant(value=Bool(value=value))
    if isinstance(value, (int, float)):
        return Constant(value=Number(value=value))
    if isinstance(value, str):
        return Constant(value=String(value=value))
    raise TypeError(f"cannot lift {type(value).__name__} into a Constant")