kappybara.algebra#

Functions

parse_operator(kappa_operator)

Convert a Kappa string operator to a Python function.

Classes

Expression(type, **attrs)

Algebraic expressions as specified by the Kappa language.

class kappybara.algebra.Expression(type, **attrs)[source]#

Algebraic expressions as specified by the Kappa language.

type#

Type of expression (literal, variable, binary_op, etc.).

attrs#

Dictionary of attributes specific to the expression type.

evaluate(system=None)[source]#

Evaluate the expression to get its value.

Parameters:

system (System | None) – System context for variable evaluation (required for variables).

Returns:

Result of evaluating the expression.

Raises:

ValueError – If evaluation fails due to missing context or unsupported type.

Return type:

int | float

filter(type_str)[source]#

Returns all nodes in the expression tree whose type matches the provided string.

Note

Doesn’t detect nodes indirectly nested in named variables.

Parameters:

type_str (str)

Return type:

list[Self]

classmethod from_kappa(kappa_str)[source]#

Parse an Expression from a Kappa string.

Parameters:

kappa_str (str) – Kappa expression string.

Returns:

Parsed Expression object.

Raises:

AssertionError – If the string doesn’t represent a valid expression.

Return type:

Self

property kappa_str: str#

Get the expression representation in Kappa format.

Returns:

Kappa string representation of the expression.

Raises:

ValueError – If expression type is not supported for string conversion.

kappybara.algebra.parse_operator(kappa_operator)[source]#

Convert a Kappa string operator to a Python function.

Parameters:

kappa_operator (str) – Kappa language operator string.

Returns:

Python function counterpart.

Raises:

ValueError – If the operator is not recognized.

Return type:

Callable