We are lazy! We got this amazing Python implementaton with Java integration, why do we need to write any Java to use Java?!!!
+Optimize as necessary.
Simple integrations are possible
+ integrations are possible
* JEPP - embed CPython within the JVM using JNI
* JPype - embed JVM within CPython using JNI
-* Bridging between CPython and Java using
+* Bridging between CPython and Java using
* FIXME what about something like execnet? What's that status?
* Work with Java objects. Your programs can call Java methods, or have Java call back into Python.
* And this "magic"/integration works because we have built a proxy on the fly
-Read and write MS Office formatted docs:
+Apache POI: Read and write MS Office docs
from contextlib import closing
from org.apache.poi.hssf.usermodel import *
from java.io import FileInputStream
-file = "important-data.xls"
-with closing(FileInputStream(file)) as fis, \
+with closing(FileInputStream("data.xls")) as fis, \
closing(HSSFWorkbook(fis)) as wb:
num_rows = sheet.getPhysicalNumberOfRows()
for i in xrange(num_rows):
- num_cols = sheet.getRow(i).\
- for j in xrange(num_cols):
- # the margins are too small to do
- # something interesting!!!
+ num_cols = sheet.getRow(i).\
+ for j in xrange(num_cols):
+ # then do something interesting!!!
* Not always because boxing/unboxing is a common idiom in Java, so JVMs can often optimize this away (* significant but it depends!)
+ if _sys.platform.startswith('java'):
+ def __tojava__(self, java_class):
+ if java_class not in (Calendar, Date, Object):
+ calendar = Calendar.getInstance()
+ calendar.set(self.year, self.month - 1, self.day)
+ if java_class == Calendar:
+ return Date(calendar.getTimeInMillis())
Use of overloaded methods
FIXME Good example: Lib/datetime.py
engine = mgr.getEngineByName("jython")
+ return n * factorial(n - 1)
println 'jython: ' + engine.result
-FIXME show some implementation here
+* Java methods that take objects of single method interfaces
+* Useful for working with `Callable` or `Runnable` interfaces
+* Can pass in a lexical closure, that is a function that closes over variables in its lexical scope
+How does that actually work?
-If the Java method takes a single-method interface, you can even directly pass in a Python function, including a closure. It's even possible to get and set private fields (a capability given by reflection). Finally, a Python class can readily extend a Java class and/or interfaces. However, there are some limitations imposed by working with the Java object model, as we will discuss, along with performance considerations, whether that is the overhead of reflection or the cost of converting between Python and Java representations.
+Using Python code from Java: Object factories
-It's a bit more involved for Java code to directly use Python code. (There's no way just yet to say "import django;" into a Java project.) We will next talk about the APIs Jython provides for this integration. In particular, we will discuss:
-. Such factories use Jython-specific APIs to efficiently build Python objects. With some more wrapping, they can also work around the lack of Java annotation support.
+* Java code can use the Jython runtime to construct Python objects
+* Object factories use Jython-specific APIs to efficiently build Python objects
+* Ask the Jython runtime to
+. With some more wrapping, they can also work around the lack of Java annotation support.