td mxc python wierzbiki
TRANSCRIPT
-
8/14/2019 TD MXC Python Wierzbiki
1/21
1
Developer Productivity withPython and Jython
1
-
8/14/2019 TD MXC Python Wierzbiki
2/21
Sun Confidential: Internal Only 2
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
-
8/14/2019 TD MXC Python Wierzbiki
3/21
Sun Confidential: Internal Only 3
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 toTiobe (http://www.tiobe.com)
-
8/14/2019 TD MXC Python Wierzbiki
4/21
Sun Confidential: Internal Only 4
What is Jython?
Jython brings the Python language to the JVM.
Jython has full and nearly seamless integration intoany Java libraries and code.
Jython can access many of the libraries andframeworks written in Python.
-
8/14/2019 TD MXC Python Wierzbiki
5/21
Sun Confidential: Internal Only 5
Some code
print hello world
def hello(name):
print hello, name
-
8/14/2019 TD MXC Python Wierzbiki
6/21
Sun Confidential: Internal Only 6
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
-
8/14/2019 TD MXC Python Wierzbiki
7/217
Demo: Jython installationand basics
7
-
8/14/2019 TD MXC Python Wierzbiki
8/21Sun Confidential: Internal Only 8
Jython and JDBC
Jython has built in support for DB-API, Python'sstandard database interface in the zxJDBCpackage.
zxjdbc is a thin wrapper around JDBC, Java'sstandard database interface.
Provides Python programmers with access to anydatabase with a JDBC driver.
Provides Java programmers with access to anyPython frameworks that are built on DB-API
-
8/14/2019 TD MXC Python Wierzbiki
9/21Sun Confidential: Internal Only 9
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()
-
8/14/2019 TD MXC Python Wierzbiki
10/21Sun Confidential: Internal Only 10
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
-
8/14/2019 TD MXC Python Wierzbiki
11/2111
Demo: Database Access fromJython
11
-
8/14/2019 TD MXC Python Wierzbiki
12/21Sun Confidential: Internal Only 12
Django
A Python MVC framework with a web and databasebias (similar to Ruby on Rails)
Makes creating a project very simple
Comes with a powerful admin tool that can be usedto manage the data in your database no need towrite your own admin tool!
Very clean and simple design, easy to read andwrite.
-
8/14/2019 TD MXC Python Wierzbiki
13/2113
Demo: Django on Jython
13
-
8/14/2019 TD MXC Python Wierzbiki
14/21Sun Confidential: Internal Only 14
Hibernate From Jython
Hibernate is a ORM written in Java
It can be accessed as EJB3
Jython can be used
-
8/14/2019 TD MXC Python Wierzbiki
15/21Sun Confidential: Internal Only 15
EJB3 From Jythonfrom 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();
-
8/14/2019 TD MXC Python Wierzbiki
16/2116
Demo: Hibernate/EJB3 FromJython
16
-
8/14/2019 TD MXC Python Wierzbiki
17/21Sun Confidential: Internal Only 17
SQLAlchemy
SQLAlchemy is a ORM written in pure Python
Written by Michael Bayer who consults onHibernate by day
Utilizes Python's strengths to create very clear ORMcode
-
8/14/2019 TD MXC Python Wierzbiki
18/21Sun Confidential: Internal Only 18
SQLAlchemy Code Examplefrom 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
-
8/14/2019 TD MXC Python Wierzbiki
19/21Sun Confidential: Internal Only 19
Where to find out more
http://www.jython.org
http://fwierzbicki.blogspot.com
Twitter: fwierzbicki
http://www.python.org
-
8/14/2019 TD MXC Python Wierzbiki
20/21
Sun Confidential: Internal Only 20
El Zen de Python
1. Hermoso es mejor que feo. 2. Explcito es mejor que implcito.
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 especialescomo para romper las reglas.
9. Aunque lo pragmtico gana a la pureza.
10. Los errores nunca deberan dejarse pasarsilenciosamente.
-
8/14/2019 TD MXC Python Wierzbiki
21/21
S C fid ti l I t l O l
11. A menos que se silencien explcitamente.
12. Cuando te enfrentes a la ambigedad, rechaza la tentacin
de adivinar. 13. Debera haber una -- y preferiblemente slo una -- manera
obvia de hacerlo.
14. Aunque puede que no sea obvia a primera vista a menos
que seas holands. (NT: Guido van Rossum es holands) 15. Ahora es mejor que nunca.
16. Aunque muchas veces nunca es mejor que *ahora mismo*.
17. Si la implementacin es difcil de explicar, es una mala idea. 18. Si la implementacin es sencilla de explicar, puede que sea
una buena idea.
19. Los espacios de nombres son una gran idea -- tengamos
ms de esas!