November 17, 2013

Gource Visualization of Ubuntu Touch Core Apps Development

TLDR: I made a cool version control visualization of all the Ubuntu Touch Core Apps.

The video:

* Warning: abrasive techno music
* To be watched in HD, preferably at maximum volume

Making Gource visualizations of complex software projects is awesome. I love seeing a VCS commit log come to life as blooming trees and swarming workers. Normally, I do a visualization video of a single repository. But in this case, I used a bash script to create a visualization of multiple source code repositories. I wanted to see the progress of the entire stack of Ubuntu Touch Core Apps (17 projects). Ubuntu Touch Core Apps is an umbrella project for all [17] of the core apps that are available in Ubuntu on mobile devices

The Ubuntu Touch Core Apps:

  • Dropping Letters
  • Evernote Online Accounts plugin
  • QtDeclarative bindings for the Grilo media scanner
  • Stock Ticker App
  • Sudoku App
  • Ubuntu Calculator App
  • Ubuntu Calendar App
  • Ubuntu Clock App
  • Ubuntu Document Viewer App
  • Ubuntu E-mail App
  • Ubuntu Facebook App
  • Ubuntu File Manager App
  • Ubuntu Music App
  • Ubuntu Phone Commons
  • Ubuntu RSS Feed Reader App
  • Ubuntu Terminal App
  • Ubuntu Weather App

Making the visualization:

Assuming you have a bunch of source code repositories already branched/cloned locally, here is a general version of the script to generate visualization videos of multiple projects/repositories:

The script I used to create the Ubuntu Touch Core Apps video:

October 22, 2013

deadsnakes - Using Old Versions of Python on Ubuntu

How do you install an older version of Python on Ubuntu without building it yourself?

The Python packages in the official Ubuntu archives generally don't go back all that far, but people might still need to develop and test against these old Python interpreters. Felix Krull maintains a PPA (package archive) of older Python versions that are easy to install on Ubuntu.


Currently supported Python releases: 2.4, 2.5, 2.6, 2.7, 3.1, 3.2, 3.3


Add the deadsnakes repository:

$ sudo add-apt-repository ppa:fkrull/deadsnakes

Run Update:

$ sudo apt-get update

Install an older version of Python:

$ sudo apt-get install python2.6 python2.6-dev

June 22, 2013

Generating Audio Spectrograms in Python

A spectrogram is a visual representation of the spectrum of frequencies in a sound sample.

more info: wikipedia spectrogram

Spectrogram code in Python, using Matplotlib:
(source on GitHub)

"""Generate a Spectrogram image for a given WAV audio sample.

A spectrogram, or sonogram, is a visual representation of the spectrum
of frequencies in a sound.  Horizontal axis represents time, Vertical axis
represents frequency, and color represents amplitude.

import os
import wave

import pylab

def graph_spectrogram(wav_file):
    sound_info, frame_rate = get_wav_info(wav_file)
    pylab.figure(num=None, figsize=(19, 12))
    pylab.title('spectrogram of %r' % wav_file)
    pylab.specgram(sound_info, Fs=frame_rate)

def get_wav_info(wav_file):
    wav =, 'r')
    frames = wav.readframes(-1)
    sound_info = pylab.fromstring(frames, 'Int16')
    frame_rate = wav.getframerate()
    return sound_info, frame_rate

if __name__ == '__main__':
    wav_file = 'sample.wav'

Spectrogram code in Python, using timeside:
(source on GitHub)

"""Generate a Spectrogram image for a given audio sample.

Compatible with several audio formats: wav, flac, mp3, etc.

A spectrogram, or sonogram, is a visual representation of the spectrum
of frequencies in a sound.  Horizontal axis represents time, Vertical axis
represents frequency, and color represents amplitude.

import timeside

audio_file = 'sample.wav'

decoder = timeside.decoder.FileDecoder(audio_file)
grapher = timeside.grapher.Spectrogram(width=1920, height=1080)
(decoder | grapher).run()

happy audio hacking.

June 10, 2013

Python - concurrencytest: Running Concurrent Tests

Add parallel testing to your unit test framework.

In my previous post, I described running concurrent tests using nose as a loader and runner.

On a similar note, let's look at building concurrency into your own test framework built on Python's unittest.

Have a look at this module: concurrencytest

(Thanks to bits and concepts taken from testtools and bzrlib)

An Example:

Say you have a 'TestSuite' of tests loaded. You could run them with the standard 'TextTestRunner' like this:

runner = unittest.TextTestRunner()

That would run the tests in your suite sequentially in a single process.

By adding the concurrencytest module, you can use a 'ConcurrentTestSuite' instead, by adding:

