IronPython DLR;将参数传递给编译的代码?

发布于 2024-10-15 06:20:21 字数 1250 浏览 6 评论 0原文

我目前正在执行以下操作,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不顾 2024-10-22 06:20:21

您可以在 Python 中计算表达式并返回其结果 (1),也可以将值赋给作用域中的某个变量,然后再选择它 (2):

    var py = Python.CreateEngine();

    // 1
    var value = py.Execute("1+1");
    Console.WriteLine(value);

    // 2
    var scriptScope = py.CreateScope();
    py.Execute("a = 1 + 1", scriptScope);
    var value2 = scriptScope.GetVariable("a");
    Console.WriteLine(value2);

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):

    var py = Python.CreateEngine();

    // 1
    var value = py.Execute("1+1");
    Console.WriteLine(value);

    // 2
    var scriptScope = py.CreateScope();
    py.Execute("a = 1 + 1", scriptScope);
    var value2 = scriptScope.GetVariable("a");
    Console.WriteLine(value2);
oО清风挽发oО 2024-10-22 06:20:21

您绝对不必打印它。我期望有一种只评估表达式的方法,但如果没有,还有其他选择。

例如,在我很久以前编写的动态图形演示中(不幸的是不再有代码),我使用 python: 创建了一个函数,

def f(x):
    return x * x

然后将 f 移出了脚本范围,例如this:

Func<double, double> function;
if (!scope.TryGetVariable<Func<double, double>>("f", out function))
{
    // Error handling here
}
double step = (maxInputX - minInputX) / 100;
for (int i = 0; i < 101; i++)
{
    values[i] = function(minInputX + step * i);
}

如果您想多次计算表达式,则可以执行类似的操作,或者如果您只需要计算一次,则只需将结果分配给变量。

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:

def f(x):
    return x * x

and then got f out of the script scope like this:

Func<double, double> function;
if (!scope.TryGetVariable<Func<double, double>>("f", out function))
{
    // Error handling here
}
double step = (maxInputX - minInputX) / 100;
for (int i = 0; i < 101; i++)
{
    values[i] = function(minInputX + step * i);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文