Skip to content

New look for new posts

You may have noticed that I’ve changed the visual theme for this blog to something more minimalist. I have also removed most of the widgets, which I am going to replace with some plain old links to me on other sites.

I did this in preparation for getting my act and starting writing again, hopefully new posts should start appearing this week.

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.

Habits

Habits and habit forming can be an interesting aspect of human behavior: they seem to fall in to two broad categories in my mind.  Habits are either hard to start and maintain until critical mass is achieved and after that can still require conscious maintenance to maintain. Or habits are easy to start but risk becoming all consuming: absorbing more and more time and energy.

An example of a hard to start habit for me is writing blog posts: it took a force of will to start me writing initially and it requires constant energy to keep me writing new posts.  Interruptions over even a week to my writing can be enough to disrupt my writing habit sufficiently to halt it.  At which point Inertia comes into play with devastating effect, to make the restarting of the habit hard.  It seems that a lot of virtuous habits like regular exercise and eating healthily also fall into this category.

A habit that is easy to start and yet easily becomes all consuming for me is playing computer games, usually this doesn’t get too out of hand as most games have a finite length and amount of content which limits the duration of the disruption.  Yet some games, especially MMOs like World of Warcraft (WoW) have a seemingly endless stream of content which means that it is possible to spend an almost infinite amount of time playing the game and still not complete it.

These ‘time sink’ games can be a real challenge to getting anything else done, my usual solution to this situation is to stop playing the game by unsubscribing…