Download - Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Transcript
Page 1: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Jython 2.7 UpdateFrank Wierzbicki

Wednesday, May 9, 2012

Page 2: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Why use Jython?

• Massive amounts of Java code out there

• Some really useful Java libs

• Great JVM ecosystem

• But we’d all rather be writing Python code, right?

Wednesday, May 9, 2012

Page 3: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Status

• Jython 2.7 is progresing well

• bytearray and io are the last missing big features

• 2.7 Unit test compliance

• 684 failing tests out of 10786 (~6%)

• Most of these are due to bytearray/io

Wednesday, May 9, 2012

Page 4: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Dates

• Jython 2.7 work is being sponsored by Adconion. (Thanks!)

• Jython 2.7 alpha should be out this month

• The target for a production release is July 15 - which happens to be the final day of my contract to get Jython to 2.7 :)

Wednesday, May 9, 2012

Page 5: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Why is Adconion sponsoring Jython?• Adconion is a hybrid Java/Python shop

based out of LA

• They use Python version 2.7 and Jython 2.5

• They have internal Django apps that access Java libraries

• The 2.5/2.7 differences are a pain

• They’d like to give back to the community

Wednesday, May 9, 2012

Page 6: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Collaborators

• Major apps

• Implementations - CPython, PyPy

• Other Java dynamic languages - JRuby

• Tooling support

• Java Virtual Machine development

• Academic research - gradual typing

Wednesday, May 9, 2012

Page 7: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Features

• Abstract Base Classes

• Bytearray

• __format__

• OrderedDict

• lots of Lib updates

• See CPython 2.6/2.7 what’s new for more

Wednesday, May 9, 2012

Page 8: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Not in Jython 2.7

• multiprocessing

• buffer (so far never has been in Jython)

• memoryview (though there is a start, we’ll see)

Wednesday, May 9, 2012

Page 9: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Java Platform

• Starting to (optionally) use invokedynamic

• Everything is compiled to Java Bytecode

• Use a choice of Java garbage collectors

• Always uses Java native threads

• All cores are used

java.util.concurrentnot interpretedno GIL

-J for setting options like one of 20 or so GCsheap sizeetc

Wednesday, May 9, 2012

Page 10: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Java Integration

• Jython integrates well with Java

• Jython makes calling into Java look like standard Python code

• Java classes and interfaces can be subclassed from Jython

Wednesday, May 9, 2012

Page 11: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

builtin Java interfaces

• Jython’s list implements Java List<Object>

• Jython’s dict implements Map and ConcurrentMap

• Jython’s set implements Java Set

Wednesday, May 9, 2012

Page 12: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

//Java interfacepackage org.acme;

public interface MyInterface { public List strList();}

#From Python in file “acme.py”from org.acme import MyInterface

class pyobject(MyInterface): def strList(self): return [‘a’, ‘b’, ‘c’]

//From JavaPythonInterpreter interpreter = new PythonInterpreter ();interpreter.exec ("from acme import pyobject");MyInterface pythonObject = interpreter.get ("pyobject");

