Re: [IronPython] Need help using IronPython from C#
A while ago I asked a similar question about how to get to methods inside the engine from C#. This was the sample I got: public delegate int IntIntDelegate(int arg); engine.Execute("def IntIntMethod(a): return a * 100"); IntIntDelegate d = engine.Evaluate("IntIntMethod"); Console.WriteLine(d(2)); // This prints "200" You can try to adapt this and see if it works for you. It looks a bit easier this way. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jason Ferrara Sent: Saturday 12 August 2006 20:01 To: Discussion of IronPython Subject: Re: [IronPython] Need help using IronPython from C# On Aug 11, 2006, at 5:32 PM, Dino Viehland wrote: > You're quickly getting into the guts of the runtime, so you'll find > most of what you want over in Ops. > > To get an attribute off of an object you can do TryGetAttr(object > o, SymbolId name, out object ret). o would be your ls, SymbolId's > our what we use internally to represent attributes - you can get > one by doing SymbolTable.SymbolToId("foo"), and then we'll pass you > out the value. > > As you get the values back you'd need to re-create delegates to > them. Note if you're re-creating delegates of the same type we > won't need to do any additional code gen work - we'll just bind the > new object back to the existing method we created, so this > shouldn't be too nasty to do. If you're getting a property we'll > return the property value from Ops.GetAttr (basically if you hit a > descriptor we will run the descriptor code for you). And if you're > getting some plain old attribute back out then you can do whatever > you want w/ the value from there. > Ah. Thanks. This is what I needed. So to make a C# wrapper for a Python class I should do something like... class LogViewerService { public delegate int GetNumberOfEntriesDelegate(); public GetNumberOfEntriesDelegate GetNumberOfEntries; public delegate object GetEntryDelegate(int entryNumber); public GetEntryDelegate GetEntry; public delegate void closeDelegate(); public closeDelegate close; public LogViewerService(object pythonObject) { object method; IronPython.Runtime.Operations.Ops.TryGetAttr (pythonObject, IronPython.Runtime.SymbolTable.StringToId ("GetNumberOfEntries"), out method); GetNumberOfEntries = (GetNumberOfEntriesDelegate) IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof (GetNumberOfEntriesDelegate)); IronPython.Runtime.Operations.Ops.TryGetAttr (pythonObject, IronPython.Runtime.SymbolTable.StringToId("GetEntry"), out method); GetEntry = (GetEntryDelegate) IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof (GetEntryDelegate)); IronPython.Runtime.Operations.Ops.TryGetAttr (pythonObject, IronPython.Runtime.SymbolTable.StringToId("close"), out method); close = (closeDelegate) IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof (closeDelegate)); } } ___ 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] Need help using IronPython from C#
On Aug 11, 2006, at 5:32 PM, Dino Viehland wrote: > You're quickly getting into the guts of the runtime, so you'll find > most of what you want over in Ops. > > To get an attribute off of an object you can do TryGetAttr(object > o, SymbolId name, out object ret). o would be your ls, SymbolId's > our what we use internally to represent attributes - you can get > one by doing SymbolTable.SymbolToId("foo"), and then we'll pass you > out the value. > > As you get the values back you'd need to re-create delegates to > them. Note if you're re-creating delegates of the same type we > won't need to do any additional code gen work - we'll just bind the > new object back to the existing method we created, so this > shouldn't be too nasty to do. If you're getting a property we'll > return the property value from Ops.GetAttr (basically if you hit a > descriptor we will run the descriptor code for you). And if you're > getting some plain old attribute back out then you can do whatever > you want w/ the value from there. > Ah. Thanks. This is what I needed. So to make a C# wrapper for a Python class I should do something like... class LogViewerService { public delegate int GetNumberOfEntriesDelegate(); public GetNumberOfEntriesDelegate GetNumberOfEntries; public delegate object GetEntryDelegate(int entryNumber); public GetEntryDelegate GetEntry; public delegate void closeDelegate(); public closeDelegate close; public LogViewerService(object pythonObject) { object method; IronPython.Runtime.Operations.Ops.TryGetAttr (pythonObject, IronPython.Runtime.SymbolTable.StringToId ("GetNumberOfEntries"), out method); GetNumberOfEntries = (GetNumberOfEntriesDelegate) IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof (GetNumberOfEntriesDelegate)); IronPython.Runtime.Operations.Ops.TryGetAttr (pythonObject, IronPython.Runtime.SymbolTable.StringToId("GetEntry"), out method); GetEntry = (GetEntryDelegate) IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof (GetEntryDelegate)); IronPython.Runtime.Operations.Ops.TryGetAttr (pythonObject, IronPython.Runtime.SymbolTable.StringToId("close"), out method); close = (closeDelegate) IronPython.Runtime.Operations.Ops.GetDelegate(method, typeof (closeDelegate)); } } ___ users mailing list users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Need help using IronPython from C#
You're quickly getting into the guts of the runtime, so you'll find most of what you want over in Ops. To get an attribute off of an object you can do TryGetAttr(object o, SymbolId name, out object ret). o would be your ls, SymbolId's our what we use internally to represent attributes - you can get one by doing SymbolTable.SymbolToId("foo"), and then we'll pass you out the value. As you get the values back you'd need to re-create delegates to them. Note if you're re-creating delegates of the same type we won't need to do any additional code gen work - we'll just bind the new object back to the existing method we created, so this shouldn't be too nasty to do. If you're getting a property we'll return the property value from Ops.GetAttr (basically if you hit a descriptor we will run the descriptor code for you). And if you're getting some plain old attribute back out then you can do whatever you want w/ the value from there. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jason Ferrara Sent: Friday, August 11, 2006 1:29 PM To: Discussion of IronPython Subject: Re: [IronPython] Need help using IronPython from C# On Aug 11, 2006, at 3:53 PM, Dino Viehland wrote: > > > If you really do want to get a delegate to a Python function that came > from a module you might want to look at Ops.GetDelegate. If you were > to create a module, execute its contents, and then get the method back > out by name as an object you could pass the method to Ops.GetDelegate > w/ a delegate type and create a delegate to the raw Python method that > you could call from C#. I think this is closer to what I want. I'm looking for clean way to expose an API written in Python to a C# program. So I can do MyDelegate ConnectToService = (MyDelegate) IronPython.Runtime.Operations.Ops.GetDelegate(e.Evaluate ("RemoteAdmin.ConnectToService"), typeof(MyDelegate)); and then... object ls = ConnectToService("localhost","LogViewer"); but now how do I get to the methods and attributes of ls? > > -Original Message- > From: [EMAIL PROTECTED] [mailto:users- > [EMAIL PROTECTED] On Behalf Of Jason Ferrara > Sent: Friday, August 11, 2006 12:08 PM > To: Discussion of IronPython > Subject: [IronPython] Need help using IronPython from C# > > I'd like to do the equivalent of something like... > > import RemoteAdmin > > ls = RemoteAdmin.ConnectToService("localhost","LogViewer") > numEntries = ls.GetNumberOfEntries() > e = ls.GetEntry(numEntries - 1) > entryString = str(e) > > > in C#. > > So I get as far as > > PythonEngine e = new PythonEngine(); > # The RemoteAdmin module is in an assembly built using > IronPython.Hosting.Compiler and then referenced by the C# project > ClrModule clr = (ClrModule) e.Import("clr"); > clr.AddReference(typeof(RemoteAdmin).Assembly) > e.Import("RemoteAdmin"); > > and then I'm lost. > > Is there a way to make a delegate from a method that exists in the > PythonEngine environment and then call the delegate from C#? > > I'm aware of the Evaluate and Execute methods of PythonEngine, but I'm > looking for a more direct way to call the python methods and access > python object attributes, rather than building up strings to pass to > Evaluate. > > Thanks > > - Jason > > > ___ > 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 ___ 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] Need help using IronPython from C#
On Aug 11, 2006, at 3:53 PM, Dino Viehland wrote: > > > If you really do want to get a delegate to a Python function that > came from a module you might want to look at Ops.GetDelegate. If > you were to create a module, execute its contents, and then get the > method back out by name as an object you could pass the method to > Ops.GetDelegate w/ a delegate type and create a delegate to the raw > Python method that you could call from C#. I think this is closer to what I want. I'm looking for clean way to expose an API written in Python to a C# program. So I can do MyDelegate ConnectToService = (MyDelegate) IronPython.Runtime.Operations.Ops.GetDelegate(e.Evaluate ("RemoteAdmin.ConnectToService"), typeof(MyDelegate)); and then... object ls = ConnectToService("localhost","LogViewer"); but now how do I get to the methods and attributes of ls? > > -Original Message- > From: [EMAIL PROTECTED] [mailto:users- > [EMAIL PROTECTED] On Behalf Of Jason Ferrara > Sent: Friday, August 11, 2006 12:08 PM > To: Discussion of IronPython > Subject: [IronPython] Need help using IronPython from C# > > I'd like to do the equivalent of something like... > > import RemoteAdmin > > ls = RemoteAdmin.ConnectToService("localhost","LogViewer") > numEntries = ls.GetNumberOfEntries() > e = ls.GetEntry(numEntries - 1) > entryString = str(e) > > > in C#. > > So I get as far as > > PythonEngine e = new PythonEngine(); > # The RemoteAdmin module is in an assembly built using > IronPython.Hosting.Compiler and then referenced by the C# project > ClrModule clr = (ClrModule) e.Import("clr"); > clr.AddReference(typeof(RemoteAdmin).Assembly) > e.Import("RemoteAdmin"); > > and then I'm lost. > > Is there a way to make a delegate from a method that exists in the > PythonEngine environment and then call the delegate from C#? > > I'm aware of the Evaluate and Execute methods of PythonEngine, but > I'm looking for a more direct way to call the python methods and > access python object attributes, rather than building up strings to > pass to Evaluate. > > Thanks > > - Jason > > > ___ > 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 ___ users mailing list users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Need help using IronPython from C#
What you're probably looking for is the CreateMethod method that hangs off of the PythonEngine. Using this you can do: delegate string GetEntryString(); GetEntryString es = engine.CreateMethod(@"import RemoteAdmin ls = RemoteAdmin.ConnectToService('localhost','LogViewer') numEntries = ls.GetNumberOfEntries() e = ls.GetEntry(numEntries - 1) entryString = str(e) return entryString"); Console.WriteLine(es()); If you really do want to get a delegate to a Python function that came from a module you might want to look at Ops.GetDelegate. If you were to create a module, execute its contents, and then get the method back out by name as an object you could pass the method to Ops.GetDelegate w/ a delegate type and create a delegate to the raw Python method that you could call from C#. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jason Ferrara Sent: Friday, August 11, 2006 12:08 PM To: Discussion of IronPython Subject: [IronPython] Need help using IronPython from C# I'd like to do the equivalent of something like... import RemoteAdmin ls = RemoteAdmin.ConnectToService("localhost","LogViewer") numEntries = ls.GetNumberOfEntries() e = ls.GetEntry(numEntries - 1) entryString = str(e) in C#. So I get as far as PythonEngine e = new PythonEngine(); # The RemoteAdmin module is in an assembly built using IronPython.Hosting.Compiler and then referenced by the C# project ClrModule clr = (ClrModule) e.Import("clr"); clr.AddReference(typeof(RemoteAdmin).Assembly) e.Import("RemoteAdmin"); and then I'm lost. Is there a way to make a delegate from a method that exists in the PythonEngine environment and then call the delegate from C#? I'm aware of the Evaluate and Execute methods of PythonEngine, but I'm looking for a more direct way to call the python methods and access python object attributes, rather than building up strings to pass to Evaluate. Thanks - Jason ___ 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
[IronPython] Need help using IronPython from C#
I'd like to do the equivalent of something like... import RemoteAdmin ls = RemoteAdmin.ConnectToService("localhost","LogViewer") numEntries = ls.GetNumberOfEntries() e = ls.GetEntry(numEntries - 1) entryString = str(e) in C#. So I get as far as PythonEngine e = new PythonEngine(); # The RemoteAdmin module is in an assembly built using IronPython.Hosting.Compiler and then referenced by the C# project ClrModule clr = (ClrModule) e.Import("clr"); clr.AddReference(typeof(RemoteAdmin).Assembly) e.Import("RemoteAdmin"); and then I'm lost. Is there a way to make a delegate from a method that exists in the PythonEngine environment and then call the delegate from C#? I'm aware of the Evaluate and Execute methods of PythonEngine, but I'm looking for a more direct way to call the python methods and access python object attributes, rather than building up strings to pass to Evaluate. Thanks - Jason ___ users mailing list users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com