static void Main(string[] args) { // Create simple host environment var host = new DlrHost <IronPythonLanguage>(new IronPythonLanguage()); host.AddSearchPath(@"C:\Program Files (x86)\IronPython 2.7\Lib\"); // Add some resolver for embedded scripts/modules //host.AddImportResolver(new EmbeddedModuleResolver()); // Execute script by path //var val = host.DefaultScope.ExecuteScript("FileSample/FileSample.py"); //Console.WriteLine("Script value: " + val.ToString()); // Import some classes and use them host.DefaultScope.Execute("" // + "from System import Console" + "\r\n" //+ "import os\r\n" // + "import Math.MathImpl" + "\r\n" + "from importlib import import_module" + "\r\n" + "import_module('collections')" + "\r\n" //+ "globals()['defaultdict'] = getattr(import_module('collections'), 'defaultdict')" + "\r\n" //+ "print defaultdict" + "\r\n" //+ "# Ausgabe: <type '_collections.defaultdict'>" + "\r\n" //+ "import Simplic.Dlr" + "\r\n" + "" + "\r\n" //+ "Console.WriteLine(str(Math.add(1, 2)))" + "\r\n" + "" + "\r\n" + "" + "\r\n" + "" + "\r\n"); Console.ReadLine(); }
static void Main(string[] args) { // Create simple host environment var host = new DlrHost <IronPythonLanguage>(new IronPythonLanguage()); host.DefaultScope.Execute("import sys\r\n" + "class TestClass(object):\r\n" + " var1 = ''\r\n" + " def doPrint(self, txt):\r\n" + " print txt\r\n" + "" + " def doPrintVar(self):\r\n" + " print self.var1\r\n"); // Call via name of the method var instance = host.DefaultScope.CreateClassInstance("TestClass"); instance.CallMethod("doPrint", "Text to print?"); // Call directly over the embedded dynamic keyword dynamic dynInstance = instance; dynInstance.doPrint("Text 2 to print!"); // Set variable an print out instance.SetMember("var1", "Variable content 1."); instance.CallMethod("doPrintVar"); dynInstance.var1 = "Variable content 2."; dynInstance.doPrintVar(); // Cache System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); host.DefaultScope.Execute("print 'Hello World'"); watch.Stop(); Console.WriteLine("No cached: " + watch.ElapsedMilliseconds.ToString()); watch.Start(); host.DefaultScope.Execute("print 'Hello World'"); watch.Stop(); Console.WriteLine("Cached: " + watch.ElapsedMilliseconds.ToString()); Console.ReadLine(); }
static void Main(string[] args) { // Create simple host environment var host = new DlrHost<IronPythonLanguage>(new IronPythonLanguage()); host.DefaultScope.Execute("import sys\r\n" + "class TestClass(object):\r\n" + " var1 = ''\r\n" + " def doPrint(self, txt):\r\n" + " print txt\r\n" + "" + " def doPrintVar(self):\r\n" + " print self.var1\r\n"); // Call via name of the method var instance = host.DefaultScope.CreateClassInstance("TestClass"); instance.CallMethod("doPrint", "Text to print?"); // Call directly over the embedded dynamic keyword dynamic dynInstance = instance; dynInstance.doPrint("Text 2 to print!"); // Set variable an print out instance.SetMember("var1", "Variable content 1."); instance.CallMethod("doPrintVar"); dynInstance.var1 = "Variable content 2."; dynInstance.doPrintVar(); // Cache System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); host.DefaultScope.Execute("print 'Hello World'"); watch.Stop(); Console.WriteLine("No cached: " + watch.ElapsedMilliseconds.ToString()); watch.Start(); host.DefaultScope.Execute("print 'Hello World'"); watch.Stop(); Console.WriteLine("Cached: " + watch.ElapsedMilliseconds.ToString()); Console.ReadLine(); }
/// <summary> /// Start application and initialize all needed systems /// </summary> protected void Application_Start() { // Create scripting language = new IronPythonLanguage(); host = new DlrHost<IronPythonLanguage>(language); host.AddImportResolver(new NoImporter()); // Search paths Host.AddSearchPath(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath); // Load app.py and execute var source = Host.ScriptEngine.CreateScriptSourceFromString ( System.IO.File.ReadAllText(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "app.py") ); source.Execute(Host.DefaultScope.ScriptScope); app = (AspNetMvcAPI.Application)Host.DefaultScope.CreateClassInstance("App").Instance; // Call start app.start(); }
/// <summary> /// Start application and initialize all needed systems /// </summary> protected void Application_Start() { // Create scripting language = new IronPythonLanguage(); host = new DlrHost <IronPythonLanguage>(language); host.AddImportResolver(new NoImporter()); // Search paths Host.AddSearchPath(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath); // Load app.py and execute var source = Host.ScriptEngine.CreateScriptSourceFromString ( System.IO.File.ReadAllText(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "app.py") ); source.Execute(Host.DefaultScope.ScriptScope); app = (AspNetMvcAPI.Application)Host.DefaultScope.CreateClassInstance("App").Instance; // Call start app.start(); }
static void Main(string[] args) { if (args.Length > 0) { // Create host var language = new IronPythonLanguage(); language.Argv = args.ToList(); var host = new DlrHost <IronPythonLanguage>(language); host.AddSearchPath(@"C:\Program Files (x86)\IronPython 2.7\Lib\"); host.AddSearchPath(System.IO.Path.GetDirectoryName(args[0])); try { host.DefaultScope.ExecuteScript(args.First()); } catch (Exception ex) { Environment.Exit(1); } } }
static void Main(string[] args) { // Create simple host environment var host = new DlrHost<IronPythonLanguage>(new IronPythonLanguage()); // Define class host.DefaultScope.Execute("class MathClass(object):\r\n" + "" + " def add(self, x, y):\r\n" + " return x + y\r\n" + "" + " def sub(self, x, y):\r\n" + " return x - y\r\n"); // Use Math-class var cl = new MathClass(host.DefaultScope); // Call c# -> python Console.WriteLine("Add: 5 + 100 = " + cl.Add(5, 100)); Console.WriteLine("Sub: 90 - 14 = " + cl.Sub(90, 40)); Console.ReadLine(); }
static void Main(string[] args) { // Create simple host environment var host = new DlrHost <IronPythonLanguage>(new IronPythonLanguage()); // Define class host.DefaultScope.Execute("class MathClass(object):\r\n" + "" + " def add(self, x, y):\r\n" + " return x + y\r\n" + "" + " def sub(self, x, y):\r\n" + " return x - y\r\n"); // Use Math-class var cl = new MathClass(host.DefaultScope); // Call c# -> python Console.WriteLine("Add: 5 + 100 = " + cl.Add(5, 100)); Console.WriteLine("Sub: 90 - 14 = " + cl.Sub(90, 40)); Console.ReadLine(); }
static void Main(string[] args) { if (args.Length > 0) { // Create host var language = new IronPythonLanguage(); language.Argv = args.ToList(); var host = new DlrHost<IronPythonLanguage>(language); host.AddSearchPath(@"C:\Program Files (x86)\IronPython 2.7\Lib\"); host.AddSearchPath(System.IO.Path.GetDirectoryName(args[0])); try { host.DefaultScope.ExecuteScript(args.First()); } catch (Exception ex) { Environment.Exit(1); } } }
static void Main(string[] args) { // Create simple host environment var host = new DlrHost<IronPythonLanguage>(new IronPythonLanguage()); host.AddSearchPath(@"C:\Program Files (x86)\IronPython 2.7\Lib\"); // Add some resolver for embedded scripts/modules //host.AddImportResolver(new EmbeddedModuleResolver()); // Execute script by path //var val = host.DefaultScope.ExecuteScript("FileSample/FileSample.py"); //Console.WriteLine("Script value: " + val.ToString()); // Import some classes and use them host.DefaultScope.Execute("" // + "from System import Console" + "\r\n" //+ "import os\r\n" // + "import Math.MathImpl" + "\r\n" + "from importlib import import_module" + "\r\n" + "import_module('collections')" + "\r\n" //+ "globals()['defaultdict'] = getattr(import_module('collections'), 'defaultdict')" + "\r\n" //+ "print defaultdict" + "\r\n" //+ "# Ausgabe: <type '_collections.defaultdict'>" + "\r\n" //+ "import Simplic.Dlr" + "\r\n" + "" + "\r\n" //+ "Console.WriteLine(str(Math.add(1, 2)))" + "\r\n" + "" + "\r\n" + "" + "\r\n" + "" + "\r\n"); Console.ReadLine(); }