jython

18
. . Jython Python on the JVM Robert Bachmann JSUG Meeting #49 1

Upload: robert-bachmann

Post on 15-May-2015

340 views

Category:

Technology


2 download

DESCRIPTION

http://jsug.at/wiki/Meeting_49

TRANSCRIPT

Page 1: Jython

.

......

JythonPython on the JVM

Robert Bachmann

JSUG Meeting #49

1

Page 2: Jython

Outline

• Use Cases• Limitations• Usage & javax.script / JSR223• A (short) case study

2

Page 3: Jython

Python?

• Scripting language• Dynamically typed• Standard interpreter: (C)Python

3

Page 4: Jython

A broader view…

• JRuby (Ruby on the JVM)• Groovy (native JVM language)• CLR: IronPython & IronRuby

4

Page 5: Jython

Use cases

• Re-use of Java libraries• Re-use of Java infrastructure• Adding scripting abilities to a Java software• Prototyping• Test scripting• Performance improvements w.r.t (C)Python

5

Page 6: Jython

Limitations

• Not a Java replacement (cf. Scala)• No current compiler• Can not use Python modules with C code• Jython lags behind (C)Python & IronPython

I Jython: 2.5.3 stable / 2.7a2 alphaI (C)Python: 2.7 stableI IronPython: 2.7 stable

• Performance worse than Java

6

Page 7: Jython

Usage

• “Hello World” with Jython• Using Java from Jython• Using Jython with javax.script• Using Jython from Java• Deployment options

7

Page 8: Jython

Hello World

# program.pyprint ”Hello␣World”

$ jython program.pyHello World

8

Page 9: Jython

Hello World with classes and modules

### demo.pyclass Hello:

def greet(self, name):print ”Hello␣” + name

### program.pyfrom demo import Helloh = Hello()h.greet(”JSUG”)

# or:import demoh = demo.Hello()h.greet(”JSUG”)

9

Page 10: Jython

Using Java from Jython – Example

# Hello World with Swing

from javax.swing import JOptionPane

JOptionPane.showMessageDialog(None, ”Hello!”)

10

Page 11: Jython

Using Java from Jython – Notes

• Jython classes canI implement Java interfacesI extend Java classes

• ClasspathI import uses classpath via sys.pathI Add JARs via sys.path.append()

11

Page 12: Jython

javax.script

• JSR223: Scripting for the JavaTM Platform• API for using scripting languages with Java• Central class: ScriptEngine

12

Page 13: Jython

javax.script

ScriptEngineManager factory =new ScriptEngineManager();ScriptEngine engine =factory.getEngineByName(”python”);

engine.eval(”print␣’Hello,␣World’”);engine.put(”x”, 10)engine.eval(”y␣=␣x␣*␣2”);Object y = engine.get(”y”)System.out.println(y)

13

Page 14: Jython

Using Jython classes from Java

• Steps:I Derive from Java class / interfaceI Use JythonInterperter to create aninstance

I Call the instance’s __tojava__method

• Complete solution:http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html

14

Page 15: Jython

Deployment options

• Servlet ContaierI org.python.util.PyServletI WSGI via modjy

• Standalone .jar

15

Page 16: Jython

Case study

• Scenario: A C library with Java and .Netwrappers

• Challenge: Automated testing of all libraries• Solution: Single-source test automationwith Jython/IronPython

16

Page 17: Jython

Questions?

17

Page 18: Jython

Thanks

Twitter @robertbachmann

Email rb@ — .at

18