[IronPython] How to nest ScriptScopes

Marty Nelson Marty.Nelson at symyx.com
Fri May 9 20:39:05 CEST 2008


Yes, that would be a good logical next step for such code.

 

As far as the concept of the approach, it seems like I have two
approaches for producing both and outer. notation and import of outer
into inner.

 

It seems that functions that I add to outer are returned as variables as
well, which is a nice thing :-)

 

I did notice the "__builtins__" variable, should I avoid copying this?

 

________________________________

From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Seshadri
Pillailokam Vijayaraghavan
Sent: Friday, May 09, 2008 11:16 AM
To: Discussion of IronPython
Subject: Re: [IronPython] How to nest ScriptScopes

 

You can use C# extension methods
<http://msdn.microsoft.com/en-us/library/bb383977.aspx>  to copy the
variables without cluttering up the code too much

 

Here's what the code would look like...

 

    internal static class Extensions{

 

        internal static void SetOuter(this ScriptScope scope,
ScriptScope outerScope) {

            scope.SetVariable("outer", outerScope);

            foreach (string name in outerScope.VariableNames) {

                scope.SetVariable(name, outerScope.GetVariable(name));

            }

        }

 

        internal static ScriptScope GetOuter(this ScriptScope scope) {

            return scope.GetVariable<ScriptScope>("outer");

        }

 

    }

 

You can then invoke it like this : innerScope.SetOuter(outerScope);

      

Hope that helps

sesh

From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Marty Nelson
Sent: Friday, May 09, 2008 10:44 AM
To: Discussion of IronPython
Subject: Re: [IronPython] How to nest ScriptScopes

 

That seems to work, although I used inner to execute (see below).  

 

[TestMethod]

public void TestMethod1()

{

    var outerscope = _env.CreateScope();

    var innerscope = _env.CreateScope();

 

    int outerInput = 3;

    int innerInput = 2;

    outerscope.SetVariable("value", outerInput);

    innerscope.SetVariable("outer", outerscope);

    innerscope.SetVariable("value", innerInput);

 

    _pe.CreateScriptSourceFromString("result = outer.value + value",
SourceCodeKind.Statements)

        .Execute(innerscope);

    var result = innerscope.GetVariable<int>("result");

    Assert.AreEqual(outerInput + innerInput, result);

 

    object ignore;

    Assert.IsFalse(outerscope.TryGetVariable("result", out ignore));

}

 

I'd prefer not to have the dot notation (I know in my sample I had
conflicting variable names, but I don't expect that to be the case.
It's more a layering of available variables) .  Is there some python
function I could write that would go through the outerscope and create
variables at the inner scope using the name?  Or I'm I making this
harder than it needs to be?  See below, where I just copied the
variables.

 

[TestMethod]

public void TestMethod2()

{

    var outerscope = _env.CreateScope();

    var innerscope = _env.CreateScope();

 

    int outerInput = 3;

    int innerInput = 2;

    outerscope.SetVariable("foo", outerInput);

 

    foreach (string name in outerscope.VariableNames)

    {

        innerscope.SetVariable(name, outerscope.GetVariable(name));

    }

    innerscope.SetVariable("bar", innerInput);

 

    _pe.CreateScriptSourceFromString("result = foo + bar",
SourceCodeKind.Statements)

        .Execute(innerscope);

    var result = innerscope.GetVariable<int>("result");

    Assert.AreEqual(outerInput + innerInput, result);

 

    object ignore;

    Assert.IsFalse(outerscope.TryGetVariable("result", out ignore));

}

 

Also, what does "when you have a closure" mean?

 

 

________________________________

From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Friday, May 09, 2008 9:59 AM
To: Discussion of IronPython
Subject: Re: [IronPython] How to nest ScriptScopes

 

It's not really intended that you nest the scopes yourself - usually the
scopes only get nested when you have a closure.  And then we'll burn
references into the parent scopes tuple's directly so we might not even
see nested variables (although in some cases we might due a full runtime
lookup and see them).

 

Instead of nesting them can you have a ScriptScope that contains other
ScriptScope's?  In other words your inner most level is just a
collection of ScriptScope's which you publish under some well known name
into the outer level.  Then users can dot through them:

 

outermost = new ScriptScope();

innermost = new ScriptScope();

innermost.SetVariable("somevalue", 3);

outermost.SetVariable("next", innermost);

 

then run in outermost.

 

 

From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Marty Nelson
Sent: Friday, May 09, 2008 9:52 AM
To: users at lists.ironpython.com
Subject: [IronPython] How to nest ScriptScopes

 

I've gone over the 2.0 B2 source code, but I'm not clear how I might
create nested ScriptScopes using the API in C#

 

One of the tests in EngineTests.cs uses "from published_context_test
import x" but this is only one variable, and I'd rather do it from the
C# setup side anyway.

 

I noticed that Scope, which ScriptScope contains, has the ability to
take a parent Scope, but I'm not clear how I would do this.  It seems
not to be exposed via ScriptScope.

 

We are using IronPython as a script based extension mechanism for our C#
application, my interest is to have a set of established scopes that I
can populate with the variables of current application objects at the
appropriate level for the current script.

 

Thanks,

 

Marty Nelson

=======
Notice: This e-mail message, together with any attachments, contains
information of Symyx Technologies, Inc. or any of its affiliates or
subsidiaries that may be confidential, proprietary, copyrighted,
privileged and/or protected work product, and is meant solely for
the intended recipient. If you are not the intended recipient, and
have received this message in error, please contact the sender
immediately, permanently delete the original and any copies of this
email and any attachments thereto.

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080509/be1928b1/attachment.html>


More information about the Ironpython-users mailing list