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

Danny Fernandez fernandez.dan at gmail.com
Wed Jul 14 14:48:51 CEST 2010


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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100714/c0e90639/attachment.html>


More information about the Ironpython-users mailing list