public override IObservable <TSource> Process <TSource>(IObservable <TSource> source) { return(Observable.Defer(() => { var engine = PythonEngine.Create(); var scope = engine.CreateScope(); engine.Execute(Script, scope); object condition; PythonProcessor <TSource, bool> processor; if (PythonHelper.TryGetClass(scope, "Condition", out condition)) { processor = new PythonProcessor <TSource, bool>(engine.Operations, condition); } else { processor = new PythonProcessor <TSource, bool>(scope); } if (processor.Load != null) { processor.Load(); } var result = source.Where(processor.Process); if (processor.Unload != null) { result = result.Finally(processor.Unload); } return result; })); }
public override Expression Build(IEnumerable <Expression> arguments) { var engine = PythonEngine.Create(); var scope = engine.CreateScope(); var script = PythonHelper.ReturnsDecorator + Script; var scriptSource = engine.CreateScriptSourceFromString(script); scriptSource.Execute(scope); var scopeExpression = Expression.Constant(scope); var outputType = PythonHelper.GetOutputType(scope, PythonHelper.GenerateFunction); var generatorType = Expression.GetFuncType(typeof(PythonGenerator)); var generateExpression = Expression.Call( scopeExpression, "GetVariable", new[] { generatorType }, Expression.Constant(PythonHelper.GenerateFunction)); var combinatorExpression = Expression.Constant(this); return(Expression.Call(combinatorExpression, "Generate", new[] { outputType }, generateExpression)); }
public override Expression Build(IEnumerable <Expression> arguments) { var engine = PythonEngine.Create(); var scope = engine.CreateScope(); var script = PythonHelper.ReturnsDecorator + Script; var scriptSource = engine.CreateScriptSourceFromString(script); scriptSource.Execute(scope); object selectMany; var source = arguments.Single(); var observableType = source.Type.GetGenericArguments()[0]; if (PythonHelper.TryGetClass(scope, "SelectMany", out selectMany)) { var classExpression = Expression.Constant(selectMany); var opExpression = Expression.Constant(engine.Operations); var outputType = PythonHelper.GetOutputType(engine.Operations, selectMany, PythonHelper.ProcessFunction); return(Expression.Call( typeof(PythonTransform), "Process", new[] { observableType, outputType }, source, opExpression, classExpression)); } else { var outputType = PythonHelper.GetOutputType(scope, PythonHelper.ProcessFunction); var scopeExpression = Expression.Constant(scope); return(Expression.Call( typeof(PythonSelectMany), "Process", new[] { observableType, outputType }, source, scopeExpression)); } }
protected virtual ScriptEngine CreateEngine() { return(PythonEngine.Create()); }