Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

8,896 views
8,675 views

Published on

Jython 2.7 is scheduled to release by July 15. Frank will talk about the new features for Jython 2.7 and plans for Jython 3.x. He will also spend some time talking about techniques for integrating Jython with Java. Jython 2.7 release is sponsored by Adconion

Published in: Technology, Education

Jython 2.7 and techniques for integrating with Java - Frank Wierzbicki

  1. 1. Jython 2.7 Update Frank WierzbickiWednesday, May 9, 2012
  2. 2. 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
  3. 3. 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/ioWednesday, May 9, 2012
  4. 4. 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
  5. 5. 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 communityWednesday, May 9, 2012
  6. 6. Collaborators • Major apps • Implementations - CPython, PyPy • Other Java dynamic languages - JRuby • Tooling support • Java Virtual Machine development • Academic research - gradual typingWednesday, May 9, 2012
  7. 7. Features • Abstract Base Classes • Bytearray • __format__ • OrderedDict • lots of Lib updates • See CPython 2.6/2.7 what’s new for moreWednesday, May 9, 2012
  8. 8. 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
  9. 9. java.util.concurrent not interpreted Java Platform no GIL -J for setting options like one of 20 or so GCs heap size etc • 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 usedWednesday, May 9, 2012
  10. 10. 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 JythonWednesday, May 9, 2012
  11. 11. builtin Java interfaces • Jython’s list implements Java List<Object> • Jython’s dict implements Map and ConcurrentMap • Jython’s set implements Java SetWednesday, May 9, 2012
  12. 12. //Java interface package 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 Java PythonInterpreter 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
  13. 13. PlyJy • For a more sophisticated approach to calling into Python code from Java in Jython see the PlyJy projectWednesday, May 9, 2012
  14. 14. import org.plyjy.factory.JythonObjectFactory;   JythonObjectFactory factory = JythonObjectFactory.getInstance(); MyInterface pythonObject = (MyInterface) factory.createObject( MyInterface.class, "acme.pyobject");Wednesday, May 9, 2012
  15. 15. 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
  16. 16. 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 worksWednesday, May 9, 2012
  17. 17. zxJDBC • zxJDBC implements DB-API • integrated into Jython’s core • A crucial piece for SQLAlchemy, Pyramid and Django support.Wednesday, May 9, 2012
  18. 18. Modjy • Modjy implements WSGI for Jython • Acts as a bridge to Java Servlets • integrated into Jython’s core • Pyramid and Django supportWednesday, May 9, 2012
  19. 19. Working apps • SQLAlchemy • Pyramid, DJango • setuptools, ez_install, distribute • virtualenv • pip • many more pure python appsWednesday, May 9, 2012
  20. 20. ez_install • Get ez_setup.py • run “jython ez_setup.py” • In Jython’s bin directory will be an ez_install that runs from JythonWednesday, May 9, 2012
  21. 21. 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 detailWednesday, May 9, 2012
  22. 22. 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
  23. 23. war format for doj |-- WEB-INF | |-- lib | `-- lib-python | |-- Lib | |-- django | |-- doj | `-- mysite `-- mediaWednesday, May 9, 2012
  24. 24. 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
  25. 25. 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.0Wednesday, May 9, 2012
  26. 26. Add Jython install stuff to a new jar $ cd $JYTHON_HOME $ cp jython.jar app.jar $ zip -r app.jar LibWednesday, May 9, 2012
  27. 27. 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.jarWednesday, May 9, 2012
  28. 28. 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.jarWednesday, May 9, 2012
  29. 29. 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
  30. 30. 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 moreWednesday, May 9, 2012
  31. 31. 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 gunkWednesday, May 9, 2012
  32. 32. 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 packagesWednesday, May 9, 2012
  33. 33. Where to Find Out More • http://www.jython.org • http://wiki.python.org/jython • http://fwierzbicki.blogspot.com • Twitter: fwierzbicki • frank@python.orgWednesday, May 9, 2012

×