Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Jython

889 views

Published on

http://jsug.at/wiki/Meeting_49

Published in: Technology
  • Be the first to comment

Jython

  1. 1. . Jython . Python on the JVM Robert Bachmann JSUG Meeting #49 1
  2. 2. Outline • • • • Use Cases Limitations Usage & javax.script / JSR223 A (short) case study 2
  3. 3. Python? • Scripting language • Dynamically typed • Standard interpreter: (C)Python 3
  4. 4. A broader view… • JRuby (Ruby on the JVM) • Groovy (native JVM language) • CLR: IronPython & IronRuby 4
  5. 5. 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
  6. 6. Limitations • • • • Not a Java replacement (cf. Scala) No current compiler Can not use Python modules with C code Jython lags behind (C)Python & IronPython Jython: 2.5.3 stable / 2.7a2 alpha (C)Python: 2.7 stable IronPython: 2.7 stable • Performance worse than Java 6
  7. 7. Usage • • • • • “Hello World” with Jython Using Java from Jython Using Jython with javax.script Using Jython from Java Deployment options 7
  8. 8. Hello World # program.py print ”Hello␣World” $ jython program.py Hello World 8
  9. 9. Hello World with classes and modules ### demo.py class Hello: def greet(self, name): print ”Hello␣” + name ### program.py from demo import Hello h = Hello() h.greet(”JSUG”) # or: import demo h = demo.Hello() h.greet(”JSUG”) 9
  10. 10. Using Java from Jython – Example # Hello World with Swing from javax.swing import JOptionPane JOptionPane.showMessageDialog(None, ”Hello!”) 10
  11. 11. Using Java from Jython – Notes • Jython classes can implement Java interfaces extend Java classes • Classpath import uses classpath via sys.path Add JARs via sys.path.append() 11
  12. 12. javax.script • JSR223: Scripting for the JavaTM Platform • API for using scripting languages with Java • Central class: ScriptEngine 12
  13. 13. 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
  14. 14. Using Jython classes from Java • Steps: Derive from Java class / interface Use JythonInterperter to create an instance Call the instance’s __tojava__ method • Complete solution: http://www.jython.org/jythonbook/en/1.0/ JythonAndJavaIntegration.html 14
  15. 15. Deployment options • Servlet Contaier org.python.util.PyServlet WSGI via modjy • Standalone .jar 15
  16. 16. Case study • Scenario: A C library with Java and .Net wrappers • Challenge: Automated testing of all libraries • Solution: Single-source test automation with Jython/IronPython 16
  17. 17. Questions? 17
  18. 18. Thanks Twitter @robertbachmann Email rb@ — .at 18

×