Unformatted text preview:

Interfacing with PythonWhy?Overview1/2: Python/C API3: Using Java from PythonJythonJythonJythonJythonJPypeJPype4: Embed Python in JavaEmbedded JythonEmbedded JythonEmbedded JythonEmbedded JythonEmbedded JythonIncestuous InterpreterJEPResourcesInterfacing with PythonRavi ChughApril 19, 20062Why?• Python is still a young language• Many more C/C++/Java libraries than Python• Ability to communicate between languages is powerful• Use languages that are suited for different problems, then combine them3Overview• Many independent ways of achieving cross-language communication:1) Extending Python using C2) Embedding Python in C applications3) Extending Python to support Java4) Embedding Python in Java apps41/2: Python/C API• Can create your own Python modules in C, callable from Python• Can call Python modules from C• Both using Python/C API• #include <Python.h>• Writing modules – boilerplate wrapper for your C code• See Python/C Tutorial for details53: Using Java from Python• Here are two ways:–Jython•An implementation of Python written completely in Java•Runs completely within JVM–JPype•Native Python interpreter communicates with JVM through JNI (Java Native Interface)6Jython• Jython is an executable jar file>>> from java.lang import *>>> System.out.println("Hello from Jython!")Hello from Jython!>>> s = String("No type declaration and no new!")>>> System.out.println(s)No type declaration and no new!7Jython• Simple Swing example:>>> from javax.swing import *>>> frame = JFrame()>>> frame.setSize(300, 300)>>> frame.setTitle(“Hello!”)>>> frame.show()8Jythondates.py:from java.util import Datefrom java.lang import Systemday = 24 * 60 * 60 * 1000d = Date()now = d.getTime()list = [ (Date(now + i*day), now + i*day) \for i in range( 0, 7 ) ]for x in list:print x[0], ":", x[1]9Jythonjython dates.py:Tue Apr 18 15:17:30 EDT 2006 : 1145387850579Wed Apr 19 15:17:30 EDT 2006 : 1145474250579Thu Apr 20 15:17:30 EDT 2006 : 1145560650579Fri Apr 21 15:17:30 EDT 2006 : 1145647050579Sat Apr 22 15:17:30 EDT 2006 : 1145733450579Sun Apr 23 15:17:30 EDT 2006 : 1145819850579Mon Apr 24 15:17:30 EDT 2006 : 114590625057910JPype• External communication with JVM• A little more work to set up>>> from jpype import *>>> startJVM(“C:\\Program Files\\Java\\jdk1.5.0\\jre\\bin\\client\\jvm.dll”, “-ea”)>>> java.lang.System.out.println(“Hello!”)Hello!• Can access Java libraries, but need full package qualifiers11JPypedates2.py:from jpype import *startJVM("C:\\Program Files\\Java\\jdk1.5.0\\jre\\bin\\client\\jvm.dll", "-ea")day = 24 * 60 * 60 * 1000d = java.util.Date()now = d.getTime()list = [ (java.util.Date(now + i*day), now + i*day) \for i in range( 0, 7 ) ]for x in list:print x[0], ":", x[1]shutdownJVM() # optionalpython dates2.py:<same output as from Jython>124: Embed Python in Java• Here are two ways:–Use Jython packages directly•Very easy to use–JEP•May be faster, since it runs native Python interpreter, not through JVM•Access to Python extensions13Embedded Jython• Jython is fully implemented in Java• Can directly use Jython packages• Add jython.jar to classpath•Import:– import org.python.core.*;– import org.python.util.PythonInterpreter;• And then...14Embedded Jythonpublic static void example1() {PythonInterpreter interp = new PythonInterpreter();interp.exec("import sys");interp.exec("print ‘From Jython inside Java!'");System.out.println("Goodbye from Java!");}>>>From Jython inside Java!Goodbye from Java!15Embedded Jythonpublic static void example2() {PythonInterpreter interp = new PythonInterpreter();interp.exec("import sys");interp.exec("a = 5");interp.set("b", new PyInteger(10));interp.exec("x = a + b");interp.exec("print 'x =', x, 'from Jython'");PyObject x = interp.get("x");System.out.println("x = " + x + " from Java");}>>>x = 15 from Jythonx = 15 from Java16Embedded Jythonpublic static void example3() {PythonInterpreter interp = new PythonInterpreter();interp.execfile( "hi.py" );}>>>I am a saved Python program!17Embedded Jythonpublic static void example4() {PythonInterpreter interp = new PythonInterpreter();interp.execfile( "interleave.py" );interp.exec("print allinter([1,2], [3,4])" );}>>>[[1, 2, 3, 4], [1, 3, 2, 4], [1, 3, 4, 2], [3, 1, 2, 4], [3, 1, 4, 2], [3, 4, 1, 2]]18Incestuous Interpreterpublic static void main(String[] args) throws PyException, IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));PythonInterpreter interp = new PythonInterpreter();String line = "";do {System.out.print("%%% ");line = br.readLine();try {interp.exec( line );} catch (PyException e) {e.printStackTrace();}} while ( line != null);}>>> java IncestuousInterpreter%%% print “This is odd”This is odd%%%19JEP• Java Embedded Python• Alternative to embedding Jython• http://jepp.sourceforge.net/20Resources• http://www.seas.upenn.edu/~rkc/JythonResources.htm• Instructions for setting up various techniques• Links to tutorials and other


View Full Document

Penn CIS 399 - Interfacing with Python

Download Interfacing with Python
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Interfacing with Python and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Interfacing with Python 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?