Wednesday, September 16, 2009

Jython 2.5.1rc2 - zxJDBC with-statement

This past weekend (09/12/2009), the Jython developer team rolled out the second release candidate for version 2.5.1. In the release were many bug fixes and some additional features...I definitely recommend downloading it and trying it out if you haven't already done so. Thanks to all of the developers who have made this release possible.

One of the features added in this release for database work with the zxJDBC API is with-statement compatibility. Thanks to Jim Baker for updating the PyConnection and PyCursor objects in Jython so that it can now be used in the context of a with-statement. This can be useful for maintaining resources and transactional state when working with databases.

To use the new feature, you must import the closing() function from contextlib so that the connection can be bound to a variable and closed at the end of the statement. Use of this feature works as follows:


# In this example, I am connecting to a postgresql database.
# Of course, we could connect to the database of our choice so long
# as we have the appropriate JDBC driver in our CLASSPATH

>>> from __future__ import with_statement
>>> from com.ziclix.python.sql import zxJDBC
>>> from contextlib import closing
>>> jdbc_url = "jdbc:postgresql:mydatabase"
>>> username = "postgres"
>>> password = "mypass"
>>> driver = "org.postgresql.Driver"
>>> with closing(zxJDBC.connect(jdbc_url, username, password, driver)) as conn:
... with conn:
>>> # Use one or more cursors within the context of the with-statement
... with conn.cursor() as c:
... c.execute("select name from country")
... c.fetchone()
...
(u'Afghanistan',)


In the example above, I queried a database table. However, we could use the cursor in any way that we'd like within the context of the statement. Use for transaction management and if an error occurs within a transaction then everything is rolled back.


>>> with closing(zxJDBC.connect(jdbc_url, username, password, driver)) as conn:
... with conn:
... with conn.cursor() as c:
... try:
... stmt = "insert into country values (?,?)"
... # the following produces an error
... c.executemany(stmt, ['test'])
... # perform more inserts and updates
... conn.commit()
... except Exception, err:
... raise Exception("There has been a zxJDBC error")


Transaction management is taken care of such that if a statement fails, then the complete transaction is rolled back. This is very useful for transaction management. If you attempt to work with the cursor or connection outside of the statement then it will not work because the statement closes them after use. That is the beauty of using the with-statement for resource management. The with-statement is also nice for working with files and such in a similar manner.

For more information on the use of the with_statement, please visit PEP-343 or the open source Jython Book (In Progress) which will soon be updated to illustrate correct usage of with-statement context management for zxJDBC in 2.5.1.

Enjoy!

Thursday, July 02, 2009

The Definitive Guide to Jython - Open Source Version

Start reading the open source version of 'The Definitive Guide to Jython'. This book is currently being authored by Frank Wierzbicki, Jim Baker, Leo Soto, Vic Ng, and myself. It is due to be published by Apress in the fall of 2009. I am having a great time being part of the project and really appreciate the opportunity! I thank Jim Baker for contacting me in regards to the book and giving me the opportunity to become the lead author.

The book is available in restructured text format on the kenai.com website as the jythonbook project. You will find the Sphinx formatted book here for the time being, but we will migrate it to jythonbook.com in the near future. We encourage comments and suggestions.

A special thanks to James Gardner for his help on providing us with the tools to convert the book into restructured text format. I plan to have a post dedicated to my work with converting from Word to rst and vice versa sometime in the near future.

Our book is focusing on the 2.5 release of Jython, which you can download here. It goes through the Python language basics through some advanced concepts. The book then goes into Jython-specific topics such as java integration and the like.

We hope you will find this book useful. Stay tuned for more to follow on the book...

Tuesday, June 16, 2009

Jython 2.5.0 Has Been Released

Jython 2.5.0 has been released! This release includes many new language features for Jython bringing it inline with more modern releases of Python. This is a big release and I congratulate all of the developers!

Go and download today from : http://www.jython.org

Friday, April 10, 2009

Netbeans Articles

James Branam has recently turned a couple of my Python/Jython tutorials for Netbeans 6.5 into Netbeans.org articles. Check them out:

Developing a Jython App in Netbeans

Python Quickstart

If you haven't tried Netbeans out for Jython or Python development, now would be the time...it is great!

Sunday, March 29, 2009

