Developer Productivity with Python and Jython
1
Me • Frank Joseph Wierzbicki (
[email protected]) • Project lead for Jython • Java Developer for 10 years • Python/Jython Developer for 10 years • Contributor to Jython for 4+ years
Sun Confidential: Internal Only
2
What is Python? • A programming language that is: > Elegant and Robust > Powerful and as applicable as traditional compiled
languages > Easy to pick up, readability is at the forefront of the language design > Easy to use, yet powerful
• The fastest growing language of 2007 according to Tiobe (http://www.tiobe.com)
Sun Confidential: Internal Only
3
What is Jython? • Jython brings the Python language to the JVM. • Jython has full and nearly seamless integration into any Java libraries and code. • Jython can access many of the libraries and frameworks written in Python.
Sun Confidential: Internal Only
4
Some code print “hello world” def hello(name): print “hello”, name
Sun Confidential: Internal Only
5
Some Users of Python and Jython • Python > Google and YouTube > The new OpenSolaris packaging system (IPS) > Ubuntu, Red Hat, etc for system utilities
• Jython > IBM WebSphere for admin scripting > BEA Weblogic for admin scripting > Testing engines like PushToTest and the Grinder
Sun Confidential: Internal Only
6
Demo: Jython installation and basics
7
Jython and JDBC • Jython has built in support for DB-API, Python's standard database interface in the zxJDBC package. • zxjdbc is a thin wrapper around JDBC, Java's standard database interface. • Provides Python programmers with access to any database with a JDBC driver. • Provides Java programmers with access to any Python frameworks that are built on DB-API
Sun Confidential: Internal Only
8
Basic Database Access from com.ziclix.python.sql import zxJDBC
db = zxJDBC.connect("jdbc:mysql://localhost/test", 'user', 'pass', "org.gjt.mm.mysql.Driver") cursor = db.cursor() cursor.execute("select name from user") for row in cursor.fetchall(): cursor.close() db.close()
Sun Confidential: Internal Only
9
Swing from Jython from javax.swing import JTable from javax.swing import JFrame rowdata = [('bill', 'Bill Williams')] colnames = ['user name', 'full name'] table = JTable(rowdata, colnames) frame = JFrame("Table") frame.getContentPane().add( table ) frame.size = 400, 300 frame.visible = 1
Sun Confidential: Internal Only
10
Demo: Database Access from Jython
11
Django • A Python MVC framework with a web and database bias (similar to Ruby on Rails) • Makes creating a project very simple • Comes with a powerful admin tool that can be used to manage the data in your database – no need to write your own admin tool! • Very clean and simple design, easy to read and write.
Sun Confidential: Internal Only
12
Demo: Django on Jython
13
Hibernate From Jython • Hibernate is a ORM written in Java • It can be accessed as EJB3 • Jython can be used
Sun Confidential: Internal Only
14
EJB3 From Jython
from javax.persistence import * from hello import Continent # A Java class emf = Persistence.createEntityManagerFactory( "hibworld"); newEm = emf.createEntityManager(); newTx = newEm.getTransaction(); newTx.begin(); continents = newEm.createQuery('select c from Continent c \ order by c.name asc').getResultList(); print continents.size(), " message(s) found:" for c in continents: print c.name; newTx.commit(); newEm.close(); emf.close(); Sun Confidential: Internal Only 15
Demo: Hibernate/EJB3 From Jython
16
SQLAlchemy • SQLAlchemy is a ORM written in pure Python • Written by Michael Bayer who consults on Hibernate by day • Utilizes Python's strengths to create very clear ORM code
Sun Confidential: Internal Only
17
SQLAlchemy Code Example from sqlalchemy import * db = create_engine('mysql://mydb:mydb@localhost/mydb') metadata = MetaData(db) continents = Table('continent', metadata, Column('id', Integer, primary_key=True), Column('name', String(40)), ) s = continents.select() r = s.execute() for continent in r: print continent.name
Sun Confidential: Internal Only
18
Where to find out more • • • •
http://www.jython.org http://fwierzbicki.blogspot.com Twitter: fwierzbicki http://www.python.org
Sun Confidential: Internal Only
19
El Zen de Python 1. Hermoso es mejor que feo. 2. Explícito es mejor que implícito. 3. Simple es mejor que complejo. 4. Complejo es mejor que complicado. 5. Plano es mejor que anidado. 6. Disperso es mejor que denso. 7. La legibilidad cuenta. 8. Los casos especiales no son suficientemente especiales como para romper las reglas. • 9. Aunque lo pragmático gana a la pureza. • 10. Los errores nunca deberían dejarse pasar silenciosamente. • • • • • • • •
Sun Confidential: Internal Only
20
• 11. A menos que se silencien explícitamente. • 12. Cuando te enfrentes a la ambigüedad, rechaza la tentación de adivinar. • 13. Debería haber una -- y preferiblemente sólo una -- manera obvia de hacerlo. • 14. Aunque puede que no sea obvia a primera vista a menos que seas holandés. (NT: Guido van Rossum es holandés) • 15. Ahora es mejor que nunca. • 16. Aunque muchas veces nunca es mejor que *ahora mismo*. • 17. Si la implementación es difícil de explicar, es una mala idea. • 18. Si la implementación es sencilla de explicar, puede que sea una buena idea. • 19. Los espacios de nombres son una gran idea -- ¡tengamos más de esas! Sun Confidential: Internal Only
21