SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
Considering Jython

      Juergen Brendel
     Brendel Consulting




     http://brendel.com   @BrendelConsult
Jython



  http://jython.org




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


           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




              http://brendel.com   @BrendelConsult
Jython
% jython
Jython 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_20
Type "help", "copyright", "credits" or "license" for more information.
>>> 




                                  You get a standard
                                     Python shell




                      http://brendel.com   @BrendelConsult
Jython
>>> from java.util import HashMap
>>> 



                                Very easy to import any Java
                             classes you have in your classpath




                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 than
                                                      in Java!




                    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'>
>>>




                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}
                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 simple
                                                                class.


               http://brendel.com   @BrendelConsult
Using Java classes: Easy
>>> import Foo
>>> f = Foo()
>>> type(f)                           Easy to use from
<type 'Foo'>                           within Python
>>>




            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'
>>>




            http://brendel.com   @BrendelConsult
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
                                              object's Java methods.




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

            http://brendel.com   @BrendelConsult
Use Python in Java?
public class Foo
{
    ...

    public String className(Object x)
    {
        Class c = x.getClass();
        return c.getName();
    }

    ...
}

            http://brendel.com   @BrendelConsult
Use Python in Java?
>>> f = Foo()
>>> f.className(123)
u'java.lang.Integer'
>>>




            http://brendel.com   @BrendelConsult
Use Python in Java?
>>> f = Foo()                   On the Java side you
                              see Java types or some
>>> f.className(123)           Java representation of
u'java.lang.Integer'             the Python object
>>>
>>> f.className(dict())
u'org.python.core.PyDictionary'
>>>




                http://brendel.com   @BrendelConsult
Your Python in Java?




     http://brendel.com   @BrendelConsult
Your Python in Java?
●   Java likes it static!




                    http://brendel.com   @BrendelConsult
Your Python in Java?
●   Java likes it static!

●   Create static contract in Java:
         class
         abstract class
         interface




                    http://brendel.com   @BrendelConsult
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
Create Python in Java




     http://brendel.com   @BrendelConsult
Create Python in Java



     Yikes!
                                        Using Python from
                                        within Java is much
                                        easier than creating
                                          Python objects.




     http://brendel.com   @BrendelConsult
Create Python in Java
// Get the handle on the Jython interpreter
PythonInterpreter interp = new PythonInterpreter();

// Tell Jython to import something from our Python package
interp.exec("from test_package import MyPyFooBar");        Basically, use some
                                                                 copy and paste to get this
// Get the object representing the Python class
PyObject 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 class
FooBarInterface javaObject =
               (FooBarInterface)pyObject.__tojava__(FooBarInterface.class);

// Now the object can be used just as if it were a native Java object
String result = javaObject.foobar("some", "arguments", 123);
System.out.println("Received from Python: " + result);

                          http://brendel.com   @BrendelConsult
More points to remember



java.lang.Exception != exceptions.Exceptions


                                           Lots of fun when you
                                                forget that.




              http://brendel.com   @BrendelConsult
More points to remember


   No dynamic import of Java classes

    (eval() or conditional import)




          http://brendel.com   @BrendelConsult
The End


juergen@brendel.com

 @BrendelConsult

http://brendel.com


  http://brendel.com   @BrendelConsult

Contenu connexe

Tendances

Playing with Java Classes and Bytecode
Playing with Java Classes and BytecodePlaying with Java Classes and Bytecode
Playing with Java Classes and Bytecode
Yoav Avrahami
 

Tendances (20)

Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Playing with Java Classes and Bytecode
Playing with Java Classes and BytecodePlaying with Java Classes and Bytecode
Playing with Java Classes and Bytecode
 
Python testing using mock and pytest
Python testing using mock and pytestPython testing using mock and pytest
Python testing using mock and pytest
 
Py.test
Py.testPy.test
Py.test
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Implementing a decorator for thread synchronisation.
Implementing a decorator for thread synchronisation.Implementing a decorator for thread synchronisation.
Implementing a decorator for thread synchronisation.
 
Testes pythonicos com pytest
Testes pythonicos com pytestTestes pythonicos com pytest
Testes pythonicos com pytest
 
Advanced java interview questions
Advanced java interview questionsAdvanced java interview questions
Advanced java interview questions
 
Python functions (menard maranan)
Python functions (menard maranan)Python functions (menard maranan)
Python functions (menard maranan)
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Learning puppet chapter 3
Learning puppet chapter 3Learning puppet chapter 3
Learning puppet chapter 3
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Project Coin
Project CoinProject Coin
Project Coin
 
PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtime
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
J2ee standards > CDI
J2ee standards > CDIJ2ee standards > CDI
J2ee standards > CDI
 
Java object oriented programming - OOPS
Java object oriented programming - OOPSJava object oriented programming - OOPS
Java object oriented programming - OOPS
 
Abstract factory
Abstract factoryAbstract factory
Abstract factory
 
Integration testing with spring @snow one
Integration testing with spring @snow oneIntegration testing with spring @snow one
Integration testing with spring @snow one
 

En vedette

Network programming in python..
Network programming in python..Network programming in python..
Network programming in python..
Bharath Kumar
 

En vedette (10)

SyPy IronPython
SyPy IronPythonSyPy IronPython
SyPy IronPython
 
Mixing Python and Java
Mixing Python and JavaMixing Python and Java
Mixing Python and Java
 
Jython: Integrating Python and Java
Jython: Integrating Python and JavaJython: Integrating Python and Java
Jython: Integrating Python and Java
 
What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.
 
The .NET developer's introduction to IronPython
The .NET developer's introduction to IronPythonThe .NET developer's introduction to IronPython
The .NET developer's introduction to IronPython
 
Communication between Java and Python
Communication between Java and PythonCommunication between Java and Python
Communication between Java and Python
 
Network programming in python..
Network programming in python..Network programming in python..
Network programming in python..
 
Python Network Programming
Python Network ProgrammingPython Network Programming
Python Network Programming
 
快快樂樂學 Angular 2 開發框架
快快樂樂學 Angular 2 開發框架快快樂樂學 Angular 2 開發框架
快快樂樂學 Angular 2 開發框架
 
Jython
JythonJython
Jython
 

Similaire à Jython

Making the most of your Test Suite
Making the most of your Test SuiteMaking the most of your Test Suite
Making the most of your Test Suite
ericholscher
 

Similaire à Jython (20)

Introduction of Pharo 5.0
Introduction of Pharo 5.0Introduction of Pharo 5.0
Introduction of Pharo 5.0
 
Con-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistCon-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With Javassist
 
Taking User Input in Java
Taking User Input in JavaTaking User Input in Java
Taking User Input in Java
 
CakePHP 2.0 - It'll rock your world
CakePHP 2.0 - It'll rock your worldCakePHP 2.0 - It'll rock your world
CakePHP 2.0 - It'll rock your world
 
Python Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | EdurekaPython Interview Questions And Answers 2019 | Edureka
Python Interview Questions And Answers 2019 | Edureka
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
Making the most of your Test Suite
Making the most of your Test SuiteMaking the most of your Test Suite
Making the most of your Test Suite
 
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code! A beginner's guide to Annotation Processing - Ja...Write code that writes code! A beginner's guide to Annotation Processing - Ja...
Write code that writes code! A beginner's guide to Annotation Processing - Ja...
 
Write code that writes code!
Write code that writes code!Write code that writes code!
Write code that writes code!
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & Generators
 
JavaFX JumpStart @JavaOne 2016
JavaFX JumpStart @JavaOne 2016JavaFX JumpStart @JavaOne 2016
JavaFX JumpStart @JavaOne 2016
 
From Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practiceFrom Java to Kotlin - The first month in practice
From Java to Kotlin - The first month in practice
 
B.Sc. III(VI Sem) Advance Java Unit2: Appet
B.Sc. III(VI Sem) Advance Java Unit2: AppetB.Sc. III(VI Sem) Advance Java Unit2: Appet
B.Sc. III(VI Sem) Advance Java Unit2: Appet
 
Le Tour de xUnit
Le Tour de xUnitLe Tour de xUnit
Le Tour de xUnit
 
Java cheat sheet
Java cheat sheet Java cheat sheet
Java cheat sheet
 
Startup Camp - Git, Python, Django session
Startup Camp - Git, Python, Django sessionStartup Camp - Git, Python, Django session
Startup Camp - Git, Python, Django session
 
Introduction to jython
Introduction to jythonIntroduction to jython
Introduction to jython
 
Compose Camp Session 1.pdf
Compose Camp Session 1.pdfCompose Camp Session 1.pdf
Compose Camp Session 1.pdf
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
CakePHP - The Path to 2.0
CakePHP - The Path to 2.0CakePHP - The Path to 2.0
CakePHP - The Path to 2.0
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 

Jython