Example #1
0
 public void TestEval()
 {
     using (Py.GIL())
     {
         ps.Set("a", 1);
         var result = ps.Eval <int>("a + 2");
         Assert.AreEqual(3, result);
     }
 }
Example #2
0
        static async Task Main(string[] args)
        {
            await Installer.SetupPython();

            using (Py.GIL())
            {
                string a = "def funcname(a):"
                           + Environment.NewLine
                           + "    i = 12"
                           + Environment.NewLine
                           + "    a = i+25"
                           + Environment.NewLine
                           + "    print(i)"
                           + Environment.NewLine
                           + "    print(a)"
                           + Environment.NewLine
                           + "    return a";
                PyScope  pyScope  = Py.CreateScope();
                PyObject pyObject = PythonEngine.ModuleFromString("a", a);
                pyScope.ImportAll(pyObject);
                PyObject eval = pyScope.Eval("funcname(15)");
                Console.Out.WriteLine("eval = {0}", eval);
                eval.GetPythonType().WriteLine();
                var @as = eval.As <int>();
                Console.Out.WriteLine("@as = {0}", @as);
            }

            Console.ReadKey();
        }
Example #3
0
        public async Task <PyObject> PythonResponseToBytearray(HttpResponseMessage resp)
        {
            //Debug.WriteLine("PythonResponseToBytearray");
            byte[] content = await resp.Content.ReadAsByteArrayAsync();

            using (Py.GIL())
            {
                PyObject bytearray = PyScope.Eval("bytearray(" + content.Length + ")");
                PythonCompat.WriteToBuffer(bytearray, content);
                return(bytearray);
            }
        }
        public object QueryAsync(string code)
        {
            using IServiceScope scope = _serviceProvider.CreateScope();
            UsageRecordsRepository    repository   = scope.ServiceProvider.GetRequiredService <UsageRecordsRepository>();
            IEnumerable <UsageRecord> usageRecords = repository.UsageRecords.AsNoTracking();

            using Py.GILState gil = Py.GIL();
            using PyScope pyScope = Py.CreateScope();
            pyScope.Set(nameof(usageRecords), usageRecords.ToPython());
            PyObject pyObject = pyScope.Eval(code);

            if (pyObject.IsIterable())
            {
                return(pyObject.Select(item => item.AsManagedObject(typeof(object))));
            }
            else
            {
                object result = pyObject.AsManagedObject(typeof(object));
                return(result);
            }
        }
Example #5
0
        public object Run(
            [InputPin(Name = "caption", Description = "The caption of this module", PropertyMode = PropertyMode.Always, Editor = WellKnownEditors.SingleLineText)] string caption,
            [InputPin(Name = "expression", Description = "The Python expression", PropertyMode = PropertyMode.Always, Editor = WellKnownEditors.SingleLineText)] string expression,
            [InputPin(Name = "useMainThread", Description = "Whether to run the code on the Python main thread", PropertyMode = PropertyMode.Always, Editor = WellKnownEditors.CheckBox, DefaultValue = "false")] bool useMainThread,
            [InputPin(Name = "input", Description = "These are the inputs for the expression", PropertyMode = PropertyMode.Never)] params object[] inputs
            )
        {
            object evalBlock()
            {
                using (PyScope ps = Py.CreateScope())
                {
                    int i = 0;
                    foreach (var inputPin in this.dynamicInputPin.Pins)
                    {
                        if (inputPin.Connections.Any())
                        {
                            ps.Set(inputPin.Id, PyConvert.ToPyObject(inputs[i]));
                        }
                        i++;
                    }

                    ps.Set(dynamicInputPin.Alias + "s", inputs);
                    PyObject evalResult = ps.Eval(expression);
                    return(PyConvert.ToClrObject(evalResult, typeof(object)));
                }
            }

            if (useMainThread)
            {
                return(mainThread.EvalSync(evalBlock));
            }
            else
            {
                using (Py.GIL())
                {
                    return(evalBlock());
                }
            }
        }
Example #6
0
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            PyScope scope = PyScriptManager.ScriptScopes["Temp"];

            scope.ImportAll("math");

            scope.Set("value", value);

            PyObject result = scope.Eval(Script);
            string   errorMsg;

            try
            {
                errorMsg = result.As <string>();                //Expect true(bool) for valid input, an error message string for invalid input
            }
            catch (InvalidCastException)
            {
                return(new ValidationResult(true, null));
            }


            return(new ValidationResult(false, errorMsg));
        }