June 17, 2012

Python Timer Class - Context Manager for Timing Code Blocks

Here is a handy Python Timer class. It creates a context manager object, used for timing a block of code.

from timeit import default_timer


class Timer(object):
    def __init__(self, verbose=False):
        self.verbose = verbose
        self.timer = default_timer
        
    def __enter__(self):
        self.start = self.timer()
        return self
        
    def __exit__(self, *args):
        end = self.timer()
        self.elapsed_secs = end - self.start
        self.elapsed = self.elapsed_secs * 1000  # millisecs
        if self.verbose:
            print 'elapsed time: %f ms' % self.elapsed

To use the Timer (context manager object), invoke it using Python's `with` statement. The duration of the context (code inside your `with` block) will be timed. It uses the appropriate timer for your platform, via the `timeit` module.

Timer is used like this:

with Timer() as target:
    # block of code goes here.
    # result (elapsed time) is stored in `target` properties.

Example script:
timing a web request (HTTP GET), using the `requests` module.

#!/usr/bin/env python

import requests
from timer import Timer

url = 'https://github.com/timeline.json'

with Timer() as t:
    r = requests.get(url)
    
print 'fetched %r in %.2f millisecs' % (url, t.elapsed)

Output:

fetched 'https://github.com/timeline.json' in 458.76 millisecs

`timer.py` in GitHub Gist form, with more examples:

June 16, 2012

My Evolving Droids


(Droid -> Droid X -> Droid Razr) [stock pics]

In the past few years, mobile devices have turned into amazing little pocket computers. I'm blown away by the hardware (ARM) and software (Android) evolution.

For smartphone wireless service, I was a Verizon customer (USA) for the past decade. I've owned the flagship Droid series devices by Motorola since their first launch (currently running on the 4G LTE network). It started with a clunky original Droid, followed by a Droid X, and then the sleek Droid Razr... every year another generation of better, faster devices.

Device Specs:

Motorola Droid - Nov 2009

  • OS: Android v2.0
  • CPU: 600 MHz ARM Cortex-A8 (TI OMAP3430, 65nm)
  • RAM: 256 MB
  • Internal Storage: 512 MB
  • Screen Size: 3.7 inch
  • Resolution: 480 x 854

Motorola Droid X - July 2010

  • OS: Android v2.1
  • CPU: 1 GHz ARM Cortex-A8 (TI OMAP3630, 45nm)
  • RAM: 512 MB
  • Internal Storage: 8 GB
  • Screen Size: 4.3 inch
  • Resolution: 480 x 854

Motorola Droid Razr - Nov 2011

  • OS: Android v2.3
  • CPU: 1.2 GHz dual-core ARM Cortex-A9 (TI OMAP4430, 45nm)
  • RAM: 1 GB
  • Internal Storage: 16 GB
  • Screen Size: 4.3 inch
  • Resolution: 540 x 960

However, my life with the Moto Droids is coming to an end. I just signed up with a new wireless carrier and pre-ordered a new Sammy GS3!:


(Samsung Galaxy S3) [stock pic]

Samsung Galaxy S3 - June 2012

  • OS: Android v4.0
  • CPU: 1.5 GHz dual-core Krait (Qualcomm Snapdragon S4 MSM8960, 28nm)
  • RAM: 2 GB
  • Internal Storage: 16 GB
  • Screen Size: 4.8 inch
  • Resolution: 720 x 1280

June 14, 2012

Home Audio Setup

I work from my apartment and listen to music or talk-radio nearly 24-hours a day (even at soft volume while I sleep). My home audio setup is pretty important to me.

Here is a description of the current rig for home listening:


The Gear:

  • Media Server (Ubuntu, Logitech Media Server)
  • Touchscreen UI (Android, Squeezebox Controller)
  • Wifi Music Player (Squeezebox Touch)
  • Amplifier (Harman Kardon)
  • 5 Speakers (Polk Audio)

The Content:

Streaming Radio Networks:

MP3 Collection:


Tablet interface (Logitech Squeezebox Controller):

Web interface (Logitech Media Server 7.7.2):


Rock on!

June 4, 2012

History of Python - Development Visualization - Gource

I made a new visualization. Have a look!

History of Python - Gource - development visualization (august 1990 - june 2012)
[HD video, encoded at 1080p. watch on YouTube in highest resolution possible.]

What is it?

This is a visualization of Python core development. It shows growth of the Python project's source code over time (August 1990 - June 2012). Nearly 22 years! The source code history and relations are displayed by Gource as an animated tree, tracking commits over time. Directories appear as branches with files as leaves. Developers can be seen working on the tree at the times they contributed to the Python project.

Video:
Rendered with Gource v0.37 on Ubuntu 12.04

Music:
Chris Zabriskie - The Life and Death of a Certain K Zabriskie Patriarch

Repository:
cpython 3.3.0 alpha, retrieved from mercurial on June 2 2012


for more visualizations and other videos, check out my YouTube channel.