Using SQLite in Python

Python has had support for SQLite built-in since version 2.5.

This is a very convenient pairing as SQLite is an excellent lightweight SQL implementation that I find very useful for a variety of tasks e.g. data mining.  Or any task involving manipulating complex data sets where I’d otherwise end up resorting to using a full blown SQL server like MySQL.

Here is a simple example of using SQLite in Python using it’s built-in sqlite3 module:

import sqlite3

# craete a connection
con = sqlite3.connect('test.db')

# create a cursor
cur = con.cursor()

# create a test table
cur.execute( "CREATE TABLE testTable (myKey INT, myValue INT)" )

# insert some data
for i in range(0,10):
 cur.execute( "INSERT INTO testTable VALUES ( %d, %d )"%(i,i*i) )

# select the data
for row in cur.execute( "SELECT * FROM testTable" ):
 print row

# destroy (drop) our test table
cur.execute( "DROP TABLE testTable" )

# close the connection
con.close()

As you can see Python makes handling SQLite (a C language library) much easier, less error prone, and the resulting code much more compact than SQLite’s native C.

Window Managment on Large Monitors

WinSplit RevolutionI have had a 24″ monitor at work for a while and recently bought myself a 24″ for use as a second monitor on my 17″ iMac at home.  I really enjoy the extra screen real estate that a large monitor with a resolution of 1920×1200 provides.  However most applications don’t really make good use of the massive screen real estate of a large LCD monitor e.g. web browsers viewing fixed width webpages. This leaves you with the problem of how to maximise your usage of your screen real estate, if a single application using the whole display is sub-optimal then viewing two or more applications can be more useful.

The simplest solution to this is to manually position and size the windows of your applications so you can view two or more at once.  Arranging application windows manually quickly becomes tedious, due to the many events that can occur in a modern operating system which cause your application windows to be moved around, re-sized or moved to another monitor.

Size Up Animation (Max OS X)

The solution to this problem is using Window Management utilities which allow you to easily re-size and move application windows around, typically using key combinations.  These utilities exist for most operating systems for Mac OS X the window management utility is called SizeUp, the equivalent utility for the PC is called WinSplit Revolution.  I use both of these applications daily, WinSplit is freeware but SizeUp costs a minimum of $4.99 and its worth every cent.  Each utility has some unique features: WinSplit allows you to chain several window configurations on a single key combination and SizeUp allows you to set up a key combination for moving windows between monitors.

I would struggle to maximise my use of one or more large monitors without a Window Management utility.  Hopefully one day this functionality will be built into operating systems as large monitors become more common.  Until then Window Managment utilties are going to be an essential tool that ever serious power user needs.

Setting mac desktop wallpaper with Python

I have been playing with Python recently.

Here is a little script to change a mac’s desktop wallpaper to the file specified as the first argument of the script:

import subprocess,sys,os

# Raw apple script
Script = """/usr/bin/osascript<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""

# get the file name which is the first argument passed to this script
filename = sys.argv[1] 

# run the apple script inserting the filename
subprocess.Popen(Script%filename,shell=True)

Or using the nifty appscript module for python:

import sys
from appscript import app, mactypes

# get the file name which is the first argument passed to this script
fileName = sys.argv[1]

# use the appscript module to change the desktop wallpaper
app('Finder').desktop_picture.set(mactypes.File(fileName))

The more I use Python the more I like it.  I used to think Python was at a similar level to C# in terms of how high a level a programming language it is.  After using Python for a bit I now realise that Python is a higher level programming language than C#.  Out of the languages I use reguarally it would seem that Python is the highest level language followed by C# then C++ and finally C.