The Definitive Guide to Jython...Fall 2009

A new Jython book will be published by Apress this fall entitled 'The Definitive Guide to Jython with Django'. Authors include Jim Baker, Frank Wierzbicki, Leo Soto, Victor Ng, and myself.

This book will be of interest to those beginning Jython and also to advanced users. We will also maintain an open source version of the book online.

I'm looking forward to working with these talented individuals and assisting in the documentation of Jython.

Tuesday, February 24, 2009

EMD Upload Error: Oracle Enterprise Management Agent

I came into work on Monday and saw that all of the databases which are running on our development database server were no longer reporting to our Oracle Grid Control. After looking at the Grid Control server, I realized that the management agent was not functioning correctly. Time to investigate...

I went to the command line and issued the following:


emctl status agent



Everything looked fine, but then I realized that the "Collection Status" indicated that it had been disabled by the collection manager. In my case, this had occurred because over the weekend I had been moving some files around and filled up one of the disks on the server completely. Realizing I had done so, I immediately removed some of those files to free up disk space. Unfortunately, this disk which filled up was the same one on which the Oracle Management Agent is installed. Being that the disk filled up and that the collection manager was not able to add the required XML files to disk, it immediately disabled itself.

The remedy: Of course, ensure that the disk has available space. Once you've done so, simply restart the management agent. It will begin working immediately.

Thursday, February 05, 2009

Developing Django-Jython Using Netbeans

If you are interested in using Django on Jython, then you will need to incorporate the Django-Jython project into your Jython installation after Django has been installed. The Django-Jython project facilitates the use of Jython's zxJDBC database infrastructure in order for Django to access database backends. At this time, the only fully supported database is Postgresql, but there are also experimental backends for using SQL Server, sqllite, and Oracle under development. In order to use Django on Jython with another database or to assist in the development of these backends, you will need to download the Django-Jython project source code and write your own backend or update one which already exists.

Working with the Django-Jython project using a text editor is one way to go, but if you are used to the automation and simplicity of using an IDE, text editing can be a painful experience. This blog will walk you through the steps of setting up an environment within Netbeans 6.5 for developing the Django-Jython project.

The process of development with Django-Jython is as follows:

1. Download source for project

2. Add/Modify source code to facilitate your needs

3. Build the source and install it into your Jython home

Number 1 above is easy enough, just visit the Django-Jython project page. However, numbers 2 and 3 are manually performed processes. You can help to automate these processes by using the power of Netbeans. Simply follow the procedure below to set up the environment:

1) Create a Python project with existing sources and name it something like "DjangoJython"




2) Add the root-level directory of the "django-jython" source folder to the "Source Root Folders" window area when creating the project, then click finish.


At this point your project will be created and you can work with the source. However, we need a way to tell the IDE to build and install the project. You can either use the command-line to do so by traversing to the project folder and issuing:

jython setup.py build, followed by, jython setup.py install

Or, you can perform have Netbeans IDE build the project for you when you invoke the "Run" process on the project by performing step 3.

3) Right-click on the project, and choose "Properties". Select the "Run" category and then enter "setup.py" as the main module and enter "install" into the application arguments text field. Now, each time you elect to "run" the project, it will cause netbeans to build and install it.




The only caveat to using this method of building and installing Django-Jython is that you will need to manually delete the "build" directory in your DjangoJython Netbeans project as well as delete the "doj" directory from your Jython installation (Jython-Path/Lib/site-packages/doj) prior to issuing "run" each time. I have found that if you do not, sometimes all of the updated modules are not reinstalled. Deleting these directories prior to running your build/install forces a new build and install to take place each time.

Now, enjoy the great development experience of Netbeans while contributing to a great project...Django-Jython! Have fun!

Wednesday, January 21, 2009

Django-Jython: Working With Experimental Database Backends - Part 1

This is the first of several blogs that I plan to publish regarding the use of experimental database backends with the Django-Jython project. In this series of blogs, I will cover the basics of how to begin using the experimental backends, as well as some details on how to create your own. At the time of this posting, only one "fully supported" backend exists for the Django-Jython project and that is for the PostgreSQL database. However, another experimental backend exists for SQLLite and there are more on the way.

In order to build and begin using the experimental backend for SQLLite (or others), you must first obtain a copy of the code, which is available on the project site, or via your command-line using the following line:

