2005年07月22日 星期五 08:57
跟IronPython组发了一个邮件,询问怎样在C#里面嵌入Python代码。大牛很快就来了回信。 IronPython的embeding和Extension要比CPython的容易1000倍。我真是有些迫不及待地盼望它的发布了。 顺便说一下,代码可能Martin Maly是从老的代码库里直接贴过来的,所以有一行要改一改。 from System.Windows.Forms import Button 改成 from System.Windows.Forms import Button, Applicaton 此外Jim Hugunin来信说在程序里面调用LoadAssemblyByName太危险,所以第二个例子里要用 //------------------------------------------------------------ static void Main(string[] args) { PythonEngine engine = new PythonEngine(); engine.LoadAssembly(typeof(Program).Assembly); engine.Execute( "import Embed \n" + "Embed.Program.Value = 20 \n"); Console.WriteLine(Value); } //----------------------------------------------------------- 感兴趣的可以去订阅IronPython的列表。 ---------- Forwarded message ---------- From: Martin Maly <martmaly at exchange.microsoft.com> Date: Jul 21, 2005 4:50 PM Subject: RE: [IronPython] How to call IronPython Interpreter with C# code? To: shhgs <shhgs.efhilt at gmail.com>, Discussion of IronPython <users-ironpython.com at lists.ironpython.com> You can call IronPython through the IronPython.Hosting.PythonEngine object. Here's a trivial example: using IronPython.Hosting; class Program { static void Main(string[] args) { PythonEngine engine = new PythonEngine(); Console.WriteLine( engine.Evaluate("2 + 2") ); } } To get this to compile, add reference to IronPython.dll The other interesting methods on PythonEngine are: public void AddToPath(string dirName); ... adds path to sys.path public void Execute(string text); public object Evaluate(string expr); public void ExecuteFile(string fileName); public int RunFileInNewModule(string fileName); Here is another example, little more interesting, I think. The program (running as Embed.exe) will execute the Python statement, which in turn will load the Embed's type information and modify the static field. using System; using IronPython.Hosting; namespace Embed { public class Program { public static int Value = 0; static void Main(string[] args) { PythonEngine engine = new PythonEngine(); engine.Execute( "import sys \n" + "sys.LoadAssemblyByName(\"Embed\") \n"+ "import Embed \n" + "Embed.Program.Value = 20 \n"); Console.WriteLine(Value); } } } And the last example that goes even further and uses SetVariable method: using System; using IronPython.Hosting; using System.Windows.Forms; namespace Embed { public class Program { static void Main(string[] args) { PythonEngine engine = new PythonEngine(); Form form = new Form(); engine.SetVariable("form", form); engine.Execute( "import sys \n" + "sys.LoadAssemblyByName('System.Windows.Forms') \n" + "from System.Windows.Forms import Button \n" + "def on_exit(*args): \n" + " Application.Exit() \n" + "b = Button(Text='Exit') \n" + "b.Click += on_exit \n" + "form.Controls.Add(b) \n" + "form.Show() \n"); Application.Run(); } } } I hope this helps. Martin -----Original Message----- From: users-ironpython.com-bounces at lists.ironpython.com [mailto:users-ironpython.com-bounces at lists.ironpython.com] On Behalf Of shhgs Sent: Wednesday, July 20, 2005 4:18 PM To: users-ironpython.com at lists.ironpython.com Subject: [IronPython] How to call IronPython Interpreter with C# code? Can somebody tell me how to call IronPython Interpreter with C# code, so that I can embed Python into the .NET application. Thank you!
2005年07月22日 星期五 10:37
方便吗?虽然没做过,但感觉与C调python差不多啊。C中也只需要几个方法就可以调了。 在 05-7-22,shhgs<shhgs.efhilt at gmail.com> 写道: > 跟IronPython组发了一个邮件,询问怎样在C#里面嵌入Python代码。大牛很快就来了回信。 > > IronPython的embeding和Extension要比CPython的容易1000倍。我真是有些迫不及待地盼望它的发布了。 > > 顺便说一下,代码可能Martin Maly是从老的代码库里直接贴过来的,所以有一行要改一改。 > > from System.Windows.Forms import Button > 改成 > from System.Windows.Forms import Button, Applicaton > > 此外Jim Hugunin来信说在程序里面调用LoadAssemblyByName太危险,所以第二个例子里要用 > //------------------------------------------------------------ > static void Main(string[] args) { > PythonEngine engine = new PythonEngine(); > engine.LoadAssembly(typeof(Program).Assembly); > engine.Execute( > "import Embed \n" + > "Embed.Program.Value = 20 \n"); > > Console.WriteLine(Value); > } > //----------------------------------------------------------- > > 感兴趣的可以去订阅IronPython的列表。 > > > ---------- Forwarded message ---------- > From: Martin Maly <martmaly at exchange.microsoft.com> > Date: Jul 21, 2005 4:50 PM > Subject: RE: [IronPython] How to call IronPython Interpreter with C# code? > To: shhgs <shhgs.efhilt at gmail.com>, Discussion of IronPython > <users-ironpython.com at lists.ironpython.com> > > > You can call IronPython through the IronPython.Hosting.PythonEngine > object. > Here's a trivial example: > > using IronPython.Hosting; > class Program { > static void Main(string[] args) { > PythonEngine engine = new PythonEngine(); > Console.WriteLine( engine.Evaluate("2 + 2") ); > } > } > > To get this to compile, add reference to IronPython.dll > > The other interesting methods on PythonEngine are: > > public void AddToPath(string dirName); ... adds path to > sys.path > public void Execute(string text); > public object Evaluate(string expr); > public void ExecuteFile(string fileName); > public int RunFileInNewModule(string fileName); > > > Here is another example, little more interesting, I think. The program > (running as Embed.exe) will > execute the Python statement, which in turn will load the Embed's type > information and modify the static field. > > using System; > using IronPython.Hosting; > > namespace Embed { > public class Program { > public static int Value = 0; > > static void Main(string[] args) { > PythonEngine engine = new PythonEngine(); > > engine.Execute( > "import sys \n" + > "sys.LoadAssemblyByName(\"Embed\") \n"+ > "import Embed \n" + > "Embed.Program.Value = 20 \n"); > > Console.WriteLine(Value); > } > } > } > > And the last example that goes even further and uses SetVariable method: > > using System; > using IronPython.Hosting; > using System.Windows.Forms; > > namespace Embed { > public class Program { > static void Main(string[] args) { > PythonEngine engine = new PythonEngine(); > > Form form = new Form(); > engine.SetVariable("form", form); > > engine.Execute( > "import sys \n" > + > "sys.LoadAssemblyByName('System.Windows.Forms') \n" > + > "from System.Windows.Forms import Button \n" > + > "def on_exit(*args): \n" > + > " Application.Exit() \n" > + > "b = Button(Text='Exit') \n" > + > "b.Click += on_exit \n" > + > "form.Controls.Add(b) \n" > + > "form.Show() > \n"); > > Application.Run(); > } > } > } > > I hope this helps. > > Martin > > -----Original Message----- > From: users-ironpython.com-bounces at lists.ironpython.com > [mailto:users-ironpython.com-bounces at lists.ironpython.com] On Behalf Of > shhgs > Sent: Wednesday, July 20, 2005 4:18 PM > To: users-ironpython.com at lists.ironpython.com > Subject: [IronPython] How to call IronPython Interpreter with C# code? > > Can somebody tell me how to call IronPython Interpreter with C# code, so > that I can embed Python into the .NET application. > > Thank you! > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- I like python! My Donews Blog: http://www.donews.net/limodou New Google Maillist: http://groups-beta.google.com/group/python-cn
2005年07月22日 星期五 15:12
比C方便的不是一点点。 IronPython调用C#的源代码要比C#自己调用都方便。C#里到处要写变量的类型,IronPython就不用这么唧唧歪歪的,直接一步就行了。 至于调用Python的解释器,C下面我根本没试过,IronPython倒是一次成功。 On 7/21/05, limodou <limodou at gmail.com> wrote: > 方便吗?虽然没做过,但感觉与C调python差不多啊。C中也只需要几个方法就可以调了。 > > 在 05-7-22,shhgs<shhgs.efhilt at gmail.com> 写道: > > 跟IronPython组发了一个邮件,询问怎样在C#里面嵌入Python代码。大牛很快就来了回信。 > > > > IronPython的embeding和Extension要比CPython的容易1000倍。我真是有些迫不及待地盼望它的发布了。 > > > > 顺便说一下,代码可能Martin Maly是从老的代码库里直接贴过来的,所以有一行要改一改。 > > > > from System.Windows.Forms import Button > > 改成 > > from System.Windows.Forms import Button, Applicaton > > > > 此外Jim Hugunin来信说在程序里面调用LoadAssemblyByName太危险,所以第二个例子里要用 > > //------------------------------------------------------------ > > static void Main(string[] args) { > > PythonEngine engine = new PythonEngine(); > > engine.LoadAssembly(typeof(Program).Assembly); > > engine.Execute( > > "import Embed \n" + > > "Embed.Program.Value = 20 \n"); > > > > Console.WriteLine(Value); > > } > > //----------------------------------------------------------- > > > > 感兴趣的可以去订阅IronPython的列表。 > > > > > > ---------- Forwarded message ---------- > > From: Martin Maly <martmaly at exchange.microsoft.com> > > Date: Jul 21, 2005 4:50 PM > > Subject: RE: [IronPython] How to call IronPython Interpreter with C# code? > > To: shhgs <shhgs.efhilt at gmail.com>, Discussion of IronPython > > <users-ironpython.com at lists.ironpython.com> > > > > > > You can call IronPython through the IronPython.Hosting.PythonEngine > > object. > > Here's a trivial example: > > > > using IronPython.Hosting; > > class Program { > > static void Main(string[] args) { > > PythonEngine engine = new PythonEngine(); > > Console.WriteLine( engine.Evaluate("2 + 2") ); > > } > > } > > > > To get this to compile, add reference to IronPython.dll > > > > The other interesting methods on PythonEngine are: > > > > public void AddToPath(string dirName); ... adds path to > > sys.path > > public void Execute(string text); > > public object Evaluate(string expr); > > public void ExecuteFile(string fileName); > > public int RunFileInNewModule(string fileName); > > > > > > Here is another example, little more interesting, I think. The program > > (running as Embed.exe) will > > execute the Python statement, which in turn will load the Embed's type > > information and modify the static field. > > > > using System; > > using IronPython.Hosting; > > > > namespace Embed { > > public class Program { > > public static int Value = 0; > > > > static void Main(string[] args) { > > PythonEngine engine = new PythonEngine(); > > > > engine.Execute( > > "import sys \n" + > > "sys.LoadAssemblyByName(\"Embed\") \n"+ > > "import Embed \n" + > > "Embed.Program.Value = 20 \n"); > > > > Console.WriteLine(Value); > > } > > } > > } > > > > And the last example that goes even further and uses SetVariable method: > > > > using System; > > using IronPython.Hosting; > > using System.Windows.Forms; > > > > namespace Embed { > > public class Program { > > static void Main(string[] args) { > > PythonEngine engine = new PythonEngine(); > > > > Form form = new Form(); > > engine.SetVariable("form", form); > > > > engine.Execute( > > "import sys \n" > > + > > "sys.LoadAssemblyByName('System.Windows.Forms') \n" > > + > > "from System.Windows.Forms import Button \n" > > + > > "def on_exit(*args): \n" > > + > > " Application.Exit() \n" > > + > > "b = Button(Text='Exit') \n" > > + > > "b.Click += on_exit \n" > > + > > "form.Controls.Add(b) \n" > > + > > "form.Show() > > \n"); > > > > Application.Run(); > > } > > } > > } > > > > I hope this helps. > > > > Martin > > > > -----Original Message----- > > From: users-ironpython.com-bounces at lists.ironpython.com > > [mailto:users-ironpython.com-bounces at lists.ironpython.com] On Behalf Of > > shhgs > > Sent: Wednesday, July 20, 2005 4:18 PM > > To: users-ironpython.com at lists.ironpython.com > > Subject: [IronPython] How to call IronPython Interpreter with C# code? > > > > Can somebody tell me how to call IronPython Interpreter with C# code, so > > that I can embed Python into the .NET application. > > > > Thank you! > > > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > -- > I like python! > My Donews Blog: http://www.donews.net/limodou > New Google Maillist: http://groups-beta.google.com/group/python-cn > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > >
Zeuux © 2025
京ICP备05028076号