public T Evaluate(object host, IDictionary <string, object> variables = null) { var vars = new Dictionary <string, object>(variables ?? new Dictionary <string, object>()); vars.Add("self", Context); vars.Add("host", host); if (host is IFormulaHost) { foreach (var kvp in ((IFormulaHost)host).Variables) { vars.Add(kvp.Key, kvp.Value); } } if (host is IModObject mo) { if (mo.TemplateParameters != null) { foreach (var kvp in mo.TemplateParameters) { vars.Add(kvp.Key, kvp.Value); } } } string fulltext; if (ExternalScripts == null) { fulltext = Text; } else { fulltext = string.Join("\n", ExternalScripts.Select(q => q.Text)) + "\n" + Text; } return(PythonScriptEngine.EvaluateExpression <T>(fulltext, vars)); }
private static Field CreateStaticFormulaField(Field f, IDictionary <string, int> args) { var variables = args.ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value); var result = PythonScriptEngine.EvaluateExpression <IConvertible>(f.Value.TrimStart('='), variables).ToStringInvariant(); var field = new Field(); field.Name = f.Name; field.Value = result; return(field); }