Exemplo n.º 1
0
 private PythonExec()
 {
     setup    = Python.CreateRuntimeSetup(null);
     runtime  = new ScriptRuntime(setup);
     engine   = Python.GetEngine(runtime);
     programs = new Dictionary <string, PythonProgram>();
 }
        private void InitIronPython()
        {
            if (m_engine == null)
            {
                //s_engine = Python.CreateEngine();
                //s_runtime = s_engine.Runtime;
                // s_scope = s_engine.CreateScope();

                m_runtime = Python.CreateRuntime();
                m_runtime.LoadAssembly(typeof(String).Assembly);
                //runtime.LoadAssembly(typeof(Uri).Assembly);

                // no module name System
                //m_runtime.ImportModule("System");

                m_scope = m_runtime.CreateScope();

                m_engine = Python.GetEngine(m_runtime);

                ICollection <string> paths = m_engine.GetSearchPaths();
                foreach (string s in s_pythonLibPath)
                {
                    paths.Add(s);
                }
                m_engine.SetSearchPaths(paths);
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            try
            {
                ScriptRuntimeSetup setup   = Python.CreateRuntimeSetup(null);
                ScriptRuntime      runtime = new ScriptRuntime(setup);
                ScriptEngine       engine  = Python.GetEngine(runtime);

                var paths = engine.GetSearchPaths();
                paths.Add(@"C:\Program Files\IronPython 2.7\lib");
                engine.SetSearchPaths(paths);
                ScriptSource  source = engine.CreateScriptSourceFromFile(@"D:\VsCodeDemo\pythonfile\EagleXml.py");
                ScriptScope   scope  = engine.CreateScope();
                List <String> argv   = new List <String>();
                //Do some stuff and fill argv
                argv.Add(".");
                argv.Add(@"D:\VsCodeDemo\pythonfile\MEHOW-0.xml");
                engine.GetSysModule().SetVariable("argv", argv);
                source.Execute(scope);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.Read();
        }
Exemplo n.º 4
0
        public PythonEngine()
        {
            var setup = CreateRuntimeSetup();

            setup.Options["InterpretedMode"] = true;
            var runtime = new ScriptRuntime(setup);

            _engine = Python.GetEngine(runtime);
        }
Exemplo n.º 5
0
    // https://mail.python.org/pipermail/ironpython-users/2012-December/016366.html
    // http://ironpython.net/blog/2012/07/07/whats-new-in-ironpython-273.html
    // https://blog.adamfurmanek.pl/2017/10/14/sqlxd-part-22/
    public static dynamic CreateEngine()
    {
        ScriptRuntimeSetup setup    = Python.CreateRuntimeSetup(GetRuntimeOptions());
        var          pyRuntime      = new ScriptRuntime(setup);
        ScriptEngine engineInstance = Python.GetEngine(pyRuntime);

        AddPythonLibrariesToSysMetaPath(engineInstance);

        return(engineInstance);
    }
Exemplo n.º 6
0
        public ScriptFilter(string scriptText)
        {
            Validation.CheckObject("scriptText", scriptText);

            //initialize python
            runtime = Python.CreateRuntime();
            engine  = Python.GetEngine(runtime);
            //load the script
            source = engine.CreateScriptSourceFromString(scriptText);
            script = source.Compile();
        }
Exemplo n.º 7
0
            // https://mail.python.org/pipermail/ironpython-users/2012-December/016366.html
            // http://ironpython.net/blog/2012/07/07/whats-new-in-ironpython-273.html
            // https://blog.adamfurmanek.pl/2017/10/14/sqlxd-part-22/
            public static dynamic CreateEngine()
            {
                ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(
                    new Dictionary <string, object>
                {
                    ["Debug"] = false
                });
                var          pyRuntime      = new ScriptRuntime(setup);
                ScriptEngine engineInstance = Python.GetEngine(pyRuntime);

                AddPythonLibrariesToSysMetaPath(engineInstance);
                return(engineInstance);
            }
Exemplo n.º 8
0
        private (string, string, string, string) GetParam(int serviceid, string path)
        {
            string Question = "", Answer = "", Boxes = "", Hints = "";
            int    RandInt = rand.Next(1000);

            ScriptRuntimeSetup setup   = Python.CreateRuntimeSetup(null);
            ScriptRuntime      runtime = new ScriptRuntime(setup);
            var engine = Python.GetEngine(runtime);
            var paths  = engine.GetSearchPaths();

            paths.Add(@"C:\Python27\Lib");
            engine.SetSearchPaths(paths);
            ScriptSource source = engine.CreateScriptSourceFromFile(path);
            var          scope  = engine.CreateScope();
            var          d      = new Dictionary <string, object>
            {
                { "serviceid", serviceid },
                { "question", Question },
                { "answer", Answer },
                { "ansName", Boxes },
                { "prompt", Hints }
            };


            scope.SetVariable("params", d);
            scope.SetVariable("seed", RandInt);
            object result = source.Execute(scope);

            Question = scope.GetVariable <string>("question");

            Answer = scope.GetVariable <string>("answer");
            try
            {
                Boxes = scope.GetVariable <string>("ansName");
            }
            catch
            {
                Boxes = "";
            }
            try
            {
                Hints = scope.GetVariable <string>("prompt");
            }
            catch
            {
                Hints = "";
            }

            return(Question, Answer, Boxes, Hints);
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            ScriptRuntimeSetup setup   = Python.CreateRuntimeSetup(null);
            ScriptRuntime      runtime = new ScriptRuntime(setup);
            ScriptEngine       engine  = Python.GetEngine(runtime);
            ScriptSource       source  = engine.CreateScriptSourceFromFile("HelloWorld.py");
            ScriptScope        scope   = engine.CreateScope();
            List <String>      argv    = new List <String>();

            //Do some stuff and fill argv
            argv.Add("foo");
            argv.Add("bar");
            engine.GetSysModule().SetVariable("argv", argv);
            source.Execute(scope);
        }
Exemplo n.º 10
0
 public void SaveStatistics()
 {
     //Run the python file
     #region
     ScriptRuntimeSetup setup   = Python.CreateRuntimeSetup(null);
     ScriptRuntime      runtime = new ScriptRuntime(setup);
     ScriptEngine       engine  = Python.GetEngine(runtime);
     ScriptSource       source  = engine.CreateScriptSourceFromFile("savestats.py");
     ScriptScope        scope   = engine.CreateScope();
     List <String>      argv    = new List <String>();
     #endregion
     argv.Add(HareWins.ToString());
     argv.Add(TurtleWins.ToString());
     argv.Add(Draws.ToString());
     engine.GetSysModule().SetVariable("argv", argv);
     source.Execute(scope);
 }
Exemplo n.º 11
0
        public async Task <IEnumerable <Course> > GetList(int?userId, string name, ContextSession session, bool includeDeleted = false)
        {
            var entity  = GetEntities(session, includeDeleted).AsQueryable();
            var courses = await entity.Where(obj => obj.Id > 0).ToListAsync();

            SaveToCsv <Course>(courses, Environment.CurrentDirectory + "/csv/course.csv");
            var ratings = _dbContext.Set <CourseRating>().AsQueryable();

            if (!includeDeleted)
            {
                ratings = ratings.Where(obj => !obj.IsDeleted);
            }

            var i = await ratings.Where(obj => obj.Id > 0).ToListAsync();

            SaveToCsv <CourseRating>(i, Environment.CurrentDirectory + "/csv/ratings_Final.csv");

            ScriptRuntimeSetup   setup       = Python.CreateRuntimeSetup(null);
            ScriptRuntime        runtime     = new ScriptRuntime(setup);
            ScriptEngine         engine      = Python.GetEngine(runtime);
            ICollection <string> searchPaths = engine.GetSearchPaths();

            searchPaths.Add("C:\\Users\\Admin\\PycharmProjects\\ArClassifier\\venv\\Lib");
            searchPaths.Add("C:\\Users\\Admin\\PycharmProjects\\ArClassifier\\venv\\Lib\\site-packages");
            engine.SetSearchPaths(searchPaths);
            ScriptSource  source = engine.CreateScriptSourceFromFile(Environment.CurrentDirectory + "/CB_RS.py");
            ScriptScope   scope  = engine.CreateScope();
            List <String> argv   = new List <String>();

            argv.Add(session.UserId.ToString());
            argv.Add(Environment.CurrentDirectory + "/csv/course.csv");
            argv.Add(Environment.CurrentDirectory + "/csv/ratings_Final.csv");
            engine.GetSysModule().SetVariable("argv", argv);
            try
            {
                var qq  = source.Execute(scope);
                var qq2 = qq;
            }
            catch (Exception ex)
            {
                var error = ex;
            }

            return(await entity.Where(obj => obj.Id > 0).ToListAsync());
        }
Exemplo n.º 12
0
        public static void RunPythonFile(string filename)
        {
            try
            {
                // This environment variable can be used from scripts to iidentify
                // that is being run from 3ds Max
                System.Environment.SetEnvironmentVariable("ADSKPRODUCT", "max");

                // This is the Key to making sure that Visual Studio can debug
                // the Python script. This way you can attach to 3dsMax.exe
                // and catch exceptions that occur right at the correct location
                // in the Python script.
                var options = new Dictionary <string, object>();
                options["Debug"] = true;

                // Create an instance of the Python run-time
                var runtime = Python.CreateRuntime(options);

                // Retrive the Python scripting engine
                var engine = Python.GetEngine(runtime);

                // Get the directory of the file
                var dir = Path.GetDirectoryName(filename);

                // Make sure that the local paths are available.
                var paths = engine.GetSearchPaths();
                paths.Add(dir);
                engine.SetSearchPaths(paths);

                // Run the Python file
                runtime.ExecuteFile(filename);

                // NOTE: here is a cute IronPython trick with C# 4.0
                // dynamic script = py.UseFile("script.py");
                // dynamic result = script.SomFunc();
            }
            catch (Exception e)
            {
                MessageBox.Show("Error occurred: " + e.Message);
            }
        }
Exemplo n.º 13
0
        public void IronPython_StdLibUsage()
        {
            // Arrange
            var setup   = Python.CreateRuntimeSetup(null);
            var runtime = new ScriptRuntime(setup);
            var engine  = Python.GetEngine(runtime);
            var paths   = engine.GetSearchPaths();

            paths.Add(@"Lib\");
            engine.SetSearchPaths(paths);

            // Act
            var source = engine.CreateScriptSourceFromFile(@"IronPythonTests\use_stdlib.py");
            var scope  = engine.CreateScope();

            source.Execute(scope);
            var result = scope.GetVariable("result");

            // Assert
            Assert.Equal("{\"a\": 0, \"b\": 0, \"c\": 0}", result);
        }
Exemplo n.º 14
0
        public void IronPython_3rdPartyLibUsage()
        {
            // Arrange
            var setup   = Python.CreateRuntimeSetup(null);
            var runtime = new ScriptRuntime(setup);
            var engine  = Python.GetEngine(runtime);
            var paths   = engine.GetSearchPaths();

            paths.Add(@"Lib\");
            paths.Add(@"IronPythonTests\Modules\");
            engine.SetSearchPaths(paths);

            // Act
            var source = engine.CreateScriptSourceFromFile(@"IronPythonTests\use_3rd_party_lib.py");
            var scope  = engine.CreateScope();

            source.Execute(scope);
            var result = scope.GetVariable("result");

            Assert.Equal(result, 200);
        }
Exemplo n.º 15
0
        public ScriptEngine CreateScriptEngine()
        {
            ScriptRuntime runt = Python.CreateRuntime();

            return(Python.GetEngine(runt));
        }
Exemplo n.º 16
0
        private void PythonConsole(bool cmdLine)
        {
            Dictionary <string, object> options = new Dictionary <string, object>();

            options["Debug"] = true;

            var ipy    = Python.CreateRuntime(options);
            var engine = Python.GetEngine(ipy);

            try
            {
                string path = "";

                System.Version version = Application.Version;

                string ver = version.ToString().Split(new char[] { '.' })[0];

                string release = "2020";

                switch (ver)
                {
                case "20":
                    release = "2016";
                    break;

                case "21":
                    release = "2017";
                    break;

                case "22":
                    release = "2018";
                    break;

                case "23":
                    release = "2019";
                    break;

                case "24":
                    release = "2020";
                    break;
                }

                if (!cmdLine)
                {
                    WF.OpenFileDialog ofd = new WF.OpenFileDialog();
                    ofd.Filter = "Python Script (*.py) | *.py";

                    if (ofd.ShowDialog() == WF.DialogResult.OK)
                    {
                        path = ofd.FileName;
                    }
                }
                else
                {
                    Document doc = Application.DocumentManager.MdiActiveDocument;
                    Editor   ed  = doc.Editor;

                    short fd = (short)Application.GetSystemVariable("FILEDIA");

                    // Ask the user to select a .py file

                    PromptOpenFileOptions pfo = new PromptOpenFileOptions("Select Python script to load");
                    pfo.Filter            = "Python script (*.py)|*.py";
                    pfo.PreferCommandLine = (cmdLine || fd == 0);
                    PromptFileNameResult pr = ed.GetFileNameForOpen(pfo);
                    path = pr.StringResult;
                }

                if (path != "")
                {
                    ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"C:\Program Files\Autodesk\AutoCAD {0}\acmgd.dll", release)));
                    ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"C:\Program Files\Autodesk\AutoCAD {0}\acdbmgd.dll", release)));
                    ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"C:\Program Files\Autodesk\AutoCAD {0}\accoremgd.dll", release)));
                    ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"C:\Program Files\Autodesk\AutoCAD {0}\ACA\AecBaseMgd.dll", release)));
                    ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"C:\Program Files\Autodesk\AutoCAD {0}\ACA\AecPropDataMgd.dll", release)));
                    ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"C:\Program Files\Autodesk\AutoCAD {0}\C3D\AeccDbMgd.dll", release)));
                    ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"C:\Program Files\Autodesk\AutoCAD {0}\C3D\AeccPressurePipesMgd.dll", release)));
                    ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"C:\Program Files\Autodesk\AutoCAD {0}\acdbmgdbrep.dll", release)));


                    ScriptSource source       = engine.CreateScriptSourceFromFile(path);
                    CompiledCode compiledCode = source.Compile();
                    ScriptScope  scope        = engine.CreateScope();
                    compiledCode.Execute(scope);
                }
            }
            catch (System.Exception ex)
            {
                string message = engine.GetService <ExceptionOperations>().FormatException(ex);
                WF.MessageBox.Show(message);
            }

            return;
        }
Exemplo n.º 17
0
 public PythonRuleConstraintEvaluator()
 {
     this._engine = Python.GetEngine(s_runtime);
     this._scope  = this._engine.CreateScope();
 }
Exemplo n.º 18
0
 internal static dynamic Import(this ScriptRuntime runtime, string moduleName)
 {
     return(Python.ImportModule(Python.GetEngine(runtime), moduleName));
 }