friendlysam.opt.Relation.evaluate¶
-
Relation.
evaluate
(replace=None, evaluators=None)¶ Evaluate the expression recursively.
Evaluating an expression:
1. Get an evaluating function. If the class of the present expression is in the
evaluators
dict, use that. Otherwise, take thecreate()
classmethod of the present expression class.2. Evaluate all the arguments. For each argument
arg
, first try to replace it by looking forreplace[arg]
. If it’s not there, try to evaluate it by callingarg.evaluate()
with the same arguments supplied to this call. Ifarg.evaluate()
is not present, leave the argument unchanged.- Run the evaluating function
func(*evaluated_args)
and return the result.
Parameters: - replace (dict, optional) – Replacements for arguments. Arguments matching keys will be replaced by specified values.
- evaluators (dict, optional) – Evaluating functions to use instead of the default
(which is the
create()
classmethod of the argument’s class). An argument whose__class__
equals a key will be evaluated with the specified function.
Examples
>>> x = VariableCollection('x') >>> expr = x(1) + x(2) >>> print(expr.evaluate()) x(1) + x(2) >>> expr.evaluate(replace={x(1): 10, x(2): 20}) <friendlysam.opt.Add at 0x...> >>> print(_) 10 + 20 >>> expr.evaluate(replace={x(1): 10, x(2): 20}, evaluators=fs.CONCRETE_EVALUATORS) 30
- Run the evaluating function