Jython: Integrating Python and Java

22
Jython: Integrating Python and Java Charles Anderson Western Skies Consulting Western Oregon University [email protected] / [email protected]

description

This was a talk I gave at the Salem Java Users Group on 2/5/08.

Transcript of Jython: Integrating Python and Java

Page 1: Jython: Integrating Python and Java

Jython: Integrating Python and Java

Charles AndersonWestern Skies Consulting

Western Oregon [email protected] / [email protected]

Page 2: Jython: Integrating Python and Java

10 Second Summary

• Jython is the Python interpreter implemented in Java

Page 3: Jython: Integrating Python and Java

Outline

• Python

• Scripting and embedded languages

• Jython

• Other Java scripting languages

Page 4: Jython: Integrating Python and Java

Python Background

• Python is a object-oriented scripting language - runs on many platforms

• Mature but still evolving: version 1.0 - 1994, current version: 2.5, 3.0 under development

• Huge user base including Google, Yahoo, YouTube, Industrial Light & Magic

Page 5: Jython: Integrating Python and Java

Python Overview

• Python is a scripting language - no compile

• Dynamic typing - no variable declarations, but strictly typed at runtime

• Simple syntax - large library rather than cryptic syntax (c.f., Perl)

• Often a refuge from other languages

Page 6: Jython: Integrating Python and Java

Hello World

• print “hello world”

• that’s it - no semicolons, no main function, no curly braces, etc.

Page 7: Jython: Integrating Python and Java

Language Features

• Object oriented: hybrid/not pure, multiple inheritance - c.f., C++

• Polymorphism without inheritance - “duck typing”

• Very dynamic - introspection/reflection is simple and powerful - c.f., Java

Page 8: Jython: Integrating Python and Java

Syntax

• Pretty simple - familiar to Java programmers

c = sqrt(a*a, b*b)

emp = Employee(‘Bob’, ‘Jones’)

employees[‘Bob’] = emp

import sys, email.mime.text

Page 9: Jython: Integrating Python and Java

More Syntax

if x < 0:

print ‘negative’

try:

z = y / x

except ZeroDivisionError, msg:

print msg

Page 10: Jython: Integrating Python and Java

Class Definition

class Point2D:

def __init__(self, x, y):

self.x = x

self.y = y

def distance(self):

return math.sqrt( self.x**2+self.y**2)

Page 11: Jython: Integrating Python and Java

Syntax Notes

• The block structure of code is determined by indentation - what you see is what the compiler sees

• No dangling curly braces

• Python favors libraries over fancy, terse syntax - c.f., Perl

• When needed, __method__

Page 12: Jython: Integrating Python and Java

Embedded Languages

• TCL was developed to provide a common command language for tools written in C

• Adding command line to tools

• Extending TCL to add commands from C to TCL

• Similar to Unix shell - C functions instead of executable programs

Page 13: Jython: Integrating Python and Java

Extending TCL

• TCL uses a simple interface between TCL and C code: int argc, char** argv

• Programmer writes thin layer of interface functions to expose functionality to TCL

• Programmer has to choose functions to be exposed

• Can be automated - swig

Page 14: Jython: Integrating Python and Java

Scripting a Program

• Do the real work in C (or whatever) and glue it together with TCL

• Provides extensibility of C tool to users

• Performance of script language doesn’t really matter - Pareto principal

• E.g., Numeric Python at LLNL

• Can prototype in scripting language

Page 15: Jython: Integrating Python and Java

Python Interpreter

• Python is an interpreted language - need an interpreter to execute Python code

• Originally written in C - “CPython”

• Code is compiled to byte codes at runtime

• Python can be interfaced with existing C code similar to TCL

Page 16: Jython: Integrating Python and Java

Jython Interpreter

• Jython is Python interpreter written in Java

• Python code is compiled to Java byte codes

• Aside: Iron Python is interpreter written to .Net CLR

Page 17: Jython: Integrating Python and Java

Jython Ramifications

• Interpreter written in Java - so what?

• Jython interfaces directly to Java w/o any explicit coding

• Uses Java introspection/reflection

• All Java classes on class path are immediately available from Python code

• Interactive command line for Java

Page 18: Jython: Integrating Python and Java

Demos

• Interactive

• GUI

Page 19: Jython: Integrating Python and Java

Java Python Integration

• Python can call Java code

• Java code can call out to Python - possibly in a new Python interpreter (sandbox)

• Python code can sub-class Java classes

Page 20: Jython: Integrating Python and Java

Possible Uses

• Command prompt for controlling app

• Interactive workbench for experimenting

• One-off utility work - e.g., data loading

• Flexible “business” rules

Page 21: Jython: Integrating Python and Java

Scripting Java

• Groovy - groovy.codehaus.org

• JACL (TCL in Java) - tcljava.sourceforge.net

• JRuby - jruby.codehaus.org

• JSR 223 Scripting the Java Platform - Java 6 SE

Page 22: Jython: Integrating Python and Java

Jython Information

• www.jython.org

• There are a couple of Jython books

• “Scripting: Higher Level Programming for the 21st Century” - Ousterhout