IronPython DLR;将参数传递给编译的代码?
我目前正在执行以下操作,使用 DLR 创建并执行一个简单的 python 计算:
ScriptRuntime runtime = Python.CreateRuntime();
ScriptEngine engine = runtime.GetEngine("py");
MemoryStream ms = new MemoryStream();
runtime.IO.SetOutput(ms, new StreamWriter(ms));
ScriptSource ss = engine.CreateScriptSourceFromString("print 1+1", SourceCodeKind.InteractiveCode);
CompiledCode cc = ss.Compile();
cc.Execute();
int length = (int)ms.Length;
Byte[] bytes = new Byte[length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(bytes, 0, (int)ms.Length);
string result = Encoding.GetEncoding("utf-8").GetString(bytes, 0, (int)ms.Length);
Console.WriteLine(result);
它将“2”打印到控制台,但是;
我想获得 1 + 1 的结果而不必打印它(因为这似乎是一个昂贵的操作)。我将 cc.Execute() 的结果分配给的任何内容都是 null。有没有其他方法可以从 Execute() 获取结果变量?
我也在尝试找到一种传递参数的方法,即结果是 arg1 + arg2 并且不知道如何做到这一点; Execute 的唯一其他重载将 ScriptScope 作为参数,而且我以前从未使用过 python。有人可以帮忙吗?
[编辑] 这两个问题的答案:(德斯科被认为为我指明了正确的方向)
ScriptEngine py = Python.CreateEngine();
ScriptScope pys = py.CreateScope();
ScriptSource src = py.CreateScriptSourceFromString("a+b");
CompiledCode compiled = src.Compile();
pys.SetVariable("a", 1);
pys.SetVariable("b", 1);
var result = compiled.Execute(pys);
Console.WriteLine(result);
I'm currently doing the following to create and execute a simple python calculation, using DLR:
ScriptRuntime runtime = Python.CreateRuntime();
ScriptEngine engine = runtime.GetEngine("py");
MemoryStream ms = new MemoryStream();
runtime.IO.SetOutput(ms, new StreamWriter(ms));
ScriptSource ss = engine.CreateScriptSourceFromString("print 1+1", SourceCodeKind.InteractiveCode);
CompiledCode cc = ss.Compile();
cc.Execute();
int length = (int)ms.Length;
Byte[] bytes = new Byte[length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(bytes, 0, (int)ms.Length);
string result = Encoding.GetEncoding("utf-8").GetString(bytes, 0, (int)ms.Length);
Console.WriteLine(result);
Which prints '2' to the console, but;
I want to get the result of 1 + 1 without having to print it (as that seems to be a costly operation). Anything I assign the result of cc.Execute() to is null. Is there any other way I can get the resulting variables from Execute()?
I am also trying to find a way to pass in parameters, i.e. so the result is arg1 + arg2 and have no idea how to do that; the only other overload for Execute takes ScriptScope as a parameter, and I've never used python before. Can anyone help?
[Edit]
The answer to both questions: (Desco's accepted as pointed me in the right direction)
ScriptEngine py = Python.CreateEngine();
ScriptScope pys = py.CreateScope();
ScriptSource src = py.CreateScriptSourceFromString("a+b");
CompiledCode compiled = src.Compile();
pys.SetVariable("a", 1);
pys.SetVariable("b", 1);
var result = compiled.Execute(pys);
Console.WriteLine(result);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 Python 中计算表达式并返回其结果 (1),也可以将值赋给作用域中的某个变量,然后再选择它 (2):
You can either evaluate expression in Python and return its result (1) or assign value to some variable in scope and later pick it (2):
您绝对不必打印它。我期望有一种只评估表达式的方法,但如果没有,还有其他选择。
例如,在我很久以前编写的动态图形演示中(不幸的是不再有代码),我使用 python: 创建了一个函数,
然后将
f
移出了脚本范围,例如this:如果您想多次计算表达式,则可以执行类似的操作,或者如果您只需要计算一次,则只需将结果分配给变量。
You definitely don't have to print it. I'd expect there to be a way of just evaluating an expression, but if not there are alternatives.
For example, in a dynamic graphing demo I wrote a long time ago (and unfortunately no longer have the code for) I created a function, using the python:
and then got
f
out of the script scope like this:You could do something similar if you want to evaluate the expression multiple times, or just assign the result to a variable if you only need to evaluate it once.