Upload
janaranjanik
View
1.391
Download
2
Embed Size (px)
DESCRIPTION
PPT contains information about the jython script.
Citation preview
AgendaAgenda
What is JythonWhat is Jython Syntax BasicsSyntax Basics Example CodeExample Code Embedding JythonEmbedding Jython Other Scripting Languages for JavaOther Scripting Languages for Java Red Robin Jython Eclipse Plug-inRed Robin Jython Eclipse Plug-in Q + AQ + A
What is Jython?What is Jython?
An implementation of the Python An implementation of the Python (2.1) language on the Java Virtual (2.1) language on the Java Virtual MachineMachine
Cross-platform (via JVM)Cross-platform (via JVM) Access to both Python and Java Access to both Python and Java
librarieslibraries Interactive InterpreterInteractive Interpreter
Jython Syntax OverviewJython Syntax Overview
Loose, Dynamic TypingLoose, Dynamic Typing Modular organizationModular organization Object Oriented – Everything is an ObjectObject Oriented – Everything is an Object Supports classes, methods Supports classes, methods Functional Programming (List Comp.)Functional Programming (List Comp.) Code blocks defined by indentationCode blocks defined by indentation Dynamic dispatch – Reflection made easy!Dynamic dispatch – Reflection made easy! Operator OverloadingOperator Overloading
Jython Syntax BasicsJython Syntax Basics
Built-in Data Types: Built-in Data Types:
Numbers (Integers, Float, Numbers (Integers, Float, Complex)Complex)
Strings (immutable sequences)Strings (immutable sequences)
Sequences (like ArrayList)Sequences (like ArrayList)
Dictionaries (like HashMap)Dictionaries (like HashMap)
Many Advanced Types, such as FileMany Advanced Types, such as File
Jython Syntax: SequencesJython Syntax: Sequences
Jython sequence:Jython sequence:list = [1,2,3,4,5]list = [1,2,3,4,5]list[1:3] list[1:3] -> 2 -> 2list[2:5]list[2:5] -> [2,3,4] #called -> [2,3,4] #called
“Slicing”“Slicing”list[1:3] = ‘two’ -> [1,’two’,3,4,5]list[1:3] = ‘two’ -> [1,’two’,3,4,5]
Versus Java ArrayList:Versus Java ArrayList:java.util.List list = new java.util.ArrayList();java.util.List list = new java.util.ArrayList();list.add(new Integer(1));list.add(new Integer(1));list.add(new Integer(2));list.add(new Integer(2));list.add(new Integer(3));list.add(new Integer(3));
……
Jython Syntax: DictionariesJython Syntax: Dictionaries
Jython dictionary:Jython dictionary:dict = {‘one’: 1, ‘two’: 2, 3: ‘three’}dict = {‘one’: 1, ‘two’: 2, 3: ‘three’}
dict[‘two’] dict[‘two’] -> 2-> 2
dict[3] = “three”dict[3] = “three” -> sets “three”-> sets “three”
versus Java HashMap:versus Java HashMap:java.util.Map map = new java.util.HashMap();java.util.Map map = new java.util.HashMap();
map.put(“one”, new Integer(1));map.put(“one”, new Integer(1));
map.put(“two” new Integer(2));map.put(“two” new Integer(2));
Map.put(new Integer(3), “three”);Map.put(new Integer(3), “three”);
Jython Syntax: Jython Syntax: ConditionalsConditionals
# in Python (and Jython) 0 = false, 1 = true# in Python (and Jython) 0 = false, 1 = true
if if expressionexpression:: blockblockelif elif expressionexpression:: blockblockelse:else: blockblock
try:try: blockblockexcept SomeError:except SomeError: blockblockfinally:finally: blockblock
Jython Syntax: LoopsJython Syntax: Loops
import java.security as secimport java.security as sec
import java.lang as langimport java.lang as lang
providers = sec.Security.getProviders() #sequenceproviders = sec.Security.getProviders() #sequence
for p in providers:for p in providers:
print p.nameprint p.name
print “-----------------------”print “-----------------------”
p.list(lang.System.out)p.list(lang.System.out)
Jython Syntax: F.P.Jython Syntax: F.P.>>> map(lambda x: 2 * x, [1,2,3])>>> map(lambda x: 2 * x, [1,2,3])[2,4,6][2,4,6]
>>> filter(lambda x: x > 0, [-1, 0, 1, 2])>>> filter(lambda x: x > 0, [-1, 0, 1, 2])[1,2][1,2]
>>> reduce(lambda x, y: x + y, [0,1,2,3])>>> reduce(lambda x, y: x + y, [0,1,2,3])66
>>> def func(x,y):>>> def func(x,y):... print "(x,y): (%s,%s)" % (x,y)... print "(x,y): (%s,%s)" % (x,y)... return x + y... return x + y......>>> reduce(lambda x,y: func(x,y), [0,1,2,3])>>> reduce(lambda x,y: func(x,y), [0,1,2,3])(x,y): (0,1)(x,y): (0,1)(x,y): (1,2)(x,y): (1,2)(x,y): (3,3)(x,y): (3,3)66
Jython Syntax: List Comp.Jython Syntax: List Comp.
>>> evens = [x for x in range(10) if x%2==0]>>> evens = [x for x in range(10) if x%2==0]
>>> evens>>> evens
[0,2,4,6,8][0,2,4,6,8]
Jython Syntax: FunctionsJython Syntax: Functions
def listAll(seq):def listAll(seq):
for each in seq:for each in seq:
print eachprint each
……
method = listAllmethod = listAll
list = [1,2,3,4,5]list = [1,2,3,4,5]
method(list)method(list)
Jython Syntax: ClassesJython Syntax: Classes
# from http://www.jython.org/applets# from http://www.jython.org/applets
from java.applet import Appletfrom java.applet import Applet
class HelloWorld(Applet):class HelloWorld(Applet):
def paint(self, g):def paint(self, g):
g.drawString(“Hello World!”, 20, 30)g.drawString(“Hello World!”, 20, 30)
Example TimeExample Time
Jython InterpreterJython Interpreter Eclipse – Red Robin Jython IDEEclipse – Red Robin Jython IDE
http://home.tiscali.be/redrobin/jython/ http://home.tiscali.be/redrobin/jython/
Cool ThingsCool Things
Read a whole file in two lines:Read a whole file in two lines:file = open(“somefilename.txt”, r+)file = open(“somefilename.txt”, r+)
file.read()file.read()
Java API ExplorationJava API Exploration
What is Jython good at:What is Jython good at:
PrototypingPrototyping Java API explorationJava API exploration Bean AccessorsBean Accessors
window = Jframe()window = Jframe()
window.dimension=[200,200]window.dimension=[200,200]
Embedding in Java for extensibilityEmbedding in Java for extensibility
What is Jython NOT good What is Jython NOT good at:at:
Performance Performance criticalcritical code code Anything JVM can not doAnything JVM can not do Access Python Extensions in CAccess Python Extensions in C C libraries without JNIC libraries without JNI COM without JNICOM without JNI
Embedding JythonEmbedding Jython
From page 207, From page 207, Java Essentials Java Essentials ::
import org.python.core.*;import org.python.core.*;
import org.python.util.*;import org.python.util.*;
public class PyInterpTest {public class PyInterpTest {
public static void main(String[] args) {public static void main(String[] args) {
PySystemState.initialize();PySystemState.initialize();
PythonInterpreter interp = new PythonInterpreter()PythonInterpreter interp = new PythonInterpreter()
PyObject value = interp.eval(“2 + 2”);PyObject value = interp.eval(“2 + 2”);
System.out.println(value);System.out.println(value);
}}
}}
Embedding JythonEmbedding Jythonimport org.python.core.*;import org.python.core.*;import org.python.util.*;import org.python.util.*; public class EmbedJython {public class EmbedJython {
public static void main(String[] args) {public static void main(String[] args) {String script = “value = (max – min) / 2”;String script = “value = (max – min) / 2”;PySystemState.initialize();PySystemState.initialize();PythonInterpreter interp = new PythonInterpreter();PythonInterpreter interp = new PythonInterpreter();interp.set(“max”, new PyInteger(10));interp.set(“max”, new PyInteger(10));interp.set(“min”, new PyInteger(0));interp.set(“min”, new PyInteger(0));interp.exec(script);interp.exec(script);System.out.println(interp.get(“value”));System.out.println(interp.get(“value”));
}}}}
Jython PerformanceJython Performance
Calculating first 10000 primesCalculating first 10000 primes1000 100001000 10000 2000020000
Java: Java: .027s.027s 2.566s 2.566s10.196s10.196s
Jython:Jython: .364s.364s 17.804s 17.804s 73.804s73.804s Word Counting the BibleWord Counting the Bible
(816635 words, 35520 unique)(816635 words, 35520 unique) Java:Java: 1.207s1.207s Jython:Jython: 4.969s4.969s
The Bean Scripting The Bean Scripting FrameworkFramework
Provides a framework for Provides a framework for embedding scripting languages embedding scripting languages into any Java applicationinto any Java application
Uses a registry for passing dataUses a registry for passing data Supports many languages (next Supports many languages (next
slide)slide)
Other Scripting Languages Other Scripting Languages for Javafor Java
Groovy – new and increasingly popularGroovy – new and increasingly popularhttp://groovy.codehaus.org/ http://groovy.codehaus.org/
Rhino – ECMAScript interpreterRhino – ECMAScript interpreterhttp://www.mozilla.org/js/ http://www.mozilla.org/js/
BeanShell – “Loose” JavaBeanShell – “Loose” Javahttp://www.beanshell.org http://www.beanshell.org
NetRexx – Rexx on JavaNetRexx – Rexx on Javahttp://www-306.ibm.com/software/awdtools/netrexx/http://www-306.ibm.com/software/awdtools/netrexx/
JudoScript – “A functional scripting language”JudoScript – “A functional scripting language”http://www.judoscript.com/index.html http://www.judoscript.com/index.html
JRuby – Ruby on JavaJRuby – Ruby on Javahttp://jruby.sourceforge.net/ http://jruby.sourceforge.net/
So many more: So many more: http://www.robert-tolksdorf.de/vmlanguages.html http://www.robert-tolksdorf.de/vmlanguages.html
Python CompatibilityPython Compatibility
CPython is now on version 2.4CPython is now on version 2.4 Not all current Python code works Not all current Python code works
with Jythonwith Jython Extensions for CPython are written Extensions for CPython are written
in C and will not work with Jython.in C and will not work with Jython.
ReferencesReferences
Jython Essentials Jython Essentials Pedroni, RappinPedroni, Rappinhttp://www.oreilly.com/catalog/jythoness/http://www.oreilly.com/catalog/jythoness/
An Introduction to JythonAn Introduction to Jython Brian Brian Zimmer Zimmer http://ziclix.com/jython/chipy20050113/slide-01.htmlhttp://ziclix.com/jython/chipy20050113/slide-01.html
URLsURLs
Jython - Jython - http://jython.orghttp://jython.orgBSF - BSF - http://http://jakartajakarta.apache.org/.apache.org/bsfbsf
/index.html/index.htmlFree Python Books - Free Python Books -
http://techbooksforfree.com/perlpython.shhttp://techbooksforfree.com/perlpython.shtmltml
Dive Into Python - Dive Into Python - http://diveintopython.orghttp://diveintopython.orgRed Robin Eclipse Plug-in - Red Robin Eclipse Plug-in -
http://home.tiscali.be/redrobin/jython/http://home.tiscali.be/redrobin/jython/This presentation - This presentation -
http://secosoft.net/presentations/http://secosoft.net/presentations/