mixing python and java

44
Folie 1 EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009 Mixing Python and Java How Python and Java can communicate and work together EuroPython 2009 (June 30th 2009, Birmingham) Andreas Schreiber <[email protected]> German Aerospace Center (DLR), Cologne, Germany http://www.dlr.de/sc

Post on 14-Sep-2014

57.763 views

Category:

Technology


4 download

DESCRIPTION

How Python and Java can communicate and work together

TRANSCRIPT

Page 1: Mixing Python and Java

Folie 1EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Mixing Python and Java

How Python and Java can communicate and work togetherEuroPython 2009 (June 30th 2009, Birmingham)Andreas Schreiber <[email protected]>German Aerospace Center (DLR), Cologne, Germanyhttp://www.dlr.de/sc

Page 2: Mixing Python and Java

Folie 2EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Follow this presentation…

twitter.com/python_demo

Live! Live! PowerPoint PowerPoint to Twitter!to Twitter!

Page 3: Mixing Python and Java

Folie 3EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Final Version of this Slides is Final Version of this Slides is available on SlideShareavailable on SlideShare

http://tr.im/ep09javahttp://tr.im/ep09java

Page 4: Mixing Python and Java

Folie 4EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

DLRGerman Aerospace Center

Research InstitutionSpace AgencyProject Management Agency

Page 5: Mixing Python and Java

Folie 5EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Koeln

Lampoldshausen

Stuttgart

Oberpfaffenhofen

Braunschweig

Goettingen

Berlin-

Bonn

Trauen

Hamburg Neustrelitz

Weilheim

Bremen-

Locations and employees

6000 employees across 29 research institutes and facilities at

13 sites.

Offices in Brussels, Paris and Washington.

Page 6: Mixing Python and Java

Folie 6EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Research Areas

AeronauticsSpaceTransportEnergySpace AgencyProject Management Agency

Page 7: Mixing Python and Java

Folie 7EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Mixing Python and Java?

JythonIs it all about

?

Page 8: Mixing Python and Java

Folie 8EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Mixing Python and Java?Outline

Accessing Python from JavaJythonJEPP

Accessing Java from PythonJPypeJCC

Inter-process communicationCORBASOAPOther remote object libraries

Page 9: Mixing Python and Java

Folie 9EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JavaWhy People are Using Java?

Widely used in many different application domainsindustry/business, research, academia

Available on many different platformsGood performance

good Garbage Collector (and no GIL)Many, many libraries

E.g., for data base access, XML processing, PDF generation, security, or user interfaces

Availability of good documentation for all aspects of Java programmingVery good development tools

Eclipse, NetBeans, IntelliJ IDEA, …

Page 10: Mixing Python and Java

Folie 10EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Python Why People are Using Python?

Python in Research and Industry

Scientists and engineers don’t want to write software but just solve their problems

If they have to write code, it must be as easy as possible

Reasons for Python? Very easy to learn and easy to use ( = steep learning curve)

Allows rapid development ( = short development time)

Inherent great maintainability

“I want to design planes,

not software!”

Page 11: Mixing Python and Java

Folie 11EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

“Python has the cleanest, most-scientist- or engineer friendly syntax and semantics.

-Paul F. Dubois

Paul F. Dubois. Ten good practices in scientific programming. Comp. In Sci. Eng., Jan/Feb 1999, pp.7-11

Page 12: Mixing Python and Java

Folie 12EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Why Mixing Java and Python?Embedded scripting and more…

Many mature Java applications and frameworks existsLots of commercial and Open Source software systems and librariesFor example, The Eclipse Universe, Apache-Software, Portal-Frameworks, Workflows systems, …

Common use casesAdd embedded (Python) scripting to Java applicationsUse Java libraries from Python code

Page 13: Mixing Python and Java

Folie 13EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Why Mixing Java and Python?Integration of Python code into Java applications…

Existing code or libraries exist either for Java or Python onlyEffort to re-implement the functionalities could be very highIf the library is very well tested, it could be an enormous effort to reach a comparable level of quality

