14 April 2010

Hello Python Threads

I am looking for something new.

Java and I are going through a relationship crisis, in fact we are considering divorce. She has kept me awake for nights, talking a lot, saying not much. Broken promises, nerves on edge, where has the initial joy gone? Outside people I respect have warned me.

So last week I caught up again with a long gone lover. Her name is Python. It turned out the chemistry is still strong between us. She's now got a VIP membership at AppEngine, which made her even more attractive. She is a well-loved, public person and yet some things seam mysterious about her. Here a first little detail I figured out with some help.

Understanding by example - Python's threads:
Dispatch 4 threads, the first one running the longest with non-busy waiting for completion.

Source code:
import time 
from threading import Thread 
class MyThread(Thread): 
    def __init__ (self, id, seconds): 
        Thread.__init__(self) 
        self.id = id 
        self.seconds = seconds 
    def run(self): 
        print "Start running thread", self.id, "with seconds", self.seconds       
        for c in ("A","B","C"): 
            time.sleep(self.seconds) 
            print "thread", self.id, "at", c 
        print "Exit running thread", self.id, "with seconds", self.seconds      
 
if __name__ == "__main__": 
    myThreads = [] 
    for id in range(4): 
        #thread 0 runs slowest, thread 3 runs fastest 
        seconds = 4-id 
        t = MyThread(id, seconds) 
        myThreads.append(t) 
        t.start() 
 
    for myThread in myThreads: 
        myThread.join() 
        print "Done. ", myThread.id 

Output:
Start running thread 0 with sleep 4
Start running thread 1 with sleep 3
Start running thread 2 with sleep 2
Start running thread 3 with sleep 1
thread 3 at A
thread 2 at A
thread 3 at B
thread 1 at A
thread 3 at C
Exit running thread 3 with sleep 1
thread 0 at A
thread 2 at B
thread 1 at B
thread 2 at C
Exit running thread 2 with sleep 2
thread 0 at B
thread 1 at C
Exit running thread 1 with sleep 3
thread 0 at C
Exit running thread 0 with sleep 4
Done. 0
Done. 1
Done. 2
Done. 3

12 November 2009

Mashup Write-up

It's been an intense week of cooking code. I have entered the result, the NSW Crime Map, in the MashupAustralia Contest. This data visualisation web page is my contribution to the Government 2.0 movement.


First and foremost a big thank you to Jason for his invaluable help on design and user experience questions!

Gov 2.0
Do you feel the buzz? It is the hum of changing times.
Especially in the IT industry. New startups mushroom everywhere, a lot of them target crowd sourcing, leveraging communities and platforms. Others deal with communication, cloud computing, and mobile apps. More than ever before, each voice has got an opportunity to be heard and to enhance democracy.

NSW Crime Map
Data isn't only for analysts. If it is presented appropriately, data is for everyone. Why would only experts be interested in crime incidences?
The NSW Crime Map is intended to be easily comprehensible and visually engaging. It is meant to invite to dig further.
The concept is simple. The NSW Crime Map is a one page web application with a reduced feature set. Less is more.
Growing and shrinking bubbles represent crime rate change. Press the play button, zoom-in on the map, click on bubbles, zoom into the time line chart, skip back and forward in time. Compare different crimes. Have a play.
The NSW Crime Map tells a few stories: liquor offences have steadily increased over time, robbery is particular prominent in urban areas, recorded driving offences have climbed drastically between November and December 2000. I wonder why?

Under the hood
The web application has been created with the Google Web Toolkit, using Google Maps and the Google Visualization API. It is hosted on the Appengine. Thank you Google for making all of this soooo easy.
The source code is available under the Apache License - 2.0. Warning: this is proof-of-concept/meet-the-deadline code. It is ugly.
I used the following data source:
The experience
Half of the effort went into getting data into the right shape.
Scripting, spreadsheets, copy-paste, data bases. Processing the KML data for regional boundaries was particularly difficult. At least I know about polyline encoding and the Douglas-Peuker algorithm now. Maybe my next project is going to be an online KML size reducer and a polyline encoding utility. Or a Polygon merger?
The raw crime data manipulation was tricky, too. It was a bootstrapping problem: On one side it is necessary to aggregate data to understand what you want, on the other side you need to know what you want before you can aggregate data. Space-Time Researches SuperSTAR software came in handy. I also owe my skills in data visualisation and web app building to this company. Thank you Space-Time I love this field.

Stay tuned for more emotional data.