使用 JSR-223 从 Jython 脚本获取数据

发布于 2024-08-14 13:25:36 字数 672 浏览 6 评论 0原文

我正在使用 Jython 2.5.1 和 JSR-223(即 javax.script 包),并且我希望返回 Python 脚本的最后一行。例如,在评估此脚本后:

class Multiplier:

  def multiply(self, x, y):
    return x * y

Multiplier().multiply(5, 7)

我应该返回 35,但我得到的是 null。另一方面,它与其他测试一起使用:

5 * 7

我做错了什么?

下面是 Java 代码:

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

    FileReader f = new FileReader("Multiplier.py");
    Object result = engine.eval(f);
    //assert(result == 35);
}

PS:它在 JRuby、Groovy 和 Rhino 中工作得很好,即总是返回最后一行。

提前致谢。

I am using Jython 2.5.1 with JSR-223 (i.e. javax.script package) and I expect the last line of the Python script to be returned. For example, after evaluating this script:

class Multiplier:

  def multiply(self, x, y):
    return x * y

Multiplier().multiply(5, 7)

I should get back 35, but I get null instead. In other hand it works with this other test:

5 * 7

What am I doing wrong?

Here's the Java code:

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

    FileReader f = new FileReader("Multiplier.py");
    Object result = engine.eval(f);
    //assert(result == 35);
}

PS: It works fine with JRuby, Groovy and Rhino, i.e. the last line is always returned.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

哑剧 2024-08-21 13:25:36

更新:实际上,我在最初的答案中错过了OP的目标(和问题),这一点已在评论中得到澄清。我正在相应地更新我的答案。

首先更新 Multiplier.py 脚本,如下所示:

class Multiplier:

  def multiply(self, x, y):
    return x * y

x = Multiplier().multiply(5, 7)

然后从 Java 代码中这样调用它:

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

    FileReader f = new FileReader("Multiplier.py");
    engine.eval(f);
    Object x = engine.get("x");
    System.out.println("x: " + x);
}

运行上面的代码时,我得到以下输出:

x: 35

UPDATE: I was actually missing the goal (and problem) of the OP in my initial answer that has been clarified in a comment. I'm updating my answer accordingly.

First update the Multiplier.py script as below:

class Multiplier:

  def multiply(self, x, y):
    return x * y

x = Multiplier().multiply(5, 7)

Then call it like this from the Java code:

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

    FileReader f = new FileReader("Multiplier.py");
    engine.eval(f);
    Object x = engine.get("x");
    System.out.println("x: " + x);
}

I get the following output when running the code above:

x: 35
爱,才寂寞 2024-08-21 13:25:36

这是一个 Python 语言问题,而不是 Jython 或 JSR 223 问题。 Python 区分表达式(有值)和语句(没有值)。您传递的脚本是一个声明。如果您传递一个表达式,它就会有一个值。

您在 Ruby 和 JavaScript 中看到不同的原因是复合语句具有最后评估的语句的值。例如,将 Ruby

>> (2 ; 3) + 5
=> 8
>> (x = 5) + 7
=> 12

与 Python 进行比较:

>>> (2 ; 3) + 5
  File "<stdin>", line 1
    (2 ; 3) + 5
       ^
SyntaxError: invalid syntax
>>> (x = 5) + 7
  File "<stdin>", line 1
    (x = 5) + 7
       ^
SyntaxError: invalid syntax

JavaScript 似乎介于两者之间。与 Ruby 一样,赋值的计算结果是分配的值。但是,返回块中最后一个计算的语句,但不能用作表达式的一部分:

> { 2 ; 3 }
3
> { 2 ; 3 } + 5
5
> (x = 5) + 7
12

This is a Python language issue more than a Jython or JSR 223 issue. Python differentiates between expressions (which have values) and statements (which don't). The script you're passing is a statement. If you passed an expression, it'd have a value.

The reason you're seeing something different with Ruby and JavaScript is that compound statements have the value of the last statement evaluated. For example, compare Ruby:

>> (2 ; 3) + 5
=> 8
>> (x = 5) + 7
=> 12

with Python:

>>> (2 ; 3) + 5
  File "<stdin>", line 1
    (2 ; 3) + 5
       ^
SyntaxError: invalid syntax
>>> (x = 5) + 7
  File "<stdin>", line 1
    (x = 5) + 7
       ^
SyntaxError: invalid syntax

JavaScript seems to be somewhere in between. Like Ruby, assignments evaluate to the value assigned. However, the last evaluated statement in a block is returned but not usable as part of an expression:

> { 2 ; 3 }
3
> { 2 ; 3 } + 5
5
> (x = 5) + 7
12
伴梦长久 2024-08-21 13:25:36

我也有同样的问题。我编写了一个支持 JSR223 的脚本库并提交了 http://bugs.jython.org/issue1798

$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x=5<br/>
>>> x<br/>
5
>>>

至少如果您 eval("x=5\nx") ,您似乎会得到该值。

I had the same problem. I have written a scripting library that supports JSR223 and have submited http://bugs.jython.org/issue1798:

$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x=5<br/>
>>> x<br/>
5
>>>

At least if you eval("x=5\nx") it seems like you would get the value back.

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