svn checkout http://django-jython.googlecode.com/svn/trunk/ django-jython-read-only

Once you've obtained the code, it is easy to build. Simply traverse into the root directory of the downloaded code which should be named "django-jython", and perform the build using the setup.py build script as such:

jython setup.py build

Now, this works nicely for a build to use the PostgreSQL backend, but it will not include the experimental backend implementations. In order to do so, you must open up the setup.py script and add the experimental backend that you wish to use within the "packages" listing as follows:


from distutils.core import setup
setup(
name = "django-jython",
version = "0.9",
packages = ['doj',
'doj.backends',
'doj.backends..zxjdbc',
'doj.backends..zxjdbc.postgresql',
'doj.backends..zxjdbc.sqllite', # Experimental backend
'doj.management',
'doj.management.commands'],
...



Once you've added this to the setup.py script, it will cause the experimental code to be built the next time the build process is invoked. Now you can begin using the new backend.

Next time I plan to delve into the process of creating your own experimental backend for the Django-Jython project!

Sunday, January 11, 2009

Jython 101 - Altering Tuple Values

Often times we use lists and tuples to hold data that will be used at some later point in an application. When we iterate through the data within them without changing it then they work without any issues. However, if we wish to change one or two of the values that are contained in a tuple then we may run into an issue because they are immutable in the Jython world. Lists are a bit different as they can be altered. If you've run into this issue with tuples and need an easy solution, then hopefully the technique which I am about to describe will help you.

The Problem

Let's say we have a tuple of parameters that we wish to pass to another Jython module at some later point. However, once the receiving function obtains the tuple of parameters, it is found that a value needs to be added to it in order to make it complete. How do we add or insert a value into a tuple if they are immutable?


The Solution


Quite easily actually, the answer is that we define a list that will take the values of the tuple and pass it on.

For instance, lets define tuple:

x = ("one", "two", "four")

print x
['one', 'two', four']


If we try to alter the tuple and insert the value "three" before the four, we receive an error:


x[2] = "three"

TypeError: can't assign to immutable object



Therefore, a great workaround is to define an empty list and populate it with the contents of the tuple. At the same time, you can add, change, or remove any elements that you wish. In the end, use the newly define list as your "new" parameter list and pass it on.



# define empty list
y = []

# iterate through tuple and populate new list

for elem in x:

# check element to see if it's contents need to be changed, or moved
# in this case, we are looking for an element with the value "four"
# so that we can insert "three" in front of it

if elem == "four":

# Insert the new element, and then the current element
y.append("three")
y.append(elem)
else:
# Insert the current element
y.append(elem)

print y # Use the new list and continue.
['one', 'two', 'three', 'four']




I hope that you find this tip for using Jython tuples useful.

Joined the Netbeans Community Docs Team

I have joined the Netbeans Community Docs team recently. This is a great program that has been put together to allow the Netbeans community to help document and provide guides for using the IDE. I hope that I am able to add some benefit to the team by providing tutorials, articles, and documentation from time to time. I am also going to be compiling and editing the monthly newsletter for the group.

Stay tuned for more Netbeans-related blog entries focused on using the IDE with all of the technologies I love...including (but not limited to) Java, JavaEE, Jython, Groovy, JavaFX, Swing, SQL...and more!

Sunday, January 04, 2009

Web Beans 1.0.0 Alpha Released

JBoss has released Web Beans 1.0.0 Alpha recently. Quoted from the Web Beans site:

"Web Beans defines a set of services for the Java EE environment that makes applications much easier to develop. Web Beans layers an enhanced lifecycle and interaction model over existing Java component types including JavaBeans and Enterprise Java Beans. As a complement to the traditional Java EE programming model, the Web Beans services provide:

* an improved lifecycle for stateful components, bound to well-defined contexts,
* a typesafe approach to dependency injection,
* interaction via an event notification facility, and
* a better approach to binding interceptors to components, along with a new kind of interceptor, called a decorator, that is more appropriate for use in solving business problems.

Web Beans is especially useful in the context of web applications, but is applicable to many different kinds of applications and may even be used in the Java SE context, in conjunction with an embeddable EJB Lite container, as defined in the EJB 3.1 specification. "

I'll be checking this out soon...