File: test_embed.java

package info (click to toggle)
jython 2.1.0-20
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,388 kB
  • ctags: 12,215
  • sloc: java: 48,499; python: 16,220; xml: 403; makefile: 189; perl: 103; ansic: 24; sh: 14
file content (29 lines) | stat: -rw-r--r-- 866 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.python.core.*;
import org.python.util.PythonInterpreter;

public class embed {
    public static void main(String[] args) {
        // Create a new Python Interpreter
        PythonInterpreter interp = new PythonInterpreter();

        // Add a variable to its namespace
        interp.set("x", new PyInteger(2));

        String expr = "x+5";

        // Evaluate some arbitrary strings
        System.out.println(expr+" = "+interp.eval(expr));     
        interp.exec("y = 'foo'");

        // Get a string back from the namespace
        Object y = Py.tojava(interp.get("y"), Object.class);

        System.out.println("y = "+y);
        
        // Create an instance of a Python defined class (FooRunner.py)
        Runnable foo = new FooRunner();
        
        // Call a method on this class as if it was a Java object
        foo.run();
    }
}