Read multiple files .py-files into single scriptScope?

Jun 8, 2010 at 7:33 PM

A couple of days ago I was pointed to an example that showed how to use the dynamic keyword to execute code from a python file. Very nice feature. This was for Silverlight (I don't know if it differs from other .NET).

Below is the sample code:

            var runtime = Silverlight.DynamicEngine.CreateRuntime();
            var python = runtime.GetEngine("python");
            dynamic script = runtime.UseFile("script.py");
            
            int p = script.factorial(5);
            tbkResult.Text = p.ToString();

Now I wonder if there is a way to read more than a single file into a scriptscope? We have a directory with several .py-files and some of them are dependent of others so we need to read all of them into the same scope. Can this be done? If so, how?

Thanks for help!

Jun 8, 2010 at 7:39 PM

Yep, you can create an empty scope and then run files against it:

dynamic script = python.CreateScope();

python.ExecuteFile("script1.py", script);

python.ExecuteFile("script2.py", script);

# script now has vars set by script1 and script2

~js

From: xkrja [mailto:notifications@codeplex.com]
Sent: Tuesday, June 08, 2010 12:34 PM
To: Jimmy Schementi
Subject: Read multiple files .py-files into single scriptScope? [dlr:215374]

From: xkrja

A couple of days ago I was pointed to an example that showed how to use the dynamic keyword to execute code from a python file. Very nice feature. This was for Silverlight (I don't know if it differs from other .NET).

Below is the sample code:

var runtime = Silverlight.DynamicEngine.CreateRuntime();
var python = runtime.GetEngine("python");
dynamic script = runtime.UseFile("script.py");

int p = script.factorial(5);
tbkResult.Text = p.ToString();

Now I wonder if there is a way to read more than a single file into a scriptscope? We have a directory with several .py-files and some of them are dependent of others so we need to read all of them into the same scope. Can this be done? If so, how?

Thanks for 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
Jun 8, 2010 at 8:30 PM

I'm sure you know what you're doing, but to make sure you consider it, this solution will load all the files into the same scope. Think of this as if you pasted all the contents of the .py files into a single file, then executed it. Any globals with the same names will conflict. Any code doing "import foo; foo.blah" will not work because there won't be a "foo" module in the ScriptRuntime. If you UseFile a file that imports other files, we will import them as you'd expect running python code. This might be more of what you want. You can also use ExecuteFile on each file name, but again, you know what you want, and maybe you do want all the files in one scope (or python module).

Cheers,

Bill

From: jimmysch [mailto:notifications@codeplex.com]
Sent: Tuesday, June 08, 2010 12:40 PM
To: Bill Chiles
Subject: Re: Read multiple files .py-files into single scriptScope? [dlr:215374]

From: jimmysch

Yep, you can create an empty scope and then run files against it:

dynamic script = python.CreateScope();

python.ExecuteFile("script1.py", script);

python.ExecuteFile("script2.py", script);

# script now has vars set by script1 and script2

~js

From: xkrja [mailto:notifications@codeplex.com]
Sent: Tuesday, June 08, 2010 12:34 PM
To: Jimmy Schementi
Subject: Read multiple files .py-files into single scriptScope? [dlr:215374]

From: xkrja

A couple of days ago I was pointed to an example that showed how to use the dynamic keyword to execute code from a python file. Very nice feature. This was for Silverlight (I don't know if it differs from other .NET).

Below is the sample code:

var runtime = Silverlight.DynamicEngine.CreateRuntime();
var python = runtime.GetEngine("python");
dynamic script = runtime.UseFile("script.py");

int p = script.factorial(5);
tbkResult.Text = p.ToString();

Now I wonder if there is a way to read more than a single file into a scriptscope? We have a directory with several .py-files and some of them are dependent of others so we need to read all of them into the same scope. Can this be done? If so, how?

Thanks for 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

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

Jun 9, 2010 at 7:00 AM

Thanks for your replies guys!

And billchi: I'm not sure that I know what I'm doing :-) Most of this is news to me.

But I guess that "UseFile" is exactly what I'm after.

Thanks again.