Example #1
0
        /// <summary>
        /// Runs a Python script in the Unity process.
        /// </summary>
        /// <param name="pythonFileToExecute">The script to execute.</param>
        /// <param name="scopeName">Value to write to Python special variable `__name__`</param>
        public static void RunFile(string pythonFileToExecute, string scopeName = null)
        {
            EnsureInitialized();
            if (null == pythonFileToExecute)
            {
                throw new System.ArgumentNullException("pythonFileToExecute", "Invalid (null) file path");
            }

            // Ensure we are getting the full path.
            pythonFileToExecute = Path.GetFullPath(pythonFileToExecute);

            // Forward slashes please
            pythonFileToExecute = pythonFileToExecute.Replace("\\", "/");
            if (!File.Exists(pythonFileToExecute))
            {
                throw new System.IO.FileNotFoundException("No Python file found at " + pythonFileToExecute, pythonFileToExecute);
            }

            using (Py.GIL())
            {
                if (string.IsNullOrEmpty(scopeName))
                {
                    PythonEngine.Exec(string.Format("exec(open('{0}').read())", pythonFileToExecute));
                }
                else
                {
                    using (PyScope scope = Py.CreateScope())
                    {
                        scope.Set("__name__", scopeName);
                        scope.Set("__file__", pythonFileToExecute);
                        scope.Exec(string.Format("exec(open('{0}').read())", pythonFileToExecute));
                    }
                }
            }
        }
Example #2
0
        public void TestImportScopeFunction()
        {
            using (Py.GIL())
            {
                ps.Set("bb", 100);
                ps.Set("cc", 10);
                ps.Exec(
                    "def func1():\n" +
                    "    return cc + bb\n");

                using (PyScope scope = ps.NewScope())
                {
                    //'func1' is imported from the origion scope
                    scope.Exec(
                        "def func2():\n" +
                        "    return func1() - cc - bb\n");
                    dynamic func2 = scope.Get("func2");

                    var result1 = func2().As <int>();
                    Assert.AreEqual(0, result1);

                    scope.Set("cc", 20);//it has no effect on the globals of 'func1'
                    var result2 = func2().As <int>();
                    Assert.AreEqual(-10, result2);
                    scope.Set("cc", 10); //rollback

                    ps.Set("cc", 20);
                    var result3 = func2().As <int>();
                    Assert.AreEqual(10, result3);
                    ps.Set("cc", 10); //rollback
                }
            }
        }
Example #3
0
        public void Run()
        {
            using (PyScope scope = Py.CreateScope())
            {
                dynamic np = Py.Import("numpy");
                Console.WriteLine(np.cos(np.pi * 2));

                dynamic sin = np.sin;
                Console.WriteLine(sin(5));

                Double c = np.cos(5) + sin(5);
                Console.WriteLine(c);

                dynamic a = np.array(new List <float> {
                    1, 2, 3
                });
                Console.WriteLine(a.dtype);

                dynamic b = np.array(new List <float> {
                    6, 5, 4
                }, dtype: np.int32);
                Console.WriteLine(b.dtype);

                Console.WriteLine(a * b);
            }
        }