Common use casesJust use an existing library from the “other” languageEspecially, use Python code (or C/Fortran/WHATEVER code with Python wrappers) from JavaIts harder to wrap C codes in Java than in Python (see CTypes)

Page 14: Mixing Python and Java

Folie 14EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

ExampleQF-Test – Automated GUI Testing

Page 15: Mixing Python and Java

Folie 15EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

ExampleTENT – Software Integration and Workflow Management

Page 16: Mixing Python and Java

Folie 16EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Use Cases for Python Scripting (1)

Steering and controlling the program by user defined scriptsApplications are complex parameter variations or steering multidisciplinary coupled simulations

Automation of repeating tasksIn most cases, this is called “macro” recording and replaying

Extending user interfacesFor example, with additional customized dialogs and other extensionsIn Java, this requires scripts which use the Java GUI libraries (AWT, Swing, or SWT)

Integration of additional legacy toolsImportant and widely used for integration and workflow systems End users can integrate external codes without changing the Java program itself

Page 17: Mixing Python and Java

Folie 17EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Use Cases for Python Scripting (2)

Interactive experimentation and debugging of the Java programCan be easily done with an embedded interactive interpreter Allows end users and software developers to debug and analyze the Java program during runtime

Creating automated tests for quality assuranceRecording user actions during runtime of the Java program as a Python script (“Journaling")Editing and generalizing the recorded scriptReplaying the script manually or automatically

Page 18: Mixing Python and Java

Folie 18EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Example: Codes with Python InterfacesNumerical Simulation Software in C++ or Fortran

Examples for high-definition CFD-Solver with Python interfacesDLR TAU-Code (http://www.dlr.de/as)ONERA elsA-Code (http://elsa.onera.fr)

Integration into Java workflow systems with the following techniques…

EurofighterEurofighterX-31X-31

Page 19: Mixing Python and Java

Folie 19EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Tools for Python-Java-Integration

Python Interpreter

Python Interpreter

Java Virtual Machine (JVM)

Java Code(Anwendung)

JEPP

JNI

Python Code JPype

JythonPython

Code

Interface/Protokoll

Python Code

Python Code

Page 20: Mixing Python and Java

Folie 20EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Accessing Python from Java

Use casesThe application should have embedded scripting functionality.The application should use an external code written in Python.The application should use an external code written in Python or other languages such as C, C++, Fortran.

ToolsJythonJEPP

Page 21: Mixing Python and Java

Folie 21EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Java Virtual Machine

Jython

Complete re-implementation of the Python interpreter in JavaPython code runs in the Java VM

Website: http://www.jython.org

Latest version: Jython 2.5

For details & questions, catchTobias Ivarsson Frank Wierzbicki

and others here at EuroPython

Jython

Python Code

Java Application

Code

Page 22: Mixing Python and Java

Folie 22EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JythonCode Example 1: Java code

Execute Python code

import org.python.util.PythonInterpreter ;import org.python.core.*;class TestClass { public static void main(String[] args) { try { org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter (); python.execfile("python_script.py"); } catch (Exception e) { System.out.println(“Some error!"); }}

Page 23: Mixing Python and Java

Folie 23EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JythonCode Example 2: Python code

Use Swing

from javax.swing import *

frame = JFrame("Hello Jython")label = JLabel("Hello Jython!", JLabel.CENTER)frame.add(label)frame.setDefaultCloseOperation(

JFrame.EXIT_ON_CLOSE)frame.setSize(200, 100)frame.show()

Page 24: Mixing Python and Java

Folie 24EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JEPPJava Embedded Python

Embeds CPython interpreter via Java Native Interface (JNI) in Java

Python code runs in CPython

Website: http://jepp.sourceforge.net/

Python Interpreter

Java Virtual Machine

Java Application Code

Python Code

JNIJEPP

Page 25: Mixing Python and Java

Folie 25EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JEPPCode Example

Execute (“evaluate”) Python statements

Execute a Python script file

Jep jep = new Jep(false, SCRIPT_PATH, cl);jep.eval("print 'hello'");jep.close();

Jep jep = new Jep(false, SCRIPT_PATH, cl);jep.runScript(SCRIPT_PATH + file);jep.close();

Page 26: Mixing Python and Java

Folie 26EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Accessing Java from Python

Use casesThe application should use an external Java application.The application should use a Java library.

ToolsJPypeJCC

Page 27: Mixing Python and Java

Folie 27EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Python Interpreter

JNI

JPype

Python Application Code

Java Virtual Machine

Java ApplicationCode

JPypeJava to Python Integration

Interface on native level of both (Java & Python) Virtual Machines/InterpretersStarts a Java Virtual Machine

Website: http://jpype.sourceforge.net

Page 28: Mixing Python and Java

Folie 28EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JPypeCode Example (1)

Hello World

from jpype import *# Start JVMstartJVM (path to jvm.dll, "-ea")# Print "Hello World"java.lang.System.out.println("Hello World")# Shutdown JVMshutdownJVM()

Page 29: Mixing Python and Java

Folie 29EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JPypeCode Example (2)

Call Java methods from Pythonimport jpype# Start JVMjpype.startJVM(path to jvm.dll, "-ea")# Create reference to java packagejavaPackage = jpype.JPackage("JavaPackageName")# Create reference to java classjavaClass = javaPackage.JavaClassName# Create instance of java classjavaObject = javaClass()# Call java methodsjavaObject.JavaMethodName()# Shutdown JVMjpype.shutdownJVM()

Page 30: Mixing Python and Java

Folie 30EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JCCPyLucene's Code Generator

C++ code generator for calling Java from C++/PythonC++ object interface for wrapping a Java library via JNI

Generates complete CPython extension

Supported on Mac OS X, Linux, Solaris and Windows, requires C++ compiler

Website: http://lucene.apache.org/pylucene/jcc

Part of PyLucene

Python Interpreter

JNI

PythonCode

Java Virtual Machine

Java ApplicationCode

pythonExtension()

Generated C++Wrapper Code

JCC CodeGenerator

Page 31: Mixing Python and Java

Folie 31EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JCCCode Example

Requirement: VM initialization

import jcc# Start JVMjcc.initVM(maxheap=‘2000m’, …)

Page 32: Mixing Python and Java

Folie 32EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

JCC Code ExampleUsing PyLucene: Search Lucene Index

Search for „Query“ in directory „index“

from lucene import QueryParser, IndexSearcher, StandardAnalyzer, FSDirectory, Hit, VERSION, initVM, CLASSPATH

initVM(CLASSPATH)directory = FSDirectory.getDirectory(“index”, False)searcher = IndexSearcher(directory)analyzer = StandardAnalyzer()command = raw_input("Query:")query = QueryParser("contents", analyzer).parse(command)hits = searcher.search(query)for hit in hits: doc = Hit.cast_(hit).getDocument() print 'path:', doc.get("path"), 'name:', doc.get("name")searcher.close()

Page 33: Mixing Python and Java

Folie 33EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Inter-Process Communication

Inter-process communication (IPC) is data exchange between different processes on one or more computers connected by a network Typical IPC techniques are

remote procedure calls (RPC)message passing.

The most common APIs areObject Request Broker (ORB) or Web Services protocols based on XML.

A drawback is the need for additional services, such as a directory service for registration and location of remote objects

Naming Service for CORBAUniversal Description, Discovery and Integration (UDDI) for Web Services

Page 34: Mixing Python and Java

Folie 34EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Java Virtual Machine

GeneratedJava Code

(Stub)

Python Interpreter

Object Request Broker (ORBs)CORBA

OR

BPython

ApplicationCode

(Server)

Generated Python Code

(Skeleton)

JavaApplication

Code(Client)

IDL Compiler

IDL

Page 35: Mixing Python and Java

Folie 35EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

CORBA Implementations for Python

Fnorb Pure Python implementation that works with CPython and Jython. It is a light-weight CORBA implementation and interoperable with the standard Java ORB. The development of Fnorb has ended.

omniORB An ORB implementation for C++ and Python that can be used from CPython. The Python ORB (omniORBpy) uses the C++ implementation of omniORB. omniORB is actively developed with regular releases.Website: http://omniorb.sourceforge.net

Page 36: Mixing Python and Java

Folie 36EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

CORBA ExampleInterface

Hello World interface in Interface Definition Language (IDL)

module HelloWorld {

const string Message = "Hello CORBA World!";

interface Hello {string hello_world();

};};

Page 37: Mixing Python and Java

Folie 37EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

CORBA ExamplePython Server (Fnorb): Implementation of Interface

# Standard/built-in modules.import sys# Fnorb modules.from Fnorb.orb import BOA, CORBA# Stubs and skeletons generated by 'fnidl'.import HelloWorld, HelloWorld_skel

class HelloWorldServer(HelloWorld_skel.Hello_skel): """ Implementation of the 'Hello' interface. """

def hello_world(self): print HelloWorld.Message return HelloWorld.Message

Page 38: Mixing Python and Java

Folie 38EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

CORBA ExamplePython Server (Fnorb): Main Function

def main(argv): # Initialise ORB and BOA orb = CORBA.ORB_init(argv, CORBA.ORB_ID) boa = BOA.BOA_init(argv, BOA.BOA_ID) # Create object reference obj = boa.create('fred', HelloWorldServer._FNORB_ID) # Create an instance of the implementation class. impl = HelloWorldServer() # Activate the implementation boa.obj_is_ready(obj, impl) # Write the stringified object reference to a file open('Server.ref', 'w').write(orb.object_to_string(obj)) boa._fnorb_mainloop() # Start the event loop return 0

Page 39: Mixing Python and Java

Folie 39EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

CORBA Example Java Client

public class Client { public static void main ( String args[] ) { java.util.Properties props = System.getProperties(); try { org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,props); org.omg.CORBA.Object obj = null; java.io.BufferedReader in = new java.io.BufferedReader( new java.io.FileReader(“Server.ref")); String ref = in.readLine(); obj = orb.string_to_object(ref); Hello hello = HelloHelper.narrow(obj); hello.hello_world(); orb.destroy(); } catch (Exception e) { e.printStackTrace(); }}

Page 40: Mixing Python and Java

Folie 40EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Web Services: SOAP

Java Virtual Machine

Servlet Container

JavaApplication

Code(Server)

Python Interpreter

Generated Python Code

(Stub)

Python Application

Code(Client)

GeneratedJava Code(Skeleton)

WSDL Compiler

WSDL

Page 41: Mixing Python and Java

Folie 41EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Web Services for Python

Zolera SOAP Infrastructure (ZSI)For Python the only remaining SOAP toolkit of considerable quality is the Zolera SOAP Infrastructure (ZSI)

Page 42: Mixing Python and Java

Folie 42EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Other Remote Object Libraries

Python Remote Objects (Pyro) Pyro is similar to Java's Remote Method Invocation (RMI). It is simple, very portable, and works with Jython. Brings its own Naming Service for locating remote objects. Pyro is actively developed with regular new releases.

Simple Python Interface to Remote Objects (SPIRO)SPIRO implements an ORB and is developed as a bridge between CPython and Jython. The development has ended.

Page 43: Mixing Python and Java

Folie 43EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Conclusions

… but there are alternatives which makes sense for certain use cases!

I Jython

Page 44: Mixing Python and Java

Folie 44EuroPython 2009 > Andreas Schreiber > Mixing Python and Java > 01.07.2009

Questions?

ContactContact

http://tr.im/schreiberhttp://tr.im/schreibertwitter.com/onyametwitter.com/onyame

[email protected]@dlr.de