Download - Jython
![Page 1: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/1.jpg)
http://brendel.com @BrendelConsult
Considering Jython
Juergen BrendelBrendel Consulting
![Page 2: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/2.jpg)
http://brendel.com @BrendelConsult
Jython
http://jython.org
![Page 3: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/3.jpg)
http://brendel.com @BrendelConsult
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
![Page 4: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/4.jpg)
http://brendel.com @BrendelConsult
Java is fast
No GIL! Multi-threading uses multiple cores
Object creation is faster
Much faster that cPython in number crunching
![Page 5: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/5.jpg)
http://brendel.com @BrendelConsult
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 standardPython shell
![Page 6: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/6.jpg)
http://brendel.com @BrendelConsult
Jython
>>> from java.util import HashMap>>>
Very easy to import any Javaclasses you have in your classpath
![Page 7: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/7.jpg)
http://brendel.com @BrendelConsult
Jython
>>> from java.util import HashMap>>> >>> h = HashMap()>>> h['foo'] = 123>>> print h{foo=123}>>> Java's HashMap easier
to use in Python thanin Java!
![Page 8: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/8.jpg)
http://brendel.com @BrendelConsult
Jython
>>> from java.util import HashMap>>> >>> h = HashMap()>>> h['foo'] = 123>>> print h{foo=123}>>>>>> type(h)<type 'java.util.HashMap'>>>>
![Page 9: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/9.jpg)
http://brendel.com @BrendelConsult
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{u'foo': 123}
![Page 10: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/10.jpg)
http://brendel.com @BrendelConsult
Using Java classes: Easy
public 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. Here's a simpleclass.
![Page 11: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/11.jpg)
http://brendel.com @BrendelConsult
Using Java classes: Easy
>>> import Foo>>> f = Foo()>>> type(f)<type 'Foo'>>>>
Easy to use fromwithin Python
![Page 12: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/12.jpg)
http://brendel.com @BrendelConsult
Using Java classes: Easy
>>> import Foo>>> f = Foo()>>> type(f)<type 'Foo'>>>>>>> f.stuff(123)u'This is a test from Java: 123'>>>
![Page 13: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/13.jpg)
http://brendel.com @BrendelConsult
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!”>>>
Now we inherit fromthe Java class...
... in Python!
Call some of thisobject's Java methods.
![Page 14: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/14.jpg)
http://brendel.com @BrendelConsult
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!>>>
![Page 15: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/15.jpg)
http://brendel.com @BrendelConsult
Use Python in Java?
public class Foo{ ...
public String className(Object x) { Class c = x.getClass(); return c.getName(); }
...}
![Page 16: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/16.jpg)
http://brendel.com @BrendelConsult
Use Python in Java?
>>> f = Foo()>>> f.className(123)u'java.lang.Integer'>>>
![Page 17: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/17.jpg)
http://brendel.com @BrendelConsult
Use Python in Java?
>>> f = Foo()>>> f.className(123)u'java.lang.Integer'>>>>>> f.className(dict())u'org.python.core.PyDictionary'>>>
On the Java side yousee Java types or someJava representation of
the Python object
![Page 18: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/18.jpg)
http://brendel.com @BrendelConsult
Your Python in Java?
![Page 19: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/19.jpg)
http://brendel.com @BrendelConsult
Your Python in Java?● Java likes it static!
![Page 20: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/20.jpg)
http://brendel.com @BrendelConsult
Your Python in Java?● Java likes it static!
● Create static contract in Java:class
abstract class
interface
![Page 21: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/21.jpg)
http://brendel.com @BrendelConsult
Your Python in Java?● Java likes it static!
● Create static contract in Java:class
abstract class
interface
● Inherit Python class from that
Only then does yourPython become usable
from within Java,and only the parts that have
been statically declared.
![Page 22: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/22.jpg)
http://brendel.com @BrendelConsult
Create Python in Java
![Page 23: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/23.jpg)
http://brendel.com @BrendelConsult
Create Python in Java
Yikes!Using Python fromwithin Java is mucheasier than creating
Python objects.
![Page 24: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/24.jpg)
http://brendel.com @BrendelConsult
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");
// Get the object representing the Python classPyObject pyObjectClass = interp.get("MyPyFooBar");
// 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 notsouseful 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);
Basically, use somecopy and paste to get this
right...
![Page 25: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/25.jpg)
http://brendel.com @BrendelConsult
More points to remember
java.lang.Exception != exceptions.Exceptions
Lots of fun when youforget that.
![Page 26: Jython](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a66ead8b42ae7218b4b76/html5/thumbnails/26.jpg)
http://brendel.com @BrendelConsult
More points to remember
No dynamic import of Java classes
(eval() or conditional import)