[IronPython] Can I pre-add namespaces?

Dino Viehland dinov at microsoft.com
Fri Oct 10 23:46:53 CEST 2008


I was thinking more of just runtime.Globals.TryGetVariable() and then injecting the object you get there into the scope you'll run the code against.  >From x import * will tend to pollute your namespace and therefore isn't that great (it also makes it harder when reading the code to know where your imports came from).

So something like:
                if(runtime.Globals.TryGetVariable("System", out value)) {
                                scope.SetVariable("System", value);
                }

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Marty Nelson
Sent: Friday, October 10, 2008 2:40 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Can I pre-add namespaces?

Except when I start including "from x import *" type statements, I start getting hundreds and hundres of variables.

The following doesn't seem to work though.  I am probably not understanding runtime.Globals:

    ScriptRuntime runtime = Python.CreateRuntime();
    runtime.LoadAssembly(typeof(string).Assembly);
    runtime.LoadAssembly(typeof(Uri).Assembly);

    ScriptEngine engine = runtime.GetEngine("py");

    string imports = @"from System import *";

    engine.CreateScriptSourceFromString(imports,
        SourceCodeKind.Statements)
        .Execute(runtime.Globals);

    string script = @"myDate = DateTime.Now";

    ScriptScope scope = runtime.CreateScope();
    ScriptSource scriptSource = engine.CreateScriptSourceFromString(
        script, SourceCodeKind.Statements);
    scriptSource.Execute(scope);


________________________________
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Friday, October 10, 2008 2:25 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Can I pre-add namespaces?

That seems like a good solution, the perf should be fine.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Marty Nelson
Sent: Friday, October 10, 2008 1:38 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Can I pre-add namespaces?

Maybe it would it be easier just to append it to the text prior to execution.  It didn't appear to have a side effect.  Any performance considerations?

ScriptSource scriptSource = engine.CreateScriptSourceFromString(
"import System\r\n" + script, SourceCodeKind.Statements);
scriptSource.Execute(scope);


________________________________
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Friday, October 10, 2008 1:12 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Can I pre-add namespaces?

I think if you're not replacing Globals on the script runtime you can fetch the namespace from the ScriptRuntime.Globals scope.

If that doesn't work for some reason you can always new up a TopNamespaceTracker, load assemblies into it, and get the namespace trackers from it.  You'll need to get the the ScriptDomainManager object which you can fetch w/ the HostingHelpers.GetDomainManager function.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Marty Nelson
Sent: Friday, October 10, 2008 1:02 PM
To: users at lists.ironpython.com
Subject: [IronPython] Can I pre-add namespaces?

I know I can load assemblies into the script runtime, but how can I add the imports into the scope programmatically?  I'm trying to avoid forcing people to write things like "imports System" for scripts.

Microsoft.Scripting.Actions.NamespaceTracker seems to be involved, but it isn't there till after the scope executes for the first time and it has no public contructor.

Thanks,

- Marty
=======
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/20081010/4b392604/attachment.html>


More information about the Ironpython-users mailing list