Python Quicksort

January 18th, 2007

Here is simple quick sort function written in Python to sort a list of numbers.

def qsort(toSort):

# return the list if only one is left
if len(toSort) < = 1:

return toSort

# set the value to pivot around
pivot = toSort[0] # initialize lists
less = []
greater = []
# sort the list
# skip the first value
for item in toSort[1:]:

if item < = pivot:

less.append(item)

else:

greater.append(item)

# recurse on both lists
r = qsort(less)
r.append(pivot)
r.extend(qsort(greater))
return r

# test
x = [1,99,7,0,7,7,7,99999,8,9,14,74,88]
print qsort(x)

This example is not very useful since python lists have a built in sort function, but should you have a list of tuples that you wanted to sort this function could very easily be modified to work with a value within the tuple that it pulls out instead of sorting by just the item itself.

Python and MySQL

January 18th, 2007

A MySQL database can be queried from a python script with MySQLdb. Just download it here and install it, or if you are using FreeBSD it can be found under

/usr/ports/databases/py-MySQLdb.

In the python script import the MySQLdb module and declare a connection in cursor as in this example:

import MySQLdb
# Create a connection using your db info
db = MySQLdb.connect(host=”localhost”, user=”yourMysqlUser”, passwd=”youUserPass”,db=”yourDB”)
# Create a cursor, this can be used over and over
cursor = db.cursor()
# Build a query
q = “select user_id, user_name from users”
# run the query
cursor.execute(q)
# fetch the results
users = cursor.fetchall()
# loop through the results
for user in users:

print “ID %s Name: %s” % (user[0],user[1])

The cursor is created once from the connection and can be used for all queries in the script. The query selects two columns from the table and then script loops through them and prints them out. Each column is referenced by a number zero based as it comes back as a list. The order is set by the query.

Deploying GWT applications

January 16th, 2007

Once you have built your Google Web Toolkit application run the -compile.cmd script, or hit the compile/browse button in the debugger. It will open in a browser window and you will be able to double check that it works properly as javascript. At this point you should also double check that it looks fine in multiple browsers such as IE/FireFox/Opera.

The compiled files will be written to the www/com.your.app in the GWT directory. Open this directory and you will notice several .cache.html and .cache.xml files, as well as the gwt.js file. There will also be several image files. You will need need all of these except tree_closed.gif, tree_open.gif, and tree_white.gif if you did not use a tree in your application.

After compiling out your Google Web Toolkit Ajax application everything is in the same www directory and works great. But what if you want to move all the GWT files to a different directory then the page you are calling it from? To do this you need to update 2 to 3 parts of your page that calls in the app.

First change to include the directory and an equals sign. For example, if I put my GWT app in a directory called webapps I would have the following:

Second change path of the gwt.js file to include the new directory as you normally would for a javascript file.

Third if you included the history.html file change the path on the iframe also to include the directory. If you do not wish to include history support you may leave the iframe out all together.