KEMBAR78
Introduction to jython | PPT
INTRODUCTION TO
JYTHON
John
Saturday, December 21, 2013
The reason for Jython
• Access Java class library
• Embed in Java app servers
• Mix and match Python and Java code
Install and configure Jython
• Download the jar file from
www.jython.org/downloads.html
• download the jar file (latest version is
jython.2.7 alpha,which means you will have
all same feature as Python 2.7)
• Add “C:jython2.7a2” into the path, the file
jython.bat is the executable file
Attention: if you met problems in
the interactive console
if you met this problem
• Code never end if input a
string

Use chcp solve it
Input command on line
$ chcp 437
import Java built-in classes
Use HashMap java class

covert HashMap to dict

>>> from java.util import
HashMap
>>> a = HashMap()
>>> a. get(“key”,1)
>>> a
{key=1}
>>> type(a)
<type ‘java.util.HashMap’>

>>> d = dict()
>>> d.update(a)
>>> print d
{u’key’:1}
More examples
Call java swing libaray
from javax.swing import *
frame = JFrame(“Hello,Jython”)
label = JLabel(“Hello
Jython!”,JLabel.CENTER)
frame.add(label)
frame.setDefaultCloseOperation(JFra
me.EXIT_ON_CLOSE)
frame.setSize(200,100)
frame.close()
Mix Java and Python
Step 1: write java class in java
file

Step 2: compile it and add it
to CLASSPATH

#file Foo.java
public class Foo
{
public String stuff(int x)
{
String buf = new
String(“Test from Java:”);
buf = buf +
Integer.toString(x);
return buf
}
}

#Compile the java code
#A Foo.class will be generated if java
code have no error
$ javac Foo.java
#Build the jar file Foo.jar
$ jar cvf Foo.jar Foo.class
#update the CLASSPATH on windows
$ set CLASSPATH = %CLASSPATH%
C:/Foo.jar;
#use echo view the change
$ echo %CLASSPATH%
Use Java class in Python
import the Foo class

Inherit the Foo class

>>> import Foo
>>> f = Foo()
>>> type(f)
<type ‘Foo’>
>>> f.stuff(123)
u’This is a test from Java:123’

>>> 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 Java
This is a test from Java: 123
And back to Python

Introduction to jython

  • 1.
  • 2.
    The reason forJython • Access Java class library • Embed in Java app servers • Mix and match Python and Java code
  • 3.
    Install and configureJython • Download the jar file from www.jython.org/downloads.html • download the jar file (latest version is jython.2.7 alpha,which means you will have all same feature as Python 2.7) • Add “C:jython2.7a2” into the path, the file jython.bat is the executable file
  • 4.
    Attention: if youmet problems in the interactive console if you met this problem • Code never end if input a string Use chcp solve it Input command on line $ chcp 437
  • 5.
    import Java built-inclasses Use HashMap java class covert HashMap to dict >>> from java.util import HashMap >>> a = HashMap() >>> a. get(“key”,1) >>> a {key=1} >>> type(a) <type ‘java.util.HashMap’> >>> d = dict() >>> d.update(a) >>> print d {u’key’:1}
  • 6.
    More examples Call javaswing libaray from javax.swing import * frame = JFrame(“Hello,Jython”) label = JLabel(“Hello Jython!”,JLabel.CENTER) frame.add(label) frame.setDefaultCloseOperation(JFra me.EXIT_ON_CLOSE) frame.setSize(200,100) frame.close()
  • 7.
    Mix Java andPython Step 1: write java class in java file Step 2: compile it and add it to CLASSPATH #file Foo.java public class Foo { public String stuff(int x) { String buf = new String(“Test from Java:”); buf = buf + Integer.toString(x); return buf } } #Compile the java code #A Foo.class will be generated if java code have no error $ javac Foo.java #Build the jar file Foo.jar $ jar cvf Foo.jar Foo.class #update the CLASSPATH on windows $ set CLASSPATH = %CLASSPATH% C:/Foo.jar; #use echo view the change $ echo %CLASSPATH%
  • 8.
    Use Java classin Python import the Foo class Inherit the Foo class >>> import Foo >>> f = Foo() >>> type(f) <type ‘Foo’> >>> f.stuff(123) u’This is a test from Java:123’ >>> 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 Java This is a test from Java: 123 And back to Python