psamm.expression.affine – Affine expressions

Representations of affine expressions and variables.

These classes can be used to represent affine expressions and do manipulation and evaluation with substitutions of particular variables.

class psamm.expression.affine.Expression(*args)

Represents an affine expression (e.g. 2x + 3y - z + 5)

simplify()

Return simplified expression.

If the expression is of the form ‘x’, the variable will be returned, and if the expression contains no variables, the offset will be returned as a number.

substitute(mapping)

Return expression with variables substituted

>>> Expression('x + 2y').substitute(
...     lambda v: {'y': -3}.get(v.symbol, v))
Expression('x - 6')
>>> Expression('x + 2y').substitute(
...     lambda v: {'y': Variable('z')}.get(v.symbol, v))
Expression('x + 2z')
variables()

Return iterator of variables in expression

class psamm.expression.affine.Variable(symbol)

Represents a variable in an expression

Equality of variables is based on the symbol.

simplify()

Return simplified expression

The simplified form of a variable is always the variable itself.

>>> Variable('x').simplify()
Variable('x')
substitute(mapping)

Return expression with variables substituted

>>> Variable('x').substitute(lambda v: {'x': 567}.get(v.symbol, v))
567
>>> Variable('x').substitute(lambda v: {'y': 42}.get(v.symbol, v))
Variable('x')
>>> Variable('x').substitute(
...     lambda v: {'x': 123, 'y': 56}.get(v.symbol, v))
123
symbol

Symbol of variable

>>> Variable('x').symbol
'x'