[IronPython] Access functions from other modules in an embedded environment

Curt Hagenlocher curt at hagenlocher.org
Wed Jul 14 07:02:57 PDT 2010


For this scenario, yes, you'd need to parse "moduleB.myFunB" into a module
name and function name, then pull moduleB out of the scope for moduleA, pull
myFunB out of the scope for moduleB and then execute it.

If you expect to get a function, Operations.InvokeMember is cleaner than
doing Operations.GetMember and casting to a delegate type.

If you want to execute an arbitrary snippet of code against the scope of
moduleA, (ie "moduleB.myFunB()") you can just compile the code and execute
it by using ScriptEngine.Execute(string, ScriptScope).

On Wed, Jul 14, 2010 at 5:48 AM, Danny Fernandez <fernandez.dan at gmail.com>wrote:

> Hi Curt,
>
>  In my C# test app just simple demo app for me to learn more. I have a
> textbox that I enter the function name I want to execute with my entry point
> using "moduleA.py". If I type myFunA and enter I execute this pretty
> straightforward I think. If I type moduleB.myFunB in the textbox and
> execute. I am not sure how best to access functions that were imported.
>
> If I use the ScriptRuntime.UseFile
>
> ScriptScope scope = engine.RunTime.UseFile("ModuleA");
>
> What would be the best way to get access to myFunB defined in ModuleB?
>
> If this doesn't make sense it's all good but its weird how things make
> sense in your own head.
>
> Thanks
>
> Danny
>
>
>   On Tue, Jul 13, 2010 at 9:59 AM, Curt Hagenlocher <curt at hagenlocher.org>wrote:
>
>> The easiest way to import a file as a module is with
>> ScriptRuntime.UseFile.
>>
>>   On Mon, Jul 12, 2010 at 2:51 PM, Danny Fernandez <
>> fernandez.dan at gmail.com> wrote:
>>
>>>   Hi I am planning on using IronPython 2.6.1 .NET 2.0 in a embedded in
>>> my C# app. I am playing with able
>>> to call python functions from C#. I had a question about how do you call
>>> other functions from modules
>>> that were imported. Here is a simple example
>>>
>>> moduleA.py has the following
>>>
>>> import moduleB
>>>
>>> def myFunA():
>>>  return 1
>>>
>>>
>>> moduleB.py has the following
>>>
>>> def myFunB():
>>>  return 2
>>>
>>> This is what I have in my sample C# program.
>>>
>>> ScriptSource source = _engine.CreateScriptSourceFromFile("moduleA);
>>> CompiledCode compiledCode = source.Compile();
>>> ScriptScope scope = _engine.CreateScope();
>>> compiledCode.Execute(scope);
>>>
>>>
>>> I can get access to myFunA straight forward with
>>>
>>> Func<int> funcA = scope.GetVariable<Func<int>>("myFunA");
>>>
>>>
>>> How do I access other functions from other modules in this simple case
>>> myFunB from the import moduleB?
>>> I was able to with the following but I am not sure if this is correct way
>>> or if there are better way
>>>
>>> PythonModule stuff = scope.GetVariable<PythonModule>("moduleB");
>>> Func<int> funcB= _engine.Operations.GetMember<Func<int>>(stuff,
>>> "myFunB");
>>>
>>>
>>> Thanks
>>>
>>> Danny
>>>
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20100714/ac16b0da/attachment.htm>


More information about the Users mailing list