Invoke a script funtion?

Mar 24, 2009 at 8:33 PM
Hi,
I am kind of a newbie on this. I have a question.
I have a script file written in IronPython containing more than one function. From DLR, is it possible to invoke a named function? I have looked at some functions to create script source
CreateScriptSource,CreateScriptSourceFromFile, CreateScriptSourceFromString and none of them lets me specify function name. Am I missing something?
Please  help!
Mar 24, 2009 at 10:43 PM

Is this what you are looking for?

var scope = engine.CreateScope()

engine.Execute(@”

def foo(a,b):

   return a+b

def bar(a,b):

  return a*b

def baz():

  return 3

“, scope);

var foo = scope.GetVariable<Func<int, int, int>>(“foo”)

Console.WriteLine(foo(3, 4));

var bar = scope.GetVariable<Func<double, double, double>>(“bar”)

Console.WriteLine(bar(3.0, 4.0));

var baz = scope.GetVariable<Func<int>>(“baz”)

Console.WriteLine(baz());

Tomas

From: dvn21 [mailto:notifications@codeplex.com]
Sent: Tuesday, March 24, 2009 12:34 PM
To: Tomas Matousek
Subject: Invoke a script funtion? [dlr:51160]

From: dvn21

Hi,
I am kind of a newbie on this. I have a question.
I have a script file written in IronPython containing more than one function. From DLR, is it possible to invoke a named function? I have looked at some functions to create script source
CreateScriptSource,CreateScriptSourceFromFile, CreateScriptSourceFromString and none of them lets me specify function name. Am I missing something?
Please help!

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Coordinator
Mar 25, 2009 at 12:19 AM

I think you’re looking for something pretty simple in CLR 4.0, using the common hosting API:

var sr = ScriptRuntime.CreateFromConfiguration();

dynamic random = sr.UseFile(@"random.py");

random.shuffle(...ienumerable...)

It’s still pretty simple now, without the new ‘dynamic’ keyword in C#, you could:

var sr = ScriptRuntime.CreateFromConfiguration();

var random = sr.ExecuteFile("...\random.py");

var ops = sr.GetEngine(“py”).ObjectOperations

ops.Invoke(random.GetVariable(“shuffle”), ... ienumerable...)

If you aren’t trying to host various languages, Python offers a convenience factory to get a runtime with only python in it:

ScriptRuntime py = Python.CreateRuntime();

You can see the hosting APIs on the codeplex project under the “docs and specs” link on the front page.

Bill

From: dvn21 [mailto:notifications@codeplex.com]
Sent: Tuesday, March 24, 2009 12:34 PM
To: Bill Chiles
Subject: Invoke a script funtion? [dlr:51160]

From: dvn21

Hi,
I am kind of a newbie on this. I have a question.
I have a script file written in IronPython containing more than one function. From DLR, is it possible to invoke a named function? I have looked at some functions to create script source
CreateScriptSource,CreateScriptSourceFromFile, CreateScriptSourceFromString and none of them lets me specify function name. Am I missing something?
Please help!

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Mar 25, 2009 at 1:02 AM
Edited Mar 25, 2009 at 1:03 AM
Thanks a lot. Really appreciate it. It is exactly what I am looking for.