List pywords = pythonObject.strList()for (Object o : pyList){  String string = ((PyObject)o).__toJava__(String.class); //Do something with the now Java native strings.}

Wednesday, May 9, 2012

Page 13: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

PlyJy

• For a more sophisticated approach to calling into Python code from Java in Jython see the PlyJy project

Wednesday, May 9, 2012

Page 14: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

import org.plyjy.factory.JythonObjectFactory; JythonObjectFactory factory = JythonObjectFactory.getInstance();MyInterface pythonObject = (MyInterface) factory.createObject( MyInterface.class, "acme.pyobject");

Wednesday, May 9, 2012

Page 15: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Jython package cache

[frank jython]$ ./dist/bin/jython*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/jython-dev.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/antlr-2.7.7.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/antlr-3.1.3.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/antlr-runtime-3.1.3.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/asm-4.0.jar'*sys-package-mgr*: processing new jar, '/home/frank/hg/jython/jython/dist/javalib/asm-commons-4.0.jar'

...

Wednesday, May 9, 2012

Page 16: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Java package cache

• Goes through all jars and classes and figures out Java package contents

• enables “from java.foo import *”

• Java does not have a package.getClasses()

• If cache is disabled “from java import *” will not work from Jython

• * import for pure python still works

Wednesday, May 9, 2012

Page 17: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

zxJDBC

• zxJDBC implements DB-API

• integrated into Jython’s core

• A crucial piece for SQLAlchemy, Pyramid and Django support.

Wednesday, May 9, 2012

Page 18: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Modjy

• Modjy implements WSGI for Jython

• Acts as a bridge to Java Servlets

• integrated into Jython’s core

• Pyramid and Django support

Wednesday, May 9, 2012

Page 19: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Working apps

• SQLAlchemy

• Pyramid, DJango

• setuptools, ez_install, distribute

• virtualenv

• pip

• many more pure python apps

Wednesday, May 9, 2012

Page 20: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

ez_install

• Get ez_setup.py

• run “jython ez_setup.py”

• In Jython’s bin directory will be an ez_install that runs from Jython

Wednesday, May 9, 2012

Page 21: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Install Django

• Get 1.3+ Django - run “jython setup.py install”

• From http://django-jython.googlecode.com get django-jython-1.3.tar.gz, untar it and run “jython setup.py install”

• see the django-jython project on googlecode.com for more detail

Wednesday, May 9, 2012

Page 22: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Deploy Django

• run “jython manage.py war --include-java-libs=jdbcdriver.jar”

• This will produce a “war file”

• Copy this war file to the auto-deploy directory for most application servers (like Tomcat, Jetty or Glassfish)

Wednesday, May 9, 2012

Page 23: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

war format for doj|-- WEB-INF

| |-- lib | `-- lib-python | |-- Lib | |-- django | |-- doj | `-- mysite `-- media

Wednesday, May 9, 2012

Page 24: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Pyramid on Jython

• jython ez_install pyramid

• Note that you should not try to use Chameleon with Jython - use Mako for templates.

Wednesday, May 9, 2012

Page 25: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Creating an executable Jar

• Make a copy of jython.jar

• copy Lib/ into jython.jar

• copy a __run__.py file into new jar

• in the future it will be a __main__.py to fit with the new CPython convention as of 2.6/3.0

Wednesday, May 9, 2012

Page 26: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Add Jython install stuff to a new jar

$ cd $JYTHON_HOME$ cp jython.jar app.jar$ zip -r app.jar Lib

Wednesday, May 9, 2012

Page 27: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Add modules and paths to the jar file

$ cd $MY_APP_DIRECTORY$ zip app.jar *.py# Add path to additional jar file.$ jar ufm myapp.jar othermanifest.mf#Where, othermanifest.mf contains the following:Class-Path: ./otherjar.jar

Wednesday, May 9, 2012

Page 28: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Add runner file, and run!

$ zip myapp.jar __run__.py#in 2.7/3.x$ zip myapp.jar __main__.py$ java org.python.util.jython -jar app.jar

Wednesday, May 9, 2012

Page 29: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Jython 3000

• We already have a Jython 3 branch

• It has a nearly complete parser

• Not much else yet

• Can’t wait to delete old style classes and fake str suppot!

• I’d like to target 3.3 and support only JDK7 or above (maybe even JDK8 if it takes long)

Wednesday, May 9, 2012

Page 30: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Jython 3 changes?

• All of the same changes as Python3

• unicode for all strings

• no old style classes

• new metaclass syntax

• many iterators instead of lists

• much more

Wednesday, May 9, 2012

Page 31: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Jython specific changes

• Better way to call into Jython? Project Lambda maybe?

• Default to not scan packages?

• Get rid of swing specific helper functions

• Remove Lib gunk

Wednesday, May 9, 2012

Page 32: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Jython/CPython collaboration

• Break out the CPython standard Lib into a shared Lib - merge all of Jython’s customizations into CPython Lib

• Share CPython’s 3.3 import implementation

• PEP 420 implicit namespace packages for Java packages

Wednesday, May 9, 2012

Page 33: Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

Where to Find Out More

• http://www.jython.org

• http://wiki.python.org/jython

• http://fwierzbicki.blogspot.com

• Twitter: fwierzbicki

[email protected]

Wednesday, May 9, 2012