See this section of my hosting article:
http://www.voidspace.org.uk/ironpython/hosting_api.shtml#functions-as-delegates
You cast the function to a delegate as you fetch it out of the scope.
Michael
Patrick van der Willik wrote:
I'm a bit late with responding to this as I was kinda busy. I used the
code that was given Stephen and it seems to run properly without
throwing exceptions. However, I created a little autoexec.py file with
a simple function in it with 2 parameters.
The main problem here is: How do I call the function through the
scope? I used engine.CreateScope() to create myself a new scope, but
from there on, I'm basically lost(again). What I basically want to do,
is call my function 'add' with 2 parameters(say, 10 and 15). The
modules itself are loaded into a ScriptSource.
Thanks,
Patrick
Lepisto, Stephen P wrote:
What I do when I want to work with python modules from embedded
IronPython is set the IronPython search path with the path of the
module to load then create and execute a small python script that
loads the main python modules into the current scope.
string moduleName = Path.GetFileName(modulePath);
string path = Path.GetDirectoryName(modulePath);
ICollection<string> paths = _pythonEngine.GetSearchPaths();
if (!paths.Contains(path))
{
paths.Add(path);
_pythonEngine.SetSearchPaths(paths);
}
string modInvoke = String.Format("import {0}\nfrom {0} import *\n",
moduleName);
ScriptSource source =
_pythonEngine.CreateScriptSourceFromString(modInvoke,
Microsoft.Scripting.SourceCodeKind.Statements);
Where modulePath is the full path to the python module or package to
load.
I can then invoke methods or access attributes using the IronPython
scope. In this way, I can interact with the python modules for as
long as necessary before closing down the scope/engine.
*From:* [email protected]
[mailto:[email protected]] *On Behalf Of *Dody
Gunawinata
*Sent:* Thursday, June 11, 2009 9:09 AM
*To:* Discussion of IronPython
*Subject:* Re: [IronPython] Embedding IronPython and calling python
functions from C#
You can read all yours file scripts, then using StringBuilder to
combine then and call CreateScriptSourceFromString()
Then you can call your functions within the combined scripts
normally. Pretty much you are creating a giant source code on the fly.
On Thu, Jun 11, 2009 at 5:32 PM, Patrick van der Willik
<[email protected] <mailto:[email protected]>> wrote:
I'm currently attempting to embed the IronPython 2 runtimes into an
existing application written in C#. However, I find the amount of
documentation lacking on what I'm trying to do. I currently have a
proof-of-concept version which uses Lua and LuaInterface, but the
people who have to write the scripts dislike Lua(Well, more hate it
with a passion) and would love to see this working with Python.
My host application is a networked application that must trigger
certain scripts functions on events generated by the connected
clients. The idea is that when my application starts, it will load
the IronPython script environment, launches an 'autoexec.py' which
will load various other scripts files and do some housekeeping. Once
this all is completed, it will start listening to incoming
connections. However, in various scenarios, the application has to
trigger scripted functions when data is received from a client. Which
script function is called is different per client and per event. I
have events for connecting, logging on, disconnecting and a set of
data specific events after receiving data. This highly depends on the
received packets.
My question here is: How do I embed IronPython in such a fashion that
I can load my scripts and then trigger various functions within that?
I've seen many examples that just call CreateScriptSourceFromString()
or File each time in which just 1 piece of code is implemented. This
is not suitable for the needs here because the scripted systems can
become quite complex.
With regards,
Patrick
_______________________________________________
Users mailing list
[email protected] <mailto:[email protected]>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
--
nomadlife.org <http://nomadlife.org>
------------------------------------------------------------------------
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
------------------------------------------------------------------------
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com