Re: [IronPython] .NET can see Python classes?
I see. Currently, my python class extends a CLR class, but the python class is not resolvable from CLR. I want a CLR persistence engine to be able to reflect on the python class as if it were a CLR class, but it says it cannot find the MyPython class created in my python script. I guess my python classes also do not fall under a given CLR namespace either? Now, if it were a pure interface implementation, I suppose its not possible to pass an instance of MyPython back to CLR to process as a normal class instance (including reflection)? thanks. I really like IronPython and it works great embedded in my app. Darren - Original Message - From: Curt Hagenlocher To: Discussion of IronPython Sent: Sunday, August 05, 2007 12:31 PM Subject: Re: [IronPython] .NET can see Python classes? On 8/4/07, Darren Govoni [EMAIL PROTECTED] wrote: Can .NET see python defined classes or objects? I define a class in Python but one of my .NET classes tries to find it via reflection and it cannot. I saw a post about this that is over a year old saying it was not then supported. The fundamental issue here is that Python classes don't follow the same semantics as CLR classes. They are, for instance, mutable after construction in ways that CLR classes are not. In order to expose a Python class directly to the CLR, it would have to be frozen in some manner, and you might prefer to be able to specify the types of the parameters instead of dealing with everything as object. This would require some sort of non-Python syntax to accomplish. Fortunately, there already is such a thing: a CLR interface. Define an interface in an external assembly and you can derive from it in Python. Your CLR classes can then access the Python code through the interface. -- Curt Hagenlocher [EMAIL PROTECTED] -- ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] .NET can see Python classes?
On Sun, 5 Aug 2007, Curt Hagenlocher wrote: On 8/4/07, Darren Govoni [EMAIL PROTECTED] wrote: [...] construction in ways that CLR classes are not. In order to expose a Python class directly to the CLR, it would have to be frozen in some manner, and A metaclass could achieve that. you might prefer to be able to specify the types of the parameters instead of dealing with everything as object. This would require some sort of non-Python syntax to accomplish. Fortunately, there already is such a Maybe in the future this will be used? http://www.python.org/dev/peps/pep-3107/ John ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] .NET can see Python classes?
On 8/5/07, Curt Hagenlocher [EMAIL PROTECTED] wrote: On 8/4/07, Darren Govoni [EMAIL PROTECTED] wrote: The fundamental issue here is that Python classes don't follow the same semantics as CLR classes. They are, for instance, mutable after construction in ways that CLR classes are not. In order to expose a Python class directly to the CLR, it would have to be frozen in some manner, and you might prefer to be able to specify the types of the parameters instead of dealing with everything as object. This would require some sort of non-Python syntax to accomplish. Fortunately, there already is such a thing: a CLR interface. Define an interface in an external assembly and you can derive from it in Python. Your CLR classes can then access the Python code through the interface. -- Curt Hagenlocher [EMAIL PROTECTED] hi curt, i to would like to do this. i don't understand a part of this. i understand creating a CLR interface and i understand creating a python file that has a class that derives from the CLR interface. but how does the CLR code instantiate the derived class and access the Python code through the interface? where does this instance come from? thanks, bryan ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] .NET can see Python classes?
On 8/5/07, Bryan [EMAIL PROTECTED] wrote: i to would like to do this. i don't understand a part of this. i understand creating a CLR interface and i understand creating a python file that has a class that derives from the CLR interface. but how does the CLR code instantiate the derived class and access the Python code through the interface? where does this instance come from? You would have to interact directly with the IronPython engine from the CLR code in order to get an instance of the class. Something like the code that Ori posted earlier in the thread would work for that. -- Curt Hagenlocher [EMAIL PROTECTED] ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] .NET can see Python classes?
Ori wrote: you can try the following code: PythonEngine engine = new PythonEngine(); EngineModule module = engine.CreateModule(); engine.Execute(handleCode(code, def), module); # code contains definition for python class 'PyClass' UserType ptype = module.Globals[PyClass] as UserType; object obj = Ops.Call(ptype); Here is a full example: using System; using IronPython.Hosting; using IronPython.Runtime.Types; using IronPython.Runtime.Operations; namespace TestPythonConsole { class Program { static void Main() { string code = import clr\r\nclr.AddReference('TestPythonConsole')\r\nfrom TestPythonConsole import Test\r\nclass X(Test):\r\n def test(self):\r\nreturn 'hello'\r\n\r\n; PythonEngine engine = new PythonEngine(); EngineModule module = engine.CreateModule(); engine.DefaultModule = module; engine.Execute(code); UserType ptype = module.Globals[X] as UserType; Test obj = Ops.Call(ptype) as Test; Console.WriteLine(obj.test()); } } public class Test { virtual public string test() { return goodbye; } } } The Python code imports 'Test' (it has to add the right reference to the assembly first). It then subclasses 'Test' in a class called X. The class we pull out of the Python engine globals is called and the result cast to Test. Calling the method 'test' calls the subclass rather than the parent class. I hope that helps. Michael Foord http://www.ironpython.info/ Darren Govoni-2 wrote: Hi, Can .NET see python defined classes or objects? I define a class in Python but one of my .NET classes tries to find it via reflection and it cannot. I saw a post about this that is over a year old saying it was not then supported. Could this possible in IP 2.0? thank you, D ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] .NET can see Python classes?
On 8/5/07, Michael Foord [EMAIL PROTECTED] wrote: Here is a full example: using System; using IronPython.Hosting; using IronPython.Runtime.Types; using IronPython.Runtime.Operations; namespace TestPythonConsole { class Program { static void Main() { string code = import clr\r\nclr.AddReference('TestPythonConsole')\r\nfrom TestPythonConsole import Test\r\nclass X(Test):\r\n def test(self):\r\nreturn 'hello'\r\n\r\n; PythonEngine engine = new PythonEngine(); EngineModule module = engine.CreateModule(); engine.DefaultModule = module; engine.Execute(code); UserType ptype = module.Globals[X] as UserType; Test obj = Ops.Call(ptype) as Test; Console.WriteLine(obj.test()); } } public class Test { virtual public string test() { return goodbye; } } } Michael Foord this is great news. this is very helpful... thanks. i assume there is another Execute method that you can pass in a path to file or take a file object so we don't have to write the code in a C# string. what i would like to do is create the skeleton of C# web services, then have it call out to python code. what you just showed here is that this is definitely possible. also, i see that your Test was a class and not a pure interface. i hope this will work with pure interfaces too. bryan ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] .NET can see Python classes?
Bryan wrote: On 8/5/07, Michael Foord [EMAIL PROTECTED] wrote: Here is a full example: [snip...] this is great news. this is very helpful... thanks. i assume there is another Execute method that you can pass in a path to file or take a file object so we don't have to write the code in a C# string. I believe there is an ExecuteFile method but you should check the docs. what i would like to do is create the skeleton of C# web services, then have it call out to python code. what you just showed here is that this is definitely possible. also, i see that your Test was a class and not a pure interface. i hope this will work with pure interfaces too. I'm sure it will. I'm not sure of the exact semantics though. I know that I have created Python classes that inherit from .NET interfaces. Alternatively you could create an abstract C# class that implements the interface and inherit from that. If it does work, posting an example would be great. Michael http://www.ironpython.info/ bryan ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] .NET can see Python classes?
you can try the following code: PythonEngine engine = new PythonEngine(); EngineModule module = engine.CreateModule(); engine.Execute(handleCode(code, def), module); # code contains definition for python class 'PyClass' UserType ptype = module.Globals[PyClass] as UserType; object obj = Ops.Call(ptype); Darren Govoni-2 wrote: Hi, Can .NET see python defined classes or objects? I define a class in Python but one of my .NET classes tries to find it via reflection and it cannot. I saw a post about this that is over a year old saying it was not then supported. Could this possible in IP 2.0? thank you, D ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com -- View this message in context: http://www.nabble.com/-IronPython--.NET-can-see-Python-classes--tf4218815.html#a12002302 Sent from the IronPython mailing list archive at Nabble.com. ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com