Example #4
0
        /// <summary>
        /// Runs Python code in the Unity process.
        /// </summary>
        /// <param name="pythonCodeToExecute">The code to execute.</param>
        /// <param name="scopeName">Value to write to Python special variable `__name__`</param>
        public static void RunString(string pythonCodeToExecute, string scopeName = null)
        {
            if (string.IsNullOrEmpty(pythonCodeToExecute))
            {
                return;
            }

            EnsureInitialized();
            using (Py.GIL())
            {
                // Clean up the string.
                dynamic inspect = PythonEngine.ImportModule("inspect");
                string  code    = inspect.cleandoc(pythonCodeToExecute);

                if (string.IsNullOrEmpty(scopeName))
                {
                    PythonEngine.Exec(code);
                }
                else
                {
                    using (PyScope scope = Py.CreateScope())
                    {
                        scope.Set("__name__", scopeName);
                        scope.Exec(code);
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Load a module into a scope.
        /// </summary>
        /// <param name="name">Name to be assigned to the module</param>
        /// <param name="fileName">Name of the code file.</param>
        /// <param name="globals">Globals to be set before execution of the script.</param>
        /// <returns>PyScope with code loaded.</returns>
        private PyScope LoadModule(string name, string fileName, Dictionary <string, object> globals)
        {
            PyScope scope = null;

            using (Py.GIL())
            {
                // Create a new scope
                scope = Py.CreateScope(name);

                // Assign any globals.
                if (globals != null)
                {
                    foreach (string gname in globals.Keys)
                    {
                        scope.Set(gname, globals[gname]);
                    }
                }

                // Load python code, compile, then execute it in the scope.
                string   scriptText = File.ReadAllText(fileName);
                PyObject code       = PythonEngine.Compile(scriptText, fileName);
                dynamic  r          = scope.Execute(code);
            }
            return(scope);
        }
Example #6
0
        static void Main(string[] args)
        {
            string preamble =
                "import clr\n" +
                "clr.AddReference('MyModule')\n" +
                "from MyModule import MyClass\n" +
                "import sys\n" +
                "print(sys.version)";

            string callWithFloatArgs = "result = MyClass.Add(1.0, 2.0)";
            string callWithIntArgs   = "result = MyClass.Add(1, 2)";
            string callWithMixedArgs = "result = MyClass.Add(1.0, 2)";

            PythonEngine.Initialize();
            using (Py.GIL())
            {
                using (PyScope scope = Py.CreateScope())
                {
                    scope.Exec(preamble);

                    scope.Exec(callWithFloatArgs);
                    System.Console.WriteLine(scope.Get("result")); // prints "3.0"

                    scope.Exec(callWithIntArgs);
                    System.Console.WriteLine(scope.Get("result")); // prints "3"

                    scope.Exec(callWithMixedArgs);                 // Python.Runtime.PythonException: 'TypeError : No method matches given arguments for Add'
                    System.Console.WriteLine(scope.Get("result"));
                }
            }
        }
Example #7
0
        public void RunFunction(string sFuncName, params string[] args)
        {
            using (Py.GIL())
                using (PyScope scope = Py.CreateScope())
                {
                    Scope = scope;

                    foreach (var kvp in Locals)
                    {
                        var      pyName = kvp.Key;
                        PyObject pyObj  = kvp.Value.ToPython();

                        Scope.Set(pyName, pyObj);
                    }

                    Scope.Exec(Script);

                    var functionBuilder = new StringBuilder();
                    functionBuilder.Append(sFuncName);

                    functionBuilder.Append("(");
                    foreach (var arg in args)
                    {
                        functionBuilder.Append(arg);
                    }
                    functionBuilder.Append(")");

                    Scope.Exec(functionBuilder.ToString());
                }
        }
Example #8
0
 public void Run(string cmd)
 {
     using (PyScope scope = Py.CreateScope())
     {
         scope.Exec(cmd);
     }
 }
Example #9
0
        void Analyze(string path)
        {
            var data = File.ReadAllText(path);
            var name = Path.GetFileNameWithoutExtension(path);

            using (PyScope scope = Py.CreateScope())
            {
                scope.Import("sys");
                scope.Import("os");
                var dir = Path.GetDirectoryName(path);
                var com = $"sys.path.append({dir})";
                scope.Exec("p = os.getcwd()");
                var p = scope.Get("p");
                //scope.Exec(com);
                scope.Exec(data);
                var v       = scope.Get(name);
                var mapping = v.GetAttr("mapping");
                var items   = mapping.GetAttr("keys");

                foreach (var item in mapping)
                {
                    var command = item.ToPython();
                    var target  = mapping.GetItem(command);
                    commands[command.ToString()] = target;
                }
            }
        }
Example #10
0
 public void SetUp()
 {
     using (Py.GIL())
     {
         ps = Py.CreateScope("test");
     }
 }
Example #11
0
        /// <summary>
        /// Processes additional bindings that are not actual inputs.
        /// Currently, only the node name is received in this way.
        /// </summary>
        /// <param name="scope">Python scope where execution will occur</param>
        /// <param name="bindingNames">List of binding names received for evaluation</param>
        /// <param name="bindingValues">List of binding values received for evaluation</param>
        private static void ProcessAdditionalBindings(PyScope scope, IList bindingNames, IList bindingValues)
        {
            const string NodeNameInput = "Name";
            string       nodeName;

            if (bindingNames.Count == 0 || !bindingNames[0].Equals(NodeNameInput))
            {
                // Defensive code to fallback in case the additional binding is not there, like
                // when the evaluator is called directly in unit tests.
                nodeName = "USER";
            }
            else
            {
                bindingNames.RemoveAt(0);
                nodeName = (string)bindingValues[0];
                bindingValues.RemoveAt(0);
            }

            // Session is null when running unit tests.
            if (ExecutionEvents.ActiveSession != null)
            {
                dynamic         logger      = ExecutionEvents.ActiveSession.GetParameterValue(ParameterKeys.Logger);
                Action <string> logFunction = msg => logger.Log($"{nodeName}: {msg}", LogLevel.ConsoleOnly);
                scope.Set(DynamoPrintFuncName, logFunction.ToPython());
                scope.Exec(RedirectPrint());
            }
        }
Example #12
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 #13
0
        /// <summary>
        /// Migrates Python 2 code to Python 3 using Pythons 2to3 library.
        /// </summary>
        /// <param name="code">Python 2 code that needs to be migrated</param>
        /// <returns></returns>
        internal static string MigrateCode(string code)
        {
            Installer.SetupPython().Wait();

            if (!PythonEngine.IsInitialized)
            {
                PythonEngine.Initialize();
                PythonEngine.BeginAllowThreads();
            }

            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                using (Py.GIL())
                {
                    using (PyScope scope = Py.CreateScope())
                    {
                        scope.Set(INPUT_NAME, code.ToPython());
                        scope.Exec(GetPythonMigrationScript());

                        var result = scope.Contains(RETURN_NAME) ? scope.Get(RETURN_NAME) : null;
                        return(result.ToString());
                    }
                }
            }

            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
        }
Example #14
0
 public YTDLPyBridge(YouTubeDL youtubeDL)
 {
     ytdl = youtubeDL;
     using (Py.GIL())
     {
         PyScope = Py.CreateScope();
     }
 }
 private static void Read(int reads, ref int a, PyScope scope)
 {
     for (int i = 0; i < reads; i++)
     {
         // a = scope.Eval<int>("a");
         a = scope.Get <int>("a");
     }
 }
Example #16
0
 public void Dispose()
 {
     using (Py.GIL())
     {
         ps.Dispose();
         ps = null;
     }
 }
 private static void Write(int writes, PyScope scope)
 {
     for (int i = 0; i < writes; i++)
     {
         // scope.Exec($"a = {i}");
         scope.Set("a", i);
     }
 }
Example #18
0
        /// <summary>
        ///     Executes a Python script with custom variable names. Script may be a string
        ///     read from a file, for example. Pass a list of names (matching the variable
        ///     names in the script) to bindingNames and pass a corresponding list of values
        ///     to bindingValues.
        /// </summary>
        /// <param name="code">Python script as a string.</param>
        /// <param name="bindingNames">Names of values referenced in Python script.</param>
        /// <param name="bindingValues">Values referenced in Python script.</param>
        public static object EvaluatePythonScript(
            string code,
            IList bindingNames,
            [ArbitraryDimensionArrayImport] IList bindingValues)
        {
            if (code != prev_code)
            {
                Python.Included.Installer.SetupPython().Wait();

                if (!PythonEngine.IsInitialized)
                {
                    PythonEngine.Initialize();
                    PythonEngine.BeginAllowThreads();
                }

                IntPtr gs = PythonEngine.AcquireLock();
                try
                {
                    using (Py.GIL())
                    {
                        using (PyScope scope = Py.CreateScope())
                        {
                            int amt = Math.Min(bindingNames.Count, bindingValues.Count);

                            for (int i = 0; i < amt; i++)
                            {
                                scope.Set((string)bindingNames[i], InputMarshaler.Marshal(bindingValues[i]).ToPython());
                            }

                            try
                            {
                                OnEvaluationBegin(scope, code, bindingValues);
                                scope.Exec(code);
                                OnEvaluationEnd(false, scope, code, bindingValues);

                                var result = scope.Contains("OUT") ? scope.Get("OUT") : null;

                                return(OutputMarshaler.Marshal(result));
                            }
                            catch (Exception e)
                            {
                                OnEvaluationEnd(false, scope, code, bindingValues);
                                throw e;
                            }
                        }
                    }
                }
                catch (PythonException pe)
                {
                    throw pe;
                }
                finally
                {
                    PythonEngine.ReleaseLock(gs);
                }
            }
            return(null);
        }
Example #19
0
 public void Run(OpenCvSharp.Mat src, string cmd)
 {
     using (PyScope scope = Py.CreateScope())
     {
         PyObject pySrc = src.ToPython();
         scope.Set("src", pySrc);
         scope.Exec(cmd);
     }
 }
Example #20
0
 public static void ExecResource(this PyScope scope, string name)
 {
     using (Py.GIL()) {
         var asm = Assembly.GetCallingAssembly();
         using var stream       = asm.GetManifestResourceStream(asm.GetManifestResourceNames().Single(fn => fn.EndsWith(name, StringComparison.OrdinalIgnoreCase)));
         using var streamReader = new StreamReader(stream, Encoding.UTF8);
         scope.Exec(streamReader.ReadToEnd());
     }
 }
Example #21
0
 public CPythonRuntime()
 {
     PythonEngine.PythonPath += ";" + Path.GetDirectoryName(typeof(CPythonRuntime).Assembly.Location);
     Log.WriteLine("PythonPath = {0}", PythonEngine.PythonPath);
     m_GIL   = Py.GIL();
     m_scope = Py.CreateScope();
     //TODO make sure the GraphEngine wheel is installed
     m_scope.Exec(@"import GraphEngine");
 }
Example #22
0
 /// <summary>
 /// Called immediately before evaluation starts
 /// </summary>
 /// <param name="scope">The scope in which the code is executed</param>
 /// <param name="code">The code to be evaluated</param>
 /// <param name="bindingValues">The binding values - these are already added to the scope when called</param>
 private static void OnEvaluationBegin(PyScope scope,
                                       string code,
                                       IList bindingValues)
 {
     if (EvaluationBegin != null)
     {
         EvaluationBegin(EvaluationState.Begin, scope, code, bindingValues);
     }
 }
Example #23
0
        public void PostRequestChord(string input)
        {
            try
            {
                using (Py.GIL())
                {
                    PyList pyTest = new PyList();


                    Console.WriteLine(input);

                    if (input.Contains("c") == false)
                    {
                        listaNotas.Add(input);
                    }
                    if (input.Contains("c"))
                    {
                        listaNotas.Remove(input.Remove(input.Length - 1));
                    }


                    using (PyScope scope = Py.CreateScope())
                    {
                        for (int i = 0; i < listaNotas.Count; i++)
                        {
                            pyTest.Append(new PyString(listaNotas[i]));
                            Console.WriteLine(listaNotas[i]);
                        }

                        scope.Set("test", pyTest);
                        dynamic chord   = scope.Get("test");
                        dynamic pychord = scope.Import("pychord");
                        dynamic music21 = scope.Import("music21");
                        dynamic myChord = pychord.analyzer.find_chords_from_notes(chord);


                        if (Convert.ToString(myChord) == "[]")
                        {
                            myChord = music21.chord.Chord(chord);
                            myChord = myChord.pitchedCommonName;
                        }

                        Console.WriteLine(myChord);

                        string value = Convert.ToString(myChord);
                        notaSalida.Text = value;
                    }
                }
            }


            catch
            {
            }
        }
Example #24
0
        public static PySignature GetSignature(PyScope scope, string functionName)
        {
            var s = new PySignature();

            PyObject fn = scope.Get(functionName);

            var inspect    = Py.Import("inspect");
            var signature  = inspect.InvokeMethod("signature", fn);
            var parameters = signature.GetAttr("parameters");

            var keys          = parameters.InvokeMethod("keys");
            var argumentNames = new List <string>();

            foreach (var key in keys.OfType <PyObject>())
            {
                var p    = parameters[key];
                var name = p.GetAttr("name").As <string>();
                argumentNames.Add(name);
            }

            PyDict annotations = new PyDict(fn.GetAttr("__annotations__"));

            foreach (var argumentName in argumentNames)
            {
                Type type;
                if (annotations.HasKey(argumentName))
                {
                    var pyType = annotations.GetItem(argumentName);
                    type = ToClrType(pyType);
                }
                else
                {
                    type = typeof(object);
                }
                s.Arguments.Add(new PyArgument {
                    Name = argumentName, Type = type
                });
            }

            Type returnType;

            if (annotations.HasKey("return"))
            {
                var returnPyType = annotations.GetItem("return");
                returnType = ToClrType(returnPyType);
            }
            else
            {
                returnType = typeof(object);
            }

            s.ReturnType = returnType;

            return(s);
        }
Example #25
0
 /// <summary>
 /// Called when the evaluation has completed successfully or failed
 /// </summary>
 /// <param name="isSuccessful">Whether the evaluation succeeded or not</param>
 /// <param name="scope">The scope in which the code is executed</param>
 /// <param name="code">The code to that was evaluated</param>
 /// <param name="bindingValues">The binding values - these are already added to the scope when called</param>
 private static void OnEvaluationEnd(bool isSuccessful,
                                     PyScope scope,
                                     string code,
                                     IList bindingValues)
 {
     if (EvaluationEnd != null)
     {
         EvaluationEnd(isSuccessful ? EvaluationState.Success : EvaluationState.Failed,
                       scope, code, bindingValues);
     }
 }
Example #26
0
        /// <summary>
        /// Migrates Python 2 code to Python 3 using Pythons 2to3 library.
        /// </summary>
        /// <param name="code">Python 2 code that needs to be migrated</param>
        /// <returns></returns>
        internal static string MigrateCode(string code)
        {
            InstallPython();

            if (!PythonEngine.IsInitialized)
            {
                PythonEngine.Initialize();
                PythonEngine.BeginAllowThreads();
            }

            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                using (Py.GIL())
                {
                    string output;
                    var    asm = Assembly.GetExecutingAssembly();

                    using (PyScope scope = Py.CreateScope())
                    {
                        scope.Set(INPUT_NAME, code.ToPython());

                        var path = Path.GetDirectoryName(asm.Location);

                        scope.Set(PATH_NAME, path.ToPython());
                        scope.Exec(Get2To3MigrationScript(asm));

                        output = scope.Contains(RETURN_NAME) ? scope.Get(RETURN_NAME).ToString() : string.Empty;
                    }

                    // If the code contains tabs, normalize the whitespaces. This is a Python 3 requirement
                    // that's not addressed by 2to3.
                    if (output.Contains("\t"))
                    {
                        using (PyScope scope = Py.CreateScope())
                        {
                            scope.Set(INPUT_NAME, output.ToPython());
                            scope.Exec(GetReindentationScript(asm));
                            output = scope.Contains(RETURN_NAME) ? scope.Get(RETURN_NAME).ToString() : string.Empty;
                        }
                    }

                    return(output);
                }
            }

            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
        }
Example #27
0
        public void TestPythonExtractors()
        {
            using (Py.GIL())
            {
                PyScope = Py.CreateScope();


                dynamic os = PyScope.Import("os");
                os.chdir(Environment.CurrentDirectory);

                dynamic ytdl_extactor = PyScope.Import("fake_youtube_dl.youtube_dl.extractor");
                //LogInfo("imported youtube_dl");

                //PyScope.Exec("from youtube_dl.extractor import get_info_extractor, gen_extractor_classes");
                //PyScope.Exec("from youtube_dl import YoutubeDL");
                dynamic extractors = ytdl_extactor.gen_extractor_classes();

                dynamic fakeytdl      = PyScope.Import("fake_youtube_dl");
                dynamic fakeytdlclass = fakeytdl.FakeYTDL(this.ToPython());

                foreach (dynamic ie in extractors)
                {
                    dynamic extractor = ytdl_extactor.get_info_extractor(ie.ie_key())(fakeytdlclass);
                    foreach (var t in extractor.get_testcases(true))
                    {
                        string  url = (string)t.get("url", "");
                        dynamic inf = t.get("info_dict");
                        string  id  = "unknown";
                        if (inf != null)
                        {
                            string title = (string)inf.get("title", "unknown");
                            id = (string)inf.get("id", title);
                        }

                        try
                        {
                            dynamic infoDict = extractor.extract(url);
                            Success(id, (string)ie.ie_key());
                        }
                        catch (Exception ex)
                        {
                            Failed(id, (string)ie.ie_key(), ex.Message);
                        }
                    }

                    //PyScope.Exec("from .fakeytdl import FakeYTDL");
                    //dynamic fakeytdl = PyScope.Eval("FakeYTDL");
                    //dynamic fakeytdlclass = fakeytdl(this.ToPython());
                    //PyScope.Exec("from fake_youtube_dl.youtube_dl.extractor.youtube import *");
                }
            }
        }
Example #28
0
 /// <summary>
 /// Called immediately before evaluation starts
 /// </summary>
 /// <param name="scope">The scope in which the code is executed</param>
 /// <param name="code">The code to be evaluated</param>
 /// <param name="bindingValues">The binding values - these are already added to the scope when called</param>
 private static void OnEvaluationBegin(PyScope scope,
                                       string code,
                                       IList bindingValues)
 {
     if (EvaluationBegin != null)
     {
         EvaluationBegin(EvaluationState.Begin, scope, code, bindingValues);
         Analytics.TrackEvent(
             Dynamo.Logging.Actions.Start,
             Dynamo.Logging.Categories.PythonOperations,
             "CPythonEvaluation");
     }
 }
Example #29
0
        public static void ExecFile(this PyScope scope, string path)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }
            var txt = File.ReadAllText(path, Encoding.UTF8);

            using (Py.GIL()) {
                var script = PythonEngine.Compile(txt, new FileInfo(path).FullName, RunFlagType.File);
                scope.Execute(script)?.Dispose();
            }
        }
Example #30
0
        public static void LoadScript(string name, string file)
        {
            PyScope scriptScope = Py.CreateScope();

            scriptScope.Exec(File.ReadAllText(Path.Combine("PyScripts", file)));
            if (ScriptScopes.ContainsKey(name))
            {
                ScriptScopes[name].Dispose();
                ScriptScopes.Remove(name);
            }
            ScriptScopes.Add(name, scriptScope);
            scriptScope.Set("debuglog", (Action <string>)Debug.Log);
            scriptScope.Set("debugmsgbox", (Action <string>)Debug.MsgBox);
        }