100% found this document useful (1 vote)
2K views

Jython

Jython is an implementation of the Python programming language that runs on the Java Virtual Machine. It allows Python code to access both Python and Java libraries and to be embedded within Java applications. The presentation introduces Jython syntax, which is similar to Python but also incorporates Java aspects like sequences and dictionaries. It provides examples of Jython code and discusses using Jython for prototyping, exploring Java APIs, and embedding scripts within Java applications. Other scripting languages for Java are mentioned, as well as performance comparisons between Jython and Java.

Uploaded by

janaranjani.k
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

Jython

Jython is an implementation of the Python programming language that runs on the Java Virtual Machine. It allows Python code to access both Python and Java libraries and to be embedded within Java applications. The presentation introduces Jython syntax, which is similar to Python but also incorporates Java aspects like sequences and dictionaries. It provides examples of Jython code and discusses using Jython for prototyping, exploring Java APIs, and embedding scripts within Java applications. Other scripting languages for Java are mentioned, as well as performance comparisons between Jython and Java.

Uploaded by

janaranjani.k
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Introduction to

Jython
Agenda
 What is Jython
 Syntax Basics
 Example Code
 Embedding Jython
 Other Scripting Languages for Java
 Red Robin Jython Eclipse Plug-in
Q + A
What is Jython?
 An implementation of the Python
(2.1) language on the Java Virtual
Machine
 Cross-platform (via JVM)
 Access to both Python and Java
libraries
 Interactive Interpreter
Jython Syntax Overview
 Loose, Dynamic Typing
 Modular organization
 Object Oriented – Everything is an
Object
 Supports classes, methods
 Functional Programming (List Comp.)
 Code blocks defined by indentation
 Dynamic dispatch – Reflection made
easy!
 Operator Overloading
Jython Syntax Basics
Built-in Data Types:
Numbers (Integers, Float,
Complex)
Strings (immutable sequences)
Sequences (like ArrayList)
Dictionaries (like HashMap)
Many Advanced Types, such as File
Jython Syntax: Sequences
Jython sequence:
list = [1,2,3,4,5]
list[1:3] -> 2
list[2:5] -> [2,3,4] #called “Slicing”
list[1:3] = ‘two’ -> [1,’two’,3,4,5]

Versus Java ArrayList:


java.util.List list = new java.util.ArrayList();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));

Jython Syntax: Dictionaries
Jython dictionary:
dict = {‘one’: 1, ‘two’: 2, 3: ‘three’}
dict[‘two’] -> 2
dict[3] = “three” -> sets “three”

versus Java HashMap:


java.util.Map map = new java.util.HashMap();
map.put(“one”, new Integer(1));
map.put(“two” new Integer(2));
Map.put(new Integer(3), “three”);
Jython Syntax:
Conditionals
# in Python (and Jython) 0 = false, 1 = true

if expression:
block
elif expression:
block
else:
block

try:
block
except SomeError:
block
finally:
block
Jython Syntax: Loops
import java.security as sec
import java.lang as lang
providers = sec.Security.getProviders() #sequence
for p in providers:
print p.name
print “-----------------------”
p.list(lang.System.out)
Jython Syntax: F.P.
>>> map(lambda x: 2 * x, [1,2,3])
[2,4,6]

>>> filter(lambda x: x > 0, [-1, 0, 1, 2])


[1,2]

>>> reduce(lambda x, y: x + y, [0,1,2,3])


6

>>> def func(x,y):


... print "(x,y): (%s,%s)" % (x,y)
... return x + y
...
>>> reduce(lambda x,y: func(x,y), [0,1,2,3])
(x,y): (0,1)
(x,y): (1,2)
(x,y): (3,3)
6
Jython Syntax: List Comp.
>>> evens = [x for x in range(10) if x%2==0]
>>> evens
[0,2,4,6,8]
Jython Syntax: Functions
def listAll(seq):
for each in seq:
print each

method = listAll
list = [1,2,3,4,5]
method(list)
Jython Syntax: Classes
# from http://www.jython.org/applets
from java.applet import Applet

class HelloWorld(Applet):
def paint(self, g):
g.drawString(“Hello World!”, 20, 30)
Example Time
 Jython Interpreter
 Eclipse – Red Robin Jython IDE
 http://home.tiscali.be/redrobin/jython/
Cool Things
 Read a whole file in two lines:
file = open(“somefilename.txt”, r+)
file.read()

 Java API Exploration


What is Jython good at:
 Prototyping
 JavaAPI exploration
 Bean Accessors
window = Jframe()
window.dimension=[200,200]

 Embedding in Java for extensibility


What is Jython NOT good
at:
 Performance critical code
 Anything JVM can not do
 Access Python Extensions in C
 C libraries without JNI
 COM without JNI
Embedding Jython
From page 207, Java Essentials :
import org.python.core.*;
import org.python.util.*;

public class PyInterpTest {


public static void main(String[] args) {
PySystemState.initialize();
PythonInterpreter interp = new PythonInterpreter()
PyObject value = interp.eval(“2 + 2”);
System.out.println(value);
}
}
Embedding Jython
import org.python.core.*;
import org.python.util.*;

public class EmbedJython {


public static void main(String[] args) {
String script = “value = (max – min) / 2”;
PySystemState.initialize();
PythonInterpreter interp = new PythonInterpreter();
interp.set(“max”, new PyInteger(10));
interp.set(“min”, new PyInteger(0));
interp.exec(script);
System.out.println(interp.get(“value”));
}
}
Jython Performance
 Calculating first 10000 primes
1000 10000 20000
 Java: .027s 2.566s
10.196s
 Jython: .364s 17.804s
73.804s
 Word Counting the Bible
(816635 words, 35520 unique)
 Java: 1.207s
 Jython: 4.969s
The Bean Scripting
Framework
 Provides a framework for
embedding scripting languages
into any Java application
 Uses a registry for passing data
 Supports many languages (next
slide)
Other Scripting Languages
for Java
 Groovy – new and increasingly popular
http://groovy.codehaus.org/
 Rhino – ECMAScript interpreter
http://www.mozilla.org/js/
 BeanShell – “Loose” Java
http://www.beanshell.org
 NetRexx – Rexx on Java
http://www-306.ibm.com/software/awdtools/netrexx/
 JudoScript – “A functional scripting language”
http://www.judoscript.com/index.html
 JRuby – Ruby on Java
http://jruby.sourceforge.net/
So many more:
Python Compatibility
 CPython is now on version 2.4
 Not all current Python code works
with Jython
 Extensions for CPython are written
in C and will not work with Jython.
References
Jython Essentials Pedroni, Rappin
http://www.oreilly.com/catalog/jythoness/

An Introduction to Jython Brian


Zimmer
http://ziclix.com/jython/chipy20050113/slide-01.html
URLs
Jython - http://jython.org
BSF - http://jakarta.apache.org/bsf
/index.html
Free Python Books -
http://techbooksforfree.com/perlpython.shtm
Dive Into Python -
http://diveintopython.org
Red Robin Eclipse Plug-in -
http://home.tiscali.be/redrobin/jython/
This presentation -
http://secosoft.net/presentations/

You might also like