IronPython的DLR;将参数传递给编译code? [英] IronPython DLR; passing parameters to compiled code?

查看:493
本文介绍了IronPython的DLR;将参数传递给编译code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在做下面的创建和执行一个简单的Python计算,采用DLR:

  ScriptRuntime运行时间= Python.CreateRuntime();
的ScriptEngine引擎= runtime.GetEngine(PY);

MemoryStream的毫秒=新的MemoryStream();
runtime.IO.SetOutput(MS,新的StreamWriter(MS));

ScriptSource SS = engine.CreateScriptSourceFromString(打印1 + 1,源$ C ​​$ cKind.Interactive code);

编译code CC = ss.Compile();
cc.Execute();

INT长度=(INT)ms.Length;
字节[]字节=新字节[长度];
ms.Seek(0,SeekOrigin.Begin);
ms.Read(字节,0,(int)的ms.Length);
。字符串结果= Encoding.GetEncoding(UTF-8)的GetString(字节,0,(int)的ms.Length);

Console.WriteLine(结果);
 

它打印2到控制台,但;

我想获得的1 + 1的结果,而不必进行打印(如,这似乎是一个昂贵的操作)。什么我给你cc.Execute的结果()来为空。有没有其他方法可以让我得到执行(结果变量)?

我也试图找到一种方法来传递参数,即这样的结果是ARG1 + ARG2,而且不知道该怎么做;唯一的其他重载执行需要ScriptScope作为参数,我从来没有使用过蟒蛇。任何人都可以帮忙吗?

回答这两个问题:(德斯科的接受为我指出了正确的方向)

 的ScriptEngine PY = Python.CreateEngine();
ScriptScope PYS = py.CreateScope();

ScriptSource SRC = py.CreateScriptSourceFromString(A + B);
编译= src.Compile编译code();

pys.SetVariable(一,1);
pys.SetVariable(b的,1);
VAR的结果= compiled.Execute(PYS);

Console.WriteLine(结果);
 

解决方案

您可以评估EX pression Python和返回其结果(1)或范围将值分配给某个变量,后来捡起来(2)

  VAR PY = Python.CreateEngine();

    // 1
    变种值= py.Execute(1 + 1);
    Console.WriteLine(值);

    // 2
    变种scriptScope = py.CreateScope();
    py.Execute(一个= 1 + 1,scriptScope);
    变种值2 = scriptScope.GetVariable(一);
    Console.WriteLine(值2);
 

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

解决方案

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

这篇关于IronPython的DLR;将参数传递给编译code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