Commits

Shashank Bharadwaj  committed b4ffa45 Merge

merge

  • Participants
  • Parent commits e7b9f25, e9d7250

Comments (0)

Files changed (1)

File pycon-2013/talk.md

 
 We need to 
 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.
+* Optimize as necessary.
 
 
 Why Jython?
 There are alternatives
 ======================
 
-Simple integrations are possible
+But first, other integrations are possible
 
 * JEPP - embed CPython within the JVM using JNI
 * JPype - embed JVM within CPython using JNI
-* Bridging between CPython and Java using IPC
+* Bridging between CPython and Java using interprocess communication
 * FIXME what about something like execnet? What's that status?
 * FIXME PyPy
 
 * 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
 
-Apache POI
-==========
-
-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:
   sheet = wb.getSheetAt(0)
   num_rows = sheet.getPhysicalNumberOfRows()
   for i in xrange(num_rows):
     row = sheet.getRow(i)
     if row:
-        num_cols = sheet.getRow(i).\
-          getPhysicalNumberOfCells()
-        for j in xrange(num_cols):
-          cell = row.getCell(j)
-          # the margins are too small to do 
-          # something interesting!!!
+      num_cols = sheet.getRow(i).\
+        getPhysicalNumberOfCells()
+      for j in xrange(num_cols):
+        cell = row.getCell(j)
+        # then do something interesting!!!
 ~~~~~
 
 http://www.jython.org/jythonbook/en/1.0/appendixB.html#working-with-spreadsheets
 * Not always because boxing/unboxing is a common idiom in Java, so JVMs can often optimize this away (* significant but it depends!)
 
 
+Example: datetime.py
+====================
+
+~~~~~
+    if _sys.platform.startswith('java'):
+        def __tojava__(self, java_class):
+            if java_class not in (Calendar, Date, Object):
+                return Py.NoConversion
+
+            calendar = Calendar.getInstance()
+            calendar.clear()
+            calendar.set(self.year, self.month - 1, self.day)
+            if java_class == Calendar:
+                return calendar
+            else:
+                return Date(calendar.getTimeInMillis())
+~~~~~
+
+
 Use of overloaded methods
 =========================
 
 
 `__tojava__`
 
-FIXME Good example: Lib/datetime.py
+
 
 
 Extending Java classes
 engine = mgr.getEngineByName("jython")
 engine.eval('''
 def factorial(n):
-    i=fact=1
-    while i <= n:
-        fact=fact*i
-        i=i+1
-    return fact
+  if i == 0:
+    return 1
+  else:
+    return n * factorial(n - 1)
 
 result = factorial(4)
 ''')
 println 'jython: ' + engine.result
 ~~~~~
 
-http://groovy.codehaus.org/JSR-223+access+to+other+JVM+languages
+Slightly modified example from http://groovy.codehaus.org/JSR-223+access+to+other+JVM+languages
 
 
+Callbacks
+=========
 
 
-FIXME show some implementation here
+Using closures
+==============
 
+* 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:
-
-
-Object factories
-=================
-
-. 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.
 
 
 Advanced integration