apparun.expressions#

This module contains all the functions and classes used to manipulate the expressions for the parameters of an impact model.

Classes#

ParamEnumConst

Represents an constant enum type.

ParamEnumExpr

Represents an enum type expression.

ParamExpr

Base class for the expressions used as values for the parameters of

ParamFloatConst

Represents a float type constant.

ParamFloatExpr

Represents a float type expression.

ParamsValuesSet

Represents a set of expressions for a set of parameters of an impact model.

Functions#

parse_expr(→ sympy.Expr)

Parses an arithmetic expression using sympy.

validate_expr(→ bool)

Check if an expression is a valid arithmetic expression that

Module Contents#

class apparun.expressions.ParamEnumConst#

Bases: ParamExpr

Represents an constant enum type.

evaluate(dependencies_values: Dict[str, float | int | str]) float | int | str#

Evaluate this expression based on the values of its dependencies.

Parameters:

dependencies_values – the values of the dependencies of this expression.

Returns:

the evaluated value for this expression.

property is_complex: bool#
Returns:

True if the expression is a complex one, else False.

property raw_version#
Returns:

the raw version of the expression.

value: str#
class apparun.expressions.ParamEnumExpr#

Bases: ParamExpr

Represents an enum type expression.

evaluate(dependencies_values: Dict[str, float | int | str]) float | int | str#

Evaluate this expression based on the values of its dependencies.

Parameters:

dependencies_values – the values of the dependencies of this expression.

Returns:

the evaluated value for this expression.

classmethod parse_sub_exprs(data: Any, info: pydantic_core.core_schema.ValidationInfo) Any#

Validator use to parse the sub expressions.

classmethod validate_options(data: Any, info: pydantic_core.core_schema.ValidationInfo) Any#

Check the expression contains all the options of the parameter used in it with no extra options. Also check that each option has an associated sub expression.

classmethod validate_param(data: Any, info: pydantic_core.core_schema.ValidationInfo) Any#

Check the parameter used in the expression is an existing parameter of the impact model and that is an enum type parameter.

property dependencies: List[str]#
Returns:

the list of the parameters whose are the dependencies of this expression.

property is_complex: bool#
Returns:

True if the expression is a complex one, else False.

options: Dict[str, ParamExpr]#
param: str#
property raw_version#
Returns:

the raw version of the expression.

class apparun.expressions.ParamExpr#

Bases: pydantic.BaseModel, abc.ABC

Base class for the expressions used as values for the parameters of an impact model.

abstractmethod evaluate(dependencies_values: Dict[str, float | int | str]) float | int | str#

Evaluate this expression based on the values of its dependencies.

Parameters:

dependencies_values – the values of the dependencies of this expression.

Returns:

the evaluated value for this expression.

classmethod parse(raw_expr: float | int | str | dict, param: str, parameters: apparun.parameters.ImpactModelParams) ParamExpr#

Parse an expression.

Parameters:
  • raw_expr – the expression to parse.

  • param – name of the parameter associated to this expression.

  • parameters – information about the parameters of the impact model.

Returns:

the parsed expression.

property dependencies: List[str]#
Returns:

the list of the parameters whose are the dependencies of this expression.

property is_complex: bool#
Abstractmethod:

Returns:

True if the expression is a complex one, else False.

abstract property raw_version#
Returns:

the raw version of the expression.

class apparun.expressions.ParamFloatConst#

Bases: ParamExpr

Represents a float type constant.

evaluate(dependencies_values: Dict[str, float | int | str]) float | int | str#

Evaluate this expression based on the values of its dependencies.

Parameters:

dependencies_values – the values of the dependencies of this expression.

Returns:

the evaluated value for this expression.

property is_complex: bool#
Returns:

True if the expression is a complex one, else False.

property raw_version#
Returns:

the raw version of the expression.

value: float#
class apparun.expressions.ParamFloatExpr#

Bases: ParamExpr

Represents a float type expression.

evaluate(dependencies_values: Dict[str, float | int | str]) float | int | str#

Evaluate this expression based on the values of its dependencies.

Parameters:

dependencies_values – the values of the dependencies of this expression.

Returns:

the evaluated value for this expression.

validate_dependencies(info: pydantic_core.core_schema.ValidationInfo) Self#

Check all the dependencies of the expression are existing parameters of the impact model and are of type float.

Parameters:

info – useful information used for validating data.

classmethod validate_expr(expr: str) str#

Check that the expression is a valid float expression.

Parameters:

expr – the expression to validate.

property dependencies: List[str]#
Returns:

the list of the parameters whose are the dependencies of this expression.

expr: str#
property is_complex: bool#
Returns:

True if the expression is a complex one, else False.

property raw_version#
Returns:

the raw version of the expression.

class apparun.expressions.ParamsValuesSet#

Bases: pydantic.BaseModel

Represents a set of expressions for a set of parameters of an impact model. Each expression in the set is associated to one and only one parameter.

classmethod build(expressions: Dict[str, float | int | str | dict], parameters: apparun.parameters.ImpactModelParams) ParamsValuesSet#

Builder function to create instance of this class.

Parameters:
  • expressions – a set of expressions, each expression is associated to one parameter.

  • parameters – parameters associated to the expressions.

Returns:

an instance of this class with the given expressions.

dependencies_cycle() List[str]#

Detect if there is a dependencies cycle between the expressions.

Returns:

the list of the parameters whose expressions are creating a dependencies cycle

or an empty list if there is no cycle.

evaluate() Dict[str, float | int | str]#

Evaluate the value of each expression, there must be no dependency cycle in the set. If there is a dependency cycle, a ValueError is raised.

Returns:

the value of each expression, the keys are the name of the parameter associated to the expression.

property dependencies_graph: networkx.DiGraph#

Build the dependencies graph of the expressions.

Returns:

an oriented graph representing the dependencies between the expressions.

expressions: Dict[str, ParamExpr]#
apparun.expressions.parse_expr(expr: Any) sympy.Expr#

Parses an arithmetic expression using sympy. Raises an error if the expression is invalid.

Parameters:

expr – an expression.

Returns:

a sympy expression object.

apparun.expressions.validate_expr(expr: str) bool#

Check if an expression is a valid arithmetic expression that can be used in an impact model. Allowed functions inside an expression are only functions of the modules math and numpy.

Parameters:

expr – an expression.

Returns:

True if the expression is a valid arithmetic expression, else False.