• Save
Communication between Java and Python
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Communication between Java and Python

on

  • 19,228 views

TechTalk von Anastasia Eifer (DLR Köln-Porz, 03.06.2008) über das Zusammenspiel von Java und Python

TechTalk von Anastasia Eifer (DLR Köln-Porz, 03.06.2008) über das Zusammenspiel von Java und Python

Statistics

Views

Total Views
19,228
Views on SlideShare
19,150
Embed Views
78

Actions

Likes
14
Downloads
0
Comments
0

5 Embeds 78

http://www.slideshare.net 69
https://wiki.sistec.dlr.de 4
http://static.slideshare.net 2
https://twitter.com 2
http://www.techgig.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Communication between Java and Python Presentation Transcript

  • 1. TechTalk: Communication between Java and Python Anastasia Eifer, DLR Simulations- und Softwaretechnik 03.06.2008
  • 2. Contents
    • Introduction
    • Motivation
    • Communication through bindings (JPE, JPI)
    • Jython (JPython)
    • Jepp
    • JPype
    • Client-server communication (SPYRO, Pyro)
    • Summary
  • 3. Introduction (1/4)
    • Java
      • Object-oriented
      • Platform-independent
      • Robust
      • Distributed
      • Secure
      • One of the most popular programming languages today
  • 4. Introduction (2/4)
    • Python
      • Dynamic multi-paradigm programming language
        • Object-oriented
        • Functional
        • Aspect-oriented
        • Scripting language
      • Remarkably clean syntax
      • Short development time
      • Extensive standard libraries
      • Highly readable language
      • Can be learned in a few days
  • 5. Introduction (3/4)
    • "Hello World!"-Code
      • Java
      • Python
    print("Hello World!") public class HelloWorld {        public static void main(String[] args) {            System.out.println("Hello World!");        }   }  
  • 6. Introduction (4/4)
    • Print the integers from 1 to 9
      • Java
      • Python
    for (int i = 1; i < 10; i++)  {   System.out.println(i);   }  for i in range (1,10):        print i 
  • 7. Motivation
    • Java and Python: a perfect couple
      • Building software from existing components
      • Combination of a system programming language (Java) and a scripting language (Python)
        • Rapid application development
        • Creating automated tests
        • Embedded scripting
  • 8. Communication through bindings
    • JPE
      • Java-Python Extension
      • JNI-based wrapper
      • Integration of Java and standard Python (CPython)
      • http://jpe.sourceforge.net/
      • JPE project does not appear to be active
    • JPI
      • Java-Python Interface
      • JNI-based wrapper
      • JPI project is no longer developed
  • 9. Jython (1/4)
    • What is Jython?
      • Complete re-implementation of the Python programming language in Java
      • Embedded scripting
      • Interactive experimentation
      • Rapid application development
    • Home
      • http://www.jython.org
  • 10. Jython (2/4)
    • Interactive experimentation
      • C:jython>jython
      • Jython 2.0 on java1.2.1
      • Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
      • >>> from java.util import Random
      • >>> r = Random()
      • >>> r.nextInt()-790940041
      • >>> for i in range(3):
      • ...     print r.nextDouble()
      • ...
      • 0.23347681506123852
      • 0.8526595592189546
      • 0.3647833839988137
      • >>>
  • 11. Jython (3/4)
    • Running Python scripts
      • import org.python.util.PythonInterpreter;
      • import org.python.core.*;
      • class TestPython {
      • public static void main ( String[] args ) { try { org.python.util.PythonInterpreter python =
      • new org.python.util.PythonInterpreter(); python.execfile ( &quot;testscript.py&quot; ); } catch ( Exception e ) { System.out.println ( &quot;An error was encountered.&quot; ); }
      • }
      • }
  • 12. Jython (4/4)
    • Current state of development
      • The current release is Jython-2.2.1
      • It is equivalent to the 2.2 release of CPython
      • CPython 2.5 is targeted for the next release
      • Jython-2.2.1 supports a large majority of the standard Python library
      • Jython-2.2.1 doesn't support a number of Python's built-in modules !
      • (e.g. those that are written in C for CPython)
  • 13. Jepp (1/4)
    • What is Jepp?
      • Java Embedded Python
      • Embeds CPython in Java
      • Communication through interfacing at the native level in both Virtual Machines
    • Home
      • http://jepp.sourceforge.net /
  • 14. Jepp (2/4)
    • Using Jepp from within Java
      • „ Hello World!“-Code
      • Running Python scripts
      • Jep jep = new Jep();
      • jep.eval(&quot;print 'Hello World!'&quot;);
      • jep.close();
      • Jep jep = new Jep(false);
      • jep.runScript(&quot;C:empestscript.py&quot;);
      • jep.close();
  • 15. Jepp (3/4)
    • Features
      • Using the console ( console.py script)
      • Creating new exception types on the fly
      • $ java -jar jep.jar console.py
      • >>> from java.lang import *
      • >>> s = String('do crazy stuff')
      • >>> print String(s.toCharArray()[3:8])
      • crazy
      • >>> from java.io import FileInputStream
      • >>> try:
      • ... FileInputStream('no such file')
      • ... except(jep.FileNotFoundException):
      • ... print 'File not found.'
      • ...
      • File not found.
  • 16. Jepp (4/4)
    • Summary
      • Very good scripting solution for Java
      • Run your existing Python scripts from Java
      • Access to high-quality Python extensions
      • Jepp's console provides an easy way to examine scripts and learn how to use new APIs
  • 17. JPype (1/3)
    • What is JPype?
      • JNI-based wrapper
      • Communication through interfacing at the native level in both Virtual Machines
      • Full access to java class libraries
    • Home
      • http://jpype.sourceforge.net /
  • 18. JPype (2/3)
    • „ Hello World!“-Code
      • from jpype import *
      • startJVM(path to jvm.dll, &quot;-ea&quot;)
      • java.lang.System.out.println(&quot;Hello World&quot;)
      • shutdownJVM()
  • 19. JPype (3/3)
    • Running Python scripts
      • import jpype
      • #start JVM with class path
      • startJVM(path to jvm.dll, &quot;-ea&quot;)
      • #create reference to java package
      • javaPackage = jpype.JPackage(&quot; JavaPackageName &quot; )
      • #create reference to java class
      • javaClass = javaPackage. JavaClassName
      • #create instance of java class
      • javaObject = javaClass()
      • #call java methods
      • javaObject. JavaMethodName()
      • #shutdown JVM
      • jpype.shutdownJVM()
  • 20. Client-server communication
    • SPIRO
      • Simple Python Interface to Remote Objects
      • Handy bridge between CPython and Java (via Jython)
      • http://www.freenet.org.nz/python/spiro
    • Pyro
      • Python Remote Objects
      • http://pyro.sourceforge.net /
  • 21. Python Interpreter Python Interpreter Java Virtual Machine (JVM) Java Code (Anwendung) Jepp JNI Python Code JPype Jython Python Code Interface/Protokoll Python Code Python Code (Autor: Andreas Schreiber)
  • 22. Summary
    • Reimplementation of Python (Jython)
      • (+) Very tight integration with Java
      • (+) One runtime environment
      • (-) Jython-2.2.1 doesn't support a number of Python's built-in modules, it is slower in comparison to CPython
    • Communication through Virtual Machines (JPype, Jepp, JPE)
      • (+) CPython‘s speed
      • (+) Access to high-quality Python extensions
      • (-) Two runtime environments
    • Client-server communication
      • (+) System- and language independence
      • (-) Significant overhead, development time-consuming
  • 23. Links
    • Exploring Python
    • Markus Nix (Hrsg.), Markus Nix, Martin Grimme, Torsten Marek, Michael Weigend, Wolfgang Weitz ( Download )
    • Communication between Java and Python