jython
DESCRIPTION
http://jsug.at/wiki/Meeting_49TRANSCRIPT
![Page 1: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/1.jpg)
.
......
JythonPython on the JVM
Robert Bachmann
JSUG Meeting #49
1
![Page 2: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/2.jpg)
Outline
• Use Cases• Limitations• Usage & javax.script / JSR223• A (short) case study
2
![Page 3: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/3.jpg)
Python?
• Scripting language• Dynamically typed• Standard interpreter: (C)Python
3
![Page 4: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/4.jpg)
A broader view…
• JRuby (Ruby on the JVM)• Groovy (native JVM language)• CLR: IronPython & IronRuby
4
![Page 5: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/7.jpg)
Usage
• “Hello World” with Jython• Using Java from Jython• Using Jython with javax.script• Using Jython from Java• Deployment options
7
![Page 8: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/8.jpg)
Hello World
# program.pyprint ”Hello␣World”
$ jython program.pyHello World
8
![Page 9: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/10.jpg)
Using Java from Jython – Example
# Hello World with Swing
from javax.swing import JOptionPane
JOptionPane.showMessageDialog(None, ”Hello!”)
10
![Page 11: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/12.jpg)
javax.script
• JSR223: Scripting for the JavaTM Platform• API for using scripting languages with Java• Central class: ScriptEngine
12
![Page 13: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/15.jpg)
Deployment options
• Servlet ContaierI org.python.util.PyServletI WSGI via modjy
• Standalone .jar
15
![Page 16: Jython](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080210/55557b1cb4c9055f5f8b4ecd/html5/thumbnails/17.jpg)
Questions?
17