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