L
Link
Hello!I am new to Python and I would like to rewrite the following code to
python use to call by C program:// JPython code
=======================================
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class SimpleEmbedded {
public static void main(String []args)
throws PyException
{
PythonInterpreter interp =
new PythonInterpreter();
System.out.println("Hello, brave new world");
interp.exec("import sys");
interp.exec("print sys");
interp.set("a", new PyInteger(42));
interp.exec("print a");
interp.exec("x = 2+2");
PyObject x = interp.get("x");
System.out.println("x: "+x);
System.out.println("Goodbye, cruel world");
}
}
// C Language code ==============================
void main() {
Py_Initialize();
printf("Hello, brave new world");
PyRun_SimpleString("import sys"); PyRun_SimpleString("print sys");
// interp.set("a", new PyInteger(42));
// I can't rewrite,Please Help!
PyRun_SimpleString("print a"); PyRun_SimpleString("x = 2+2");
// PyObject x = interp.get("x");
// I can't rewrite,Please Help!
// System.out.println("x: "+x);
// I can't rewrite,Please Help!
Py_Finalize();
printf("Goodbye, cruel world");
}
python use to call by C program:// JPython code
=======================================
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class SimpleEmbedded {
public static void main(String []args)
throws PyException
{
PythonInterpreter interp =
new PythonInterpreter();
System.out.println("Hello, brave new world");
interp.exec("import sys");
interp.exec("print sys");
interp.set("a", new PyInteger(42));
interp.exec("print a");
interp.exec("x = 2+2");
PyObject x = interp.get("x");
System.out.println("x: "+x);
System.out.println("Goodbye, cruel world");
}
}
// C Language code ==============================
void main() {
Py_Initialize();
printf("Hello, brave new world");
PyRun_SimpleString("import sys"); PyRun_SimpleString("print sys");
// interp.set("a", new PyInteger(42));
// I can't rewrite,Please Help!
PyRun_SimpleString("print a"); PyRun_SimpleString("x = 2+2");
// PyObject x = interp.get("x");
// I can't rewrite,Please Help!
// System.out.println("x: "+x);
// I can't rewrite,Please Help!
Py_Finalize();
printf("Goodbye, cruel world");
}