コード例 #1
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();
        }
コード例 #2
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));
        }