[Ironpython-users] Separating return values from printed values?
Doug Blank
doug.blank at gmail.com
Thu Sep 20 12:44:24 CEST 2012
On Thu, Sep 20, 2012 at 1:37 AM, Keith Rome <rome at wintellect.com> wrote:
> This might give you what you need (hooking in via the runtime output writer, which is where the print function routes to), it seems to work for my quick test:
>
> using System;
> using System.IO;
> using System.Text;
> using IronPython.Hosting;
>
> namespace ConsoleApplication3
> {
> class Program
> {
> static void Main(string[] args)
> {
> RunScript("22");
> RunScript("print(23)");
> Console.ReadKey();
> }
>
> static void RunScript(string script)
> {
> var engine = Python.CreateEngine();
> var io = engine.Runtime.IO;
> io.SetOutput(io.OutputStream, new GreenWriter(io.OutputWriter));
> var result = engine.Execute(script);
> if (result != null)
> Console.WriteLine(result);
> }
> }
>
> class GreenWriter : TextWriter
> {
> private readonly TextWriter _inner;
> public GreenWriter(TextWriter inner)
> {
> _inner = inner;
> }
> public override void Write(string value)
> {
> var original = Console.ForegroundColor;
> Console.ForegroundColor = ConsoleColor.Green;
> _inner.Write(value);
> Console.ForegroundColor = original;
> }
> public override Encoding Encoding
> {
> get { return _inner.Encoding; }
> }
> }
> }
>
Thanks for the example! I guess we also have some other issues which
may cause use to be unable to use that interface, or maybe our code is
overly complicated? We:
* use a shared scope, if available so other DLR languages can
interoperate (manager.scope below)
* compile the code, perhaps with options (for speed and feedback, I guess)
* make a distinction between different SourceCodeKinds (for the
compiler, I guess)
* thus, we don't have a return value; the system automatically prints it (?)
* we try/catch each step to give feedback to the student
Here is our Execute:
sctype = Microsoft.Scripting.SourceCodeKind.InteractiveCode;
source = engine.CreateScriptSourceFromString(text, sctype);
try {
if (compiler_options != null) {
source.Compile(compiler_options);
} else {
source.Compile();
}
} catch {
sctype = Microsoft.Scripting.SourceCodeKind.Statements;
source = engine.CreateScriptSourceFromString(text, sctype);
try {
if (compiler_options != null) {
source.Compile(compiler_options);
} else {
source.Compile();
}
} catch (Exception e) {
eo =
engine.GetService<Microsoft.Scripting.Hosting.ExceptionOperations>();
PrintLine(eo.FormatException(e));
return false;
}
}
try {
if (manager != null && manager.UseSharedScope)
source.Execute(manager.scope);
else
source.Execute(scope);
} catch (Exception e) {
if (e.Message.Contains("Thread was being aborted")) {
manager.stderr.Print("[Script stopped----------]\n");
} else {
eo =
engine.GetService<Microsoft.Scripting.Hosting.ExceptionOperations>();
PrintLine(eo.FormatException(e));
}
return false;
}
if (manager.stderr != null)
PrintLine(Tag.Info, "Ok");
Is there a way using this interface, or is our code too much, and we
could get by with less?
-Doug
> Keith Rome
> Senior Consultant and Architect
> MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS
> Wintellect | 770.617.4016 | krome at wintellect.com
> www.wintellect.com
>
> -----Original Message-----
> From: Ironpython-users [mailto:ironpython-users-bounces+rome=wintellect.com at python.org] On Behalf Of Doug Blank
> Sent: Thursday, September 20, 2012 12:43 AM
> To: ironpython-users at python.org
> Subject: [Ironpython-users] Separating return values from printed values?
>
> IronPython users,
>
> Interesting question: we have IronPython embedded in a nice little educational GUI, and have the DLR output sent to our text output window where we can control the font color, etc.
>
> In teaching CS, it is always hard to try to get across the difference between a "return value" and a "displayed string". For example:
>
>>>> 22
> 22
>>>> print(23)
> 23
>>>> function()
> 22
>
> where 22 is just a value, and 23 is a displayed string (a side-effect for you functional types). These GUIs don't make it easy to see the difference.
>
> One thing we've thought about is making a distinction between them with color, such that 22 might be blue, and 23 would be green.
>
> Can you think of an easy way to do that with IronPython and the DLR?
> Anything we could tap into to send evaluations one way, and displayed values another?
>
> Thanks for any suggestions,
>
> -Doug
> _______________________________________________
> Ironpython-users mailing list
> Ironpython-users at python.org
> http://mail.python.org/mailman/listinfo/ironpython-users
>
>
More information about the Ironpython-users
mailing list