from concurrencytest import ConcurrentTestSuite, fork_for_tests

concurrent_suite = ConcurrentTestSuite(suite, fork_for_tests(4))

That would run the same tests split across 4 processes (workers).

Note: this relies on 'os.fork()' which only works on Unix systems.

There's no way to understand this better than looking at some contrived examples!

This first example is totally unrealistic, but shows off concurrency perfectly. The test cases it loads each sleep for 0.5 seconds and then exit.

The Code:


Loaded 50 test cases...

Run tests sequentially:
Ran 50 tests in 25.031s


Run same tests across 50 processes:
Ran 50 tests in 0.525s



Now another example that shows concurrency with CPU-bound test cases. The test cases it loads each calculate fibonacci of 31 (recursively!) and then exit. We can see how it performs on my 8-core machine (Core2 i7 quad, hyperthreaded).

The Code:


Loaded 50 test cases...

Run tests sequentially:
Ran 50 tests in 21.941s


Run same tests with 2 processes:
Ran 50 tests in 11.081s


Run same tests with 4 processes:
Ran 50 tests in 5.862s


Run same tests with 8 processes:
Ran 50 tests in 4.743s


happy hacking.

June 9, 2013

Python - Nose: Running Concurrent Tests

To enable multiprocessing with N workers,
run nose with:

$ nosetests --processes=N

When writing tests in Python, I start with TestCase's derived from unittest.TestCase, and standard test discovery. When I need more complex test discovery/loading or output reports, I often use nose and its assortment of plugins as my test loader/runner.

One nice feature of nose is the multiprocess plugin. It allows you to run your tests suites concurrently rather than sequentially, spread across a number of worker processes. Running tests in parallel like this can potentially give you a large speedup in your test run times.

from the nose multiprocess docs:

"You can parallelize a test run across a configurable number of worker processes. While this can speed up CPU-bound test runs, it is mainly useful for IO-bound tests that spend most of their time waiting for data to arrive from someplace else and can benefit from parallelization."

Normally, you run tests from nose with:

$ nosetests

To run the same tests split across 4 processes (workers), you would just do:

$ nosetests --processes=4

Assuming your tests are properly isolated, everything should run normally, and you can benefit from a speedup on a multiprocessor machine.

However, Beware.

"Not all test suites will benefit from, or even operate correctly using, this plugin. For example, CPU-bound tests will run more slowly if you don't have multiple processors."
"But the biggest issue you will face is probably concurrency. Unless you have kept your tests as religiously pure unit tests, with no side-effects, no ordering issues, and no external dependencies, chances are you will experience odd, intermittent and unexplainable failures and errors when using this plugin. This doesn't necessarily mean the plugin is broken; it may mean that your test suite is not safe for concurrency."

April 1, 2013

Squeezelite - Headless Squeezebox Emulator

Use Squeezebox, without buying a Squeezebox...

Recently, Logitech discontinued most Squeezebox streaming music players. However, the media server is Open Source, so it looks like some form of Logitech Media Server (LMS) will live on, no matter what Logitech eventually does with it.

I've been a user of Squeezebox network music player since it was released by SlimDevices (SliMP3/SlimServer), and throughout the transfer to Logitech. I've owned 3 Squeezebox models over the years... currently enjoying the Squeezebox Touch, with music streamed from Logitech Media Server.

It works flawlessly for streaming my own music collection (FLAC/MP3/etc), and streaming radio (Pandora/Slacker/Sirius/etc), to my HiFi. I use the digital (S/PDIF) outputs, and sometimes the DAC/analog (RCA) outputs.

Now... with the release of Squeezelite, you can build your own Squeezebox, or use an existing computer/laptop with digital output as a Squeezebox.

Squeezelite is a cross-platform, headless, LMS client that supports playback synchronization, gapless playback, direct streaming, and playback at various sampling rates. It runs on Linux using ALSA audio output and other platforms using PortAudio. It is aimed at supporting high quality audio.

I gave Squeezelite 1.0 a try on Ubuntu 12.04, with S/PDIF optical output to my DAC. It worked like a charm!

Squeezelite info:

Squeezelite download (precompiled binaries for x86/amd64/arm):

Enjoy the music.

March 28, 2013

Python - Re-tag FLAC Audio Files (Update Metadata)

I had a bunch of FLAC (.flac) audio files together in a directory. They are from various sources and their metadata (tags) were somewhat incomplete or incorrect.

I managed to manually get all of the files standardized in "%Artist% - %Title%.flac" file name format. However, What I really wanted was to clear their metadata and just save "Artist" and "Title" tags, pulled from file names.

I looked at a few audio tagging tools in the Ubuntu repos, and came up short finding something simple that covered my needs. (I use Audio Tag Tool for MP3's, but it has no FLAC file support.)

So, I figured the easiest way to get this done was a quick Python script.

I grabbed Mutagen, a Python module to handle audio metadata with FLAC support.

This is essentially the task I was looking to do:

#!/usr/bin/env python

import glob
import os
from mutagen.flac import FLAC

for filename in glob.glob('*.flac'):
    artist, title = os.path.splitext(filename)[0].split(' - ', 1)
    audio = FLAC(filename)
    audio['artist'] = artist
    audio['title'] = title

It iterates over .flac files in the current directory, clearing the metadata and rewriting only the artist/title tags based on each file name.

I created a repository with a slightly more full-featured version, used to re-tag single FLAC files:

January 28, 2013

Python - verify a PNG file and get image dimensions

useful snippet for getting .png image dimensions without using an external imaging library.

#!/usr/bin/env python

import struct

def get_image_info(data):
    if is_png(data):
        w, h = struct.unpack('>LL', data[16:24])
        width = int(w)
        height = int(h)
        raise Exception('not a png image')
    return width, height

def is_png(data):
    return (data[:8] == '\211PNG\r\n\032\n'and (data[12:16] == 'IHDR'))

if __name__ == '__main__':
    with open('foo.png', 'rb') as f:
        data =

    print is_png(data)
    print get_image_info(data)

/headnods: source, Portable_Network_Graphics (Wikipedia)

January 23, 2013

Python Unit Testing Tutorial (PyMOTW unittest update)

tl;dr: an update to PyMOTW for `unittest` in Python 3: Python Unit Testing Tutorial.

When I was learning programming in Python, Doug Hellmann's 'PyMOTW' (Python Module Of The Week) blog-series was one of the best resources to learn Python's standard library.

His series later culminated in the book: 'The Python Standard Library By Example'.

The PyMOTW entry on `unittest` was a great introduction to unit testing in Python. Since the PyMOTW version is getting quite outdated, I updated the `unittest` module entry.

This new version includes some edits and updates to the text, and all code and examples have been updated to reflect Python 3.3.

Have a look at my updated Python 3.3 version:
'Python Unit Testing Tutorial'

license: Creative Commons Attribution, Non-commercial, Share-alike 3.0

say no to bugs...

January 14, 2013

Python - "The Matrix" in your terminal

Linux console program in Python, that scrolls binary numbers vertically in your terminal. Inspired by the movie: The Matrix

a screenshot:

in action:

the Code:

January 6, 2013

Python Testing - PhantomJS with Selenium WebDriver

PhantomJS is a headless WebKit with JavaScript API. It can be used for headless website testing.

PhantomJS has a lot of different uses. The interesting bit for me is to use PhantomJS as a lighter-weight replacement for a browser when running web acceptance tests. This enables faster testing, without a display or the overhead of full-browser startup/shutdown.

I write my web automation using Selenium WebDriver, in Python.

In future versions of PhantomJS, the GhostDriver component will be included.

GhostDriver is a pure JavaScript implementation of the WebDriver Wire Protocol for PhantomJS. It's a Remote WebDriver that uses PhantomJS as back-end.

So, Ghostdriver is the bridge we need to use Selenium WebDriver with Phantom.JS.

Since it is not available in the current PhantomJS release, you can try it yourself by compiling a special version of PhantomJS:

It wes pretty trvial to setup on Ubuntu (12.04):

$ sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
$ git clone git://
$ cd phantomjs
$ git checkout 1.8
$ ./
$ git remote add detro
$ git fetch detro && git checkout -b detro-ghostdriver-dev remotes/detro/ghostdriver-dev
$ ./

Then grab the `phantomjs` binary it produced (look inside `phantomjs/bin`). This is a self-contained executable, it can be moved to a different directory or another machine. Make sure it is located somewhere on your PATH, or declare it's location when creating your PhantomJS driver like the example below.

for these examples, `phantomjs` binary is located in same directory as test script.

Example: Python Using PhantomJS and Selenium WebDriver.

#!/usr/bin/env python

driver = webdriver.PhantomJS('./phantomjs')
# do webdriver stuff here

Example: Python Unit Test Using PhantomJS and Selenium WebDriver.

#!/usr/bin/env python

import unittest
from selenium import webdriver

class TestUbuntuHomepage(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.PhantomJS('./phantomjs')
    def testTitle(self):
        self.assertIn('Ubuntu', self.driver.title)
    def tearDown(self):

if __name__ == '__main__':