I /think/ it may be working. At least my unit tests are working. The code below runs the unit test, the unit test is at the bottom.

       public DynamicAgent(ScriptEngine aScriptEngine)
       {
           _engine = aScriptEngine;
           _engine.Runtime.GetClrModule();
           _scope = _engine.CreateScope();
}

       public virtual object PrepareString(string anExpression)
       {
return _engine.CreateScriptSourceFromString(anExpression, SourceCodeKind.Expression);
       }

       public virtual object Evaluate(ScriptSource source)
       {
           return source.Execute(_scope);
       }

       public virtual object EvaluateString(string anExpression)
       {
           var source = PrepareString(anExpression);
           return Evaluate(source);
       }
----

       [TestMethod]
       public void pythonToStringTest()
       {
           var agent = new PythonAgent();
           agent.SetVariable("aMoney", 1234.56M);
var result = agent.EvaluateString("aMoney.ToString('#,##0.00')");
           Assert.AreEqual("1,234.56", result);
       }

I'm not convinced if it's working because I'm unsure what GetClrModule() precisely does or how many scopes there can be, or if there's really only one scope and everything is additive, or...

Dino Viehland wrote:
Because this is on a per-module basis calling GetClrModule() won't affect the 
module you later want to do .ToString from.

I think the easiest way to do this would be to do:

var code = Engine.CreateScriptSourceFromString("import clr", 
SourceCodeKind.Statements).Compile();

and whenever you want a module to use import clr do:

code.Execute(_scope);

If you open a bug on CodePlex though we could look at adding an extension method like 
"scope.EnableClr()" or something along those lines that makes this simpler.



-----Original Message-----
From: users-boun...@lists.ironpython.com 
[mailto:users-boun...@lists.ironpython.com] On Behalf Of Thomas Gagne
Sent: Monday, September 21, 2009 12:00 PM
To: users@lists.ironpython.com
Subject: [IronPython] How to get ToString() working, and questions about 
GetSysModule() and GetClrModule()

We've hosted Python inside our C# application and all seems to work well. Recently, one of our programmers has asked to use ToString inside a python expression. I read online we need to import the "clr" to make that happen.

The code for kicking it off looks like:

_engine = Python.CreateEngine();
_scope = _engine.CreateScope();

I'm curious, if I want the Clr already-imported to a program can ask for a fixed-point's ToString("C"), am I supposed to be able to use the GetClrModule()?

Since both it and CreateScope() return scopes I thought I'd try using the scope returned by GetClrModule() to evaluate an expression. Suddenly, "True" no longer evaluated. It had no binding.

Signed confused.
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to