How to place static class in script name space

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to place static class in script name space

Tom Unger-2
Message
I am using IronPython for scripting an application.  To make writing scripts easier I want to place several objects in the script name space so they can be easily used.  I got stuck with how to place a reference to a static object, "App", in the name space.  This is probably a simple operation and just don't know what it is.
 
1. Python import works:
 
            from AppFrame import App
 
gives the script access to the static fields and methods on App.
 
But I want to eliminate the need for each script to do that import by setting up the scope with:
 
   scriptScope.SetVariable("App", App);
This does not compile because App is a static class, not an object. 
 
How does my hosing application place a reference to the static class in the script's scope such that it can be used as if it was imported?
 
Thanks
 
Tom
Seattle
 

_______________________________________________
Users mailing list
[hidden email]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to place static class in script name space

Dino Viehland
Message

We specifically allow from staticclass import * because we treat a static class as if it were like a module.  So you could simply run that line of code in the scope on behalf of the user.  Otherwise you’ll need to re-create getting the dynamic members and then setting them in the scope.  Then you can use PythonOps.GetAttrNames to get the defined names and finally PythonOps.GetBoundAttr to get the actual members which you’ll then set in the scope.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Tom Unger
Sent: Monday, June 13, 2011 10:16 AM
To: '[hidden email]'
Subject: [IronPython] How to place static class in script name space

 

I am using IronPython for scripting an application.  To make writing scripts easier I want to place several objects in the script name space so they can be easily used.  I got stuck with how to place a reference to a static object, "App", in the name space.  This is probably a simple operation and just don't know what it is.

 

1. Python import works:

 

            from AppFrame import App

 

gives the script access to the static fields and methods on App.

 

But I want to eliminate the need for each script to do that import by setting up the scope with:

 

   scriptScope.SetVariable("App", App);

This does not compile because App is a static class, not an object. 

 

How does my hosing application place a reference to the static class in the script's scope such that it can be used as if it was imported?

 

Thanks

 

Tom

Seattle

 


_______________________________________________
Users mailing list
[hidden email]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to place static class in script name space

Keith Rome
Message

To pre-load various module imports into our execution environment, I am doing something like this at application startup (we have a number of modules that we pre-load):

 

    dynamic builtin = Engine.GetBuiltinModule();

    dynamic import = builtin.__import__;

    var DefaultImports = new string[] { “math”, “clr”, “sys”, “string”, “cmath”, “re” /*etc…*/ };

    CachedModules = new Dictionary<string, PythonModule>();

    foreach (var mod in DefaultImports)

        CachedModules.Add(mod, import(mod));

 

And then later on, just before executing a new script each time, I create a new empty scope using Engine.CreateScope() and then I inject all of the cached modules that were previously imported:

 

    foreach (var mod in CachedModules)

        LocalScope.SetVariable(mod.Key, mod.Value);

    PreCompiled.Execute(LocalScope);

 

This allows each script invocation to reside in its own somewhat isolated sandbox, while only importing those commonly used modules once.

 

You should be able to do something similar to the above, using the builtin __import__() function. From looking over the IronPython source, it looks like you should be able to call it like this (I haven’t tested it in this way, but I believe it should work):

 

    dynamic AppModule = import(“App”, null, null, “AppFrame”);

 

This should effectively mimic exactly what happens when you run the statement “from AppFrame import App”.

 

 

Many Bothan spies have died in bringing you this information.

 

 

 

Keith Rome

Senior Consultant and Architect

MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS

Wintellect | 770.617.4016 | [hidden email]

www.wintellect.com

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Dino Viehland
Sent: Monday, June 13, 2011 1:22 PM
To: Discussion of IronPython
Subject: Re: [IronPython] How to place static class in script name space

 

We specifically allow from staticclass import * because we treat a static class as if it were like a module.  So you could simply run that line of code in the scope on behalf of the user.  Otherwise you’ll need to re-create getting the dynamic members and then setting them in the scope.  Then you can use PythonOps.GetAttrNames to get the defined names and finally PythonOps.GetBoundAttr to get the actual members which you’ll then set in the scope.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Tom Unger
Sent: Monday, June 13, 2011 10:16 AM
To: '[hidden email]'
Subject: [IronPython] How to place static class in script name space

 

I am using IronPython for scripting an application.  To make writing scripts easier I want to place several objects in the script name space so they can be easily used.  I got stuck with how to place a reference to a static object, "App", in the name space.  This is probably a simple operation and just don't know what it is.

 

1. Python import works:

 

            from AppFrame import App

 

gives the script access to the static fields and methods on App.

 

But I want to eliminate the need for each script to do that import by setting up the scope with:

 

   scriptScope.SetVariable("App", App);

This does not compile because App is a static class, not an object. 

 

How does my hosing application place a reference to the static class in the script's scope such that it can be used as if it was imported?

 

Thanks

 

Tom

Seattle

 


_______________________________________________
Users mailing list
[hidden email]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to place static class in script name space

Markus Schaber-6
In reply to this post by Tom Unger-2
Hi, Tom,

Von: Tom Unger

> But I want to eliminate the need for each script to do that import by setting up the scope with:
> scriptScope.SetVariable("App", App);
> This does not compile because App is a static class, not an object.  

Maybe scriptScope.SetVariable("App", DynamicHelpers.GetPythonTypeFromType(typeof(App))) can help here?

The C# typeof() operator returns the .NET type object (the same as you get from GetType() of an instance), and the DynamicHelpers.GetPythonTypeFromType() method returns the corresponding python type object for that .NET type.

I successfully used it to expose types (constructors, static members) to IronPython 2.6 scripts.

(DynamicHelpers is defined in the IronPython.Runtime.Types namespace.)

HTH,
Markus

 
_______________________________________________
Users mailing list
[hidden email]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Loading...