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; } } }
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 { } }
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 *"); } } }
public void TestImportModule() { using (Py.GIL()) { dynamic sys = ps.Import("sys"); Assert.IsTrue(ps.Contains("sys")); ps.Exec("sys.attr1 = 2"); var value1 = ps.Eval <int>("sys.attr1"); var value2 = sys.attr1.As <int>(); Assert.AreEqual(2, value1); Assert.AreEqual(2, value2); //import as ps.Import("sys", "sys1"); Assert.IsTrue(ps.Contains("sys1")); } }
internal ExecutionSession(IExecutionEngineIO manager, Dictionary <string, object> Values, HashSet <AssemblyDeclaration> Assemblies) { CanExecute = true; //IOManager IOManager = manager; // Create Scope Scope = InitializeScope(Values, Assemblies); if (Scope == null) { CanExecute = false; return; } // Redirect IO using (Py.GIL()) { dynamic SYS = Scope.Import("sys"); SYS.stdout = new PythonOutput(); SYS.stderr = new PythonError(); SYS.stdin = new PythonInput(); } if (IOManager?.GetOutput() != null) { Console.SetOut(IOManager.GetOutput()); } if (IOManager?.GetError() != null) { Console.SetError(IOManager.GetError()); } if (IOManager?.GetInput() != null) { Console.SetIn(IOManager.GetInput()); } }