[IronPython] Modules in hosted enviroment

Ross Hammermeister glitch17 at gmail.com
Wed Jun 23 12:17:48 PDT 2010


I'm not following what you are saying there. I figure the easiest way for me 
to show what I want to do was give an example. The example shows everything 
I would like to do ignoring exceptions. The function returned from call 
python would be executed when certain events occurred in the program.

The user script is what I would like to have and what I have to do to 
achieve this in C# doesn't really matter but if it could be as simple as 
what I have below it would be nice. I have gotten this to work right now by 
creating and filling out IronPython.Runtime.PythonModule 's dictionary to 
match the definition of each class then assigning it to the engine's 
globals. Even though I have it mostly working it is very unpleasant to deal 
with because I need to fill out everything the dictionary needs and I can't 
overload functions(thanks previous help from here I may be able to resolve 
this now). The code below will work right now if instead I set app to a 
variable in the script instead of a global but I would greatly prefer it to 
use imports.

C# application
class Program
{
                // Other stuff here for program

	Action CallPython()
	{
		App app = new App(this);
		var engine = Python.CreateEngine();
		engine.Runtime.Globals.SetVariable("app", app);
		var scope = engine.CreateScope();
		// execute python script....
		return scope.GetVariable<Action>("event");
	}
}

class Wrapper
{
	public int value { get; set; }
	public Wrapper(int i) { value = i; }
}

class Data
{
	internal Data() { }
	public PythonType Class { get { return 
DynamicHelpers.GetPythonType(typeof(Wrapper)); } }
	public readonly int CONST = 5;
}

class App
{
	private Data _data = new Data();
	private Program _mainApp;

	internal App(object mainApp) { _mainApp = mainApp; }

	public int func() { return _mainApp.Offset; }
	public int func(int i) { return i - _mainApp.Offset; }
	public void action(Wrapper w) { _mainApp.AddItem(w.value); }
	public Data bar { get { return _data; } }
}

user script (python)
from app import func, action
from app.data import CONST, Class

i = func(20)
c = Class(CONST)
c.value = i

def event(a = 5):
    action(c)


Thanks
Ross



--------------------------------------------------
From: "Dino Viehland" <dinov at microsoft.com>
Sent: Wednesday, June 23, 2010 12:35 PM
To: "Discussion of IronPython" <users at lists.ironpython.com>
Subject: Re: [IronPython] Modules in hosted enviroment

> Are you trying to get rid of the "foo.Bar" or do you want the user to
> be typing something like "MyObjectModel.Bar"?  Or are you just wanting the
> user to type "Bar"?  You can just pass the instance of the class in and 
> the
> user can dot through it and call methods on it - as far as they're 
> concerned
> there's not much of a difference between static vs. instance - it's either
> dotting through the class name or the instance name.
>
> If you really want the user to just type "Bar" the good news is that 
> you'll
> be able to do it in a future version.  You'll just need to subclass
> DynamicObject and put a dictionary in it for storing additional 
> attributes.
> Then you can add your methods to it and pass it as the storage for a 
> scope.
> You can also override TryGetMember and then do the lazy lookup of 
> attributes
> as well.
>
> Unfortunately it doesn't work right now in 2.6 due to a bug in IronPython 
> (you
> get an exception saying __doc__ is readonly). It is fixed in the 2.7 
> branch
> though and I can look at backporting it to 2.6.x.  This is the way we 
> really want
> things to work going forward though - scopes are just dynamic objects. 
> That
> will enable you to have thinks like static properties or fields which 
> languages
> can bind to very tightly and get really high performance global variable
> lookups.
>
>> -----Original Message-----
>> From: users-bounces at lists.ironpython.com [mailto:users-
>> bounces at lists.ironpython.com] On Behalf Of Ross Hammermeister
>> Sent: Wednesday, June 23, 2010 9:27 AM
>> To: Discussion of IronPython
>> Subject: Re: [IronPython] Modules in hosted enviroment
>>
>> This is almost what I want but I to pass and instance of a class(no 
>> static
>> methods) and have python treat it as if it were static method. After
>> re-evaluating what I want I can see why it might not be possible. So if 
>> it
>> ends up that I can't do what I described I will use on of the many other
>> methods indicated.
>>
>> Thanks for the help
>>
>> --------------------------------------------------
>> From: "Dino Viehland" <dinov at microsoft.com>
>> Sent: Tuesday, June 22, 2010 6:56 PM
>> To: "Discussion of IronPython" <users at lists.ironpython.com>
>> Subject: Re: [IronPython] Modules in hosted enviroment
>>
>> > Jeff wrote:
>> >> Hi Ross,
>> >> It sounds like you want Dino's suggestion IAttributesCollection, which
>> >> will give you much more flexibility. I've never done that, but it
>> >> looks fairly straightforward.
>> >>
>> >> > An unrelated issue I have with ScriptScope is I can't figure out to 
>> >> > add
>> >> > overloaded functions with different parameter counts for defaults 
>> >> > I'm
>> >> > using
>> >> > 3.5 if it matters.
>> >>
>> >> Dino?
>> >
>> > I assume what you have here is a bunch of static methods declared
>> > somewhere and
>> > you want to publish them in the scope?  The only way to really go about
>> > doing
>> > this is getting the BuiltinFunction object for them and then publishing
>> > that
>> > in the scope.  To get the BF you'll need to first get the PythonType 
>> > and
>> > then
>> > get the BF.  Such as:
>> >
>> > public class MyObjectModel {
>> > public static void Foo() {
>> > }
>> >
>> > public static void Foo(object x) {
>> > }
>> > }
>> >
>> > Using C# 4.0 then I think you could do:
>> >
>> > dynamic foos =
>> > ((dynamic)DynamicHelpers.GetPythonTypeFromType(typeof(MyObjectModel))).Foo;
>> >
>> > or you could do:
>> >
>> > object foos =
>> >
>> engine.Operations.GetMember(DynamicHelpers.GetPythonTypeFromType(typeof(MyObje
>> ctModel))),
>> > "Foo");
>> > _______________________________________________
>> > Users mailing list
>> > Users at lists.ironpython.com
>> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>> >
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> 


More information about the Users mailing list