Published on

Very brief intro on how to use Jython.

Published in: Technology, News & Politics
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Considering Jython Juergen Brendel Brendel Consulting http://brendel.com @BrendelConsult
  2. 2. Jython http://jython.orghttp://brendel.com @BrendelConsult
  3. 3. Good reasons for Jython Minimize pain for Java organization Access to Java class library Embed in Java app servers Mix and match Python and Java code http://brendel.com @BrendelConsult
  4. 4. Java is fast No GIL! Multi-threading uses multiple cores Object creation is fasterMuch faster that cPython in number crunching http://brendel.com @BrendelConsult
  5. 5. Jython% jythonJython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54) [Java HotSpot(TM) Server VM (Sun Microsystems Inc.)] on java1.6.0_20Type "help", "copyright", "credits" or "license" for more information.>>>  You get a standard Python shell http://brendel.com @BrendelConsult
  6. 6. Jython>>> from java.util import HashMap>>>  Very easy to import any Java classes you have in your classpath http://brendel.com @BrendelConsult
  7. 7. Jython>>> from java.util import HashMap>>> >>> h = HashMap()>>> h[foo] = 123>>> print h{foo=123}>>> Javas HashMap easier to use in Python than in Java! http://brendel.com @BrendelConsult
  8. 8. Jython>>> from java.util import HashMap>>> >>> h = HashMap()>>> h[foo] = 123>>> print h{foo=123}>>>>>> type(h)<type java.util.HashMap>>>> http://brendel.com @BrendelConsult
  9. 9. Jython>>> from java.util import HashMap>>> >>> h = HashMap()>>> h[foo] = 123>>> print h{foo=123}>>>>>> type(h)<type java.util.HashMap>>>>>>> d = dict()>>> d.update(h)>>> print d{ufoo: 123} http://brendel.com @BrendelConsult
  10. 10. Using Java classes: Easypublic class Foo{    public String stuff(int x)    {        String buf =            new String("Test from Java: ");        buf = buf + Integer.toString(x);        return buf;    }} So, you write your own Java code. Heres a simple class. http://brendel.com @BrendelConsult
  11. 11. Using Java classes: Easy>>> import Foo>>> f = Foo()>>> type(f) Easy to use from<type Foo> within Python>>> http://brendel.com @BrendelConsult
  12. 12. Using Java classes: Easy>>> import Foo>>> f = Foo()>>> type(f)<type Foo>>>>>>> f.stuff(123)uThis is a test from Java: 123>>> http://brendel.com @BrendelConsult
  13. 13. Using Java classes: Easy Now we inherit from the Java class...>>> class Bar(Foo): ... in Python!...     def blah(self, x):...         print “Python, about to do Java”...         print self.stuff(x)...         print “And back to Python!”>>> Call some of this objects Java methods. http://brendel.com @BrendelConsult
  14. 14. Using Java classes: Easy>>> class Bar(Foo):...     def blah(self, x):...         print “Python, about to do Java”...         print self.stuff(x)...         print “And back to Python!”>>>>>> b = Bar()>>> b.blah(123)Python, about to do JavaThis is a test from Java: 123And back to Python!>>> http://brendel.com @BrendelConsult
  15. 15. Use Python in Java?public class Foo{    ...    public String className(Object x)    {        Class c = x.getClass();        return c.getName();    }    ...} http://brendel.com @BrendelConsult
  16. 16. Use Python in Java?>>> f = Foo()>>> f.className(123)ujava.lang.Integer>>> http://brendel.com @BrendelConsult
  17. 17. Use Python in Java?>>> f = Foo() On the Java side you see Java types or some>>> f.className(123) Java representation ofujava.lang.Integer the Python object>>>>>> f.className(dict())uorg.python.core.PyDictionary>>> http://brendel.com @BrendelConsult
  18. 18. Your Python in Java? http://brendel.com @BrendelConsult
  19. 19. Your Python in Java?● Java likes it static! http://brendel.com @BrendelConsult
  20. 20. Your Python in Java?● Java likes it static!● Create static contract in Java: class abstract class interface http://brendel.com @BrendelConsult
  21. 21. Your Python in Java?● Java likes it static!● Create static contract in Java: Only then does your class Python become usable from within Java, abstract class and only the parts that have been statically declared. interface● Inherit Python class from that http://brendel.com @BrendelConsult
  22. 22. Create Python in Java http://brendel.com @BrendelConsult
  23. 23. Create Python in Java Yikes! Using Python from within Java is much easier than creating Python objects. http://brendel.com @BrendelConsult
  24. 24. Create Python in Java// Get the handle on the Jython interpreterPythonInterpreter interp = new PythonInterpreter();// Tell Jython to import something from our Python packageinterp.exec("from test_package import MyPyFooBar"); Basically, use some copy and paste to get this// Get the object representing the Python classPyObject pyObjectClass = interp.get("MyPyFooBar"); right...// Instantiate an object of that class. Arguments to __call__() are the wrapped // arguments to __init__()PyObject pyObject =    pyObjectClass.__call__(new PyString("String passed in from Java"));// Convert not­so­useful PyObject to an instance of the Java interface classFooBarInterface javaObject =               (FooBarInterface)pyObject.__tojava__(FooBarInterface.class);// Now the object can be used just as if it were a native Java objectString result = javaObject.foobar("some", "arguments", 123);System.out.println("Received from Python: " + result); http://brendel.com @BrendelConsult
  25. 25. More points to rememberjava.lang.Exception != exceptions.Exceptions Lots of fun when you forget that. http://brendel.com @BrendelConsult
  26. 26. More points to remember No dynamic import of Java classes (eval() or conditional import) http://brendel.com @BrendelConsult
  27. 27. The Endjuergen@brendel.com @BrendelConsulthttp://brendel.com http://brendel.com @BrendelConsult
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.