Example #1
0
        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;
            }));
        }
Example #2
0
        public override IObservable <TSource> Process <TSource>(IObservable <TSource> source)
        {
            return(Observable.Defer(() =>
            {
                var scriptTask = new Task(() => { });
                scriptTask.Start();

                var engine = CreateEngine();
                var scope = engine.CreateScope();
                engine.Execute(Script, scope);

                object sink;
                PythonProcessor <TSource, object> processor;
                if (PythonHelper.TryGetClass(scope, "Sink", out sink))
                {
                    processor = new PythonProcessor <TSource, object>(engine.Operations, sink);
                }
                else
                {
                    processor = new PythonProcessor <TSource, object>(scope);
                }

                if (processor.Load != null)
                {
                    processor.Load();
                }

                return source.Do(input =>
                {
                    scriptTask = scriptTask.ContinueWith(task =>
                    {
                        processor.Process(input);
                    });
                }).Finally(() =>
                {
                    var unloadAction = processor.Unload;
                    if (unloadAction != null)
                    {
                        scriptTask = scriptTask.ContinueWith(task => unloadAction());
                    }

                    engine.Runtime.IO.OutputWriter.Close();
                    scriptTask.Wait();
                });
            }));
        }
Example #3
0
 static IObservable <TResult> Process <TSource, TResult>(
     IObservable <TSource> source,
     ObjectOperations op,
     object processorClass)
 {
     return(Observable.Defer(() =>
     {
         var processor = new PythonProcessor <TSource, PythonGenerator>(op, processorClass);
         var result = source.SelectMany(input => processor.Process(input).Cast <TResult>());
         if (processor.Load != null)
         {
             processor.Load();
         }
         if (processor.Unload != null)
         {
             return result.Finally(processor.Unload);
         }
         else
         {
             return result;
         }
     }));
 }
Example #4
0
        static IObservable <TResult> Process <TSource, TResult>(
            IObservable <TSource> source,
            ScriptScope scope)
        {
            var processor = new PythonProcessor <TSource, PythonGenerator>(scope);
            var result    = source.SelectMany(input => processor.Process(input).Cast <TResult>());

            if (processor.Unload != null)
            {
                result = result.Finally(processor.Unload);
            }

            if (processor.Load != null)
            {
                var observable = result;
                result = Observable.Defer(() =>
                {
                    processor.Load();
                    return(observable);
                });
            }

            return(result);
        }