gws.plugin.model_value.expression

Expression value.

Computes the value by evaluating a Python expression. The expression is compiled at configuration time; a syntax error is a configuration error.

The following variables are available in the expression:

  • app: the Application object,

  • user: the current user,

  • project: the current project,

  • feature: the feature the value is computed for,

  • mc: the gws.ModelContext object,

  • date: the gws.lib.datetimex module.

Additional modules can be made available with the imports option, a list of module names, e.g. ["math", "os.path"]. Each module is available under its top-level package name, so os.path is used as os.path.join(...). If a module cannot be imported or the evaluation fails, the error is logged and the value is None.

Example:

fields+ {
    name "area_ha"
    type "float"
    values+ {
        type "expression"
        expression "round(feature.get('area_m2', 0) / 10000, 2)"
    }
}

Source code: gws.plugin.model_value.expression

Package Contents

class gws.plugin.model_value.expression.Config

Bases: gws.base.model.value.Config

Value computed by a Python expression.

expression: str

Python expression to evaluate.

imports: list[str] | None

Additional Python modules available to the expression.

class gws.plugin.model_value.expression.Object

Bases: gws.base.model.value.Object

Expression value object.

expression: str

Python expression to evaluate.

imports: list[str]

Names of modules imported for the expression.

compute(field, feature, mc)
configure()