Skip to content

Environment API Reference

Environment

Environment for managing model variables and their relationships.

An Environment is a container that manages all variables created by the user. It ensures that variables used together come from the same environment and maintains consistency across multiple expressions.

Environments serve as context managers to automatically manage variable scoping.

Attributes:

  • num_variables (int) –

    The number of variables registered in this environment.

  • id (int) –

    Unique identifier for this environment.

Examples:

Use as a context manager:

>>> from luna_model import Environment, Variable
>>> with Environment() as env:
...     x = Variable("x", env=env)
...     y = Variable("y", env=env)

Create and manage explicitly:

>>> env = Environment()
>>> x = Variable("x", env=env)
>>> print(env.num_variables)  # 1
1
>>> var = env.get_variable("x")
Notes

Variables from different environments cannot be combined in the same expression or constraint. This prevents accidental mixing of unrelated models.

num_variables: int property

Get the number of variables in this environment.

Returns:

  • int

    The number of registered variables.

id: int property

Get the unique identifier for this environment.

Returns:

  • int

    The environment ID.

__init__() -> None

Initialize a new environment.

__enter__() -> Environment

Enter the environment context.

__exit__(exc_type, exc_value, exc_traceback) -> None

Exit the environment context.

get_variable(name: str) -> Variable

Get a variable by name.

Parameters:

  • name (str) –

    The variable name.

Returns:

  • Variable

    The variable with the given name.

Raises:

  • VariableNotExistingError

    If no variable with the given name exists.

variables() -> list[Variable]

Get all variables in this environment.

Returns:

equal_contents(other: Environment) -> bool

Check if two environments have equal content.

Parameters:

  • other (Environment) –

    The environment to compare with.

Returns:

  • bool

    True if environments have the same variables.

encode() -> bytes

Serialize the environment into a compact binary format.

Returns:

  • bytes

    Encoded environment representation.

serialize() -> bytes

Serialize the environment into a compact binary format.

This is an alias for :meth:encode.

Returns:

  • bytes

    Encoded environment representation.

decode(data: bytes) -> Environment classmethod

Reconstruct an environment from encoded bytes.

Parameters:

  • data (bytes) –

    Binary blob returned by :meth:encode or :meth:serialize.

Returns:

Raises:

  • DecodingError

    If decoding fails due to corruption or incompatibility.

Examples:

>>> original = Environment()
>>> blob = original.encode()
>>> restored = Environment.decode(blob)

deserialize(data: bytes) -> Environment classmethod

Reconstruct an environment from encoded bytes.

This is an alias for :meth:decode.

Parameters:

  • data (bytes) –

    Binary blob returned by encode().

Returns:

  • Model

    Deserialized environment object.

__reduce__() -> tuple[Callable[[bytes], Environment], tuple[bytes]]

Support for pickle serialization.

Returns:

  • tuple

    A tuple of (decoder_function, encoded_data) for pickle.

Notes

This method is called automatically by Python's pickle module.

__eq__(other: Environment) -> bool

Check if two environments are exactly equal.

__contains__(var: str) -> bool

Check if a variable name exists in this environment.

Parameters:

  • var (str) –

    The variable name to check.

Returns:

  • bool

    True if a variable with the given name exists.

__str__() -> str

Environment as a string.