Bam! A boom Python port

March 31, 2013

Over the Easter weekend I spent some time porting Zach Holman's excellent boom tool to Python.

The source is over at Github.

You can install it like this:

$ pip install bam

Or install the latest development version like this:

$ pip install git+git://github.com/mrben/bam.git

And use it like this:

A full list of command is available on the GitHub project page.

Categories: Programming

View Comments


Validating GRids: A Python GRid check digit calculator

June 27, 2012

A Global Resource Identifier, or GRid, is used in the music industry to uniquely identify digital data such as sound recordings. Like barcodes, the last digit is a check digit to verify that the preceding digits were correct.

Calculating the check digit for a GRid is a little more of an obscure need than barcodes, but this Python code snippet will save whomever needs to do it in Python a bunch of time!

def calculate_grid_check_digit(grid):
    """
    Assumes `grid` is 17 characters long
    and missing the check digit.
    """
    def _mod_36(i):
        """
        A modified modulus function is used
        by the algorithm.
        """
        m = i % 36
        return 36 if m == 0 else m

    grid = grid.upper()
    lookup_values = ['0','1','2','3','4','5','6','7','8','9',
                     'A','B','C','D','E','F','G','H','I','J',
                     'K','L','M','N','O','P','Q','R','S','T',
                     'U','V','W','X','Y','Z']
    p_list = [36]
    s_list = []
    
    for j, c in enumerate(grid):
        c_val = lookup_values.index(c)
        s_list.append(p_list[j]%37+int(c_val))
        p_list.append(_mod_36(s_list[j]) * 2)
    
    i = 0
    while 1:
        if _mod_36(p_list[-1]%37 + i) == 1:
            break;
        i += 1
    return lookup_values[i]

You can read more on GRids at the International Federation of the Phonographic Industry website and access their online GRid validator. (Don't view the JavaScript source though, for the code is terrible!)

Categories: Programming, Python

View Comments


Validating barcodes

June 1, 2012

Here is some Python to validate barcodes (tested with EAN-13s). Someone should find it useful.

get_check_digit() will generate the valid check digit for the first part of a barcode. While is_valid_barcode() will check the final digit of a barcode against the generated check digit.

import math

def get_check_digit(b):
    """
    Returns the check digit for a barcode.
    
    It is assumed that the check digit is missing from the input.
    """
    sum = 0
    for x, c in enumerate(b[::-1]):
        if (x + 1) % 2:
            sum += int(c) * 3
        else:
            sum += int(c)
    return str(int(math.ceil(sum / 10.0) * 10) - sum)

def is_valid_barcode(b):
    """
    Checks the last digit of a barcode matches the calculated 
    check digit.
    """
    return get_check_digit(b[0:-1]) == b[-1]

test_data = (
    # Valid barcodes
    ('9771473968012', True),
    ('0123456789012', True),
    ('1234567890128', True),
    # Invalid barcodes
    ('9771473968011', False),
    ('0123456789019', False),
    ('1234567890127', False),
)

for t in test_data:
    print t[1] == is_valid_barcode(t[0])

Categories: Programming, Python

View Comments


Shall I Go To The Pub?

April 21, 2011

Last Friday someone asked "shall we go to the pub?" I thought it was a great idea, but as I feel I've lost my own identity and become a conduit for the Internet I thought I should check with it first.

Whacking shalligotothepub.com into my browser I was shocked to find no answer. I couldn't possibly go to the pub without the Internet sanctioning it, so Pawel and I spent 20 minutes creating shalligotothepub.com.

shalligotothepub.com screenshot

It was done for our own amusement, but after Patrick submitted it to StumbleUpon the hits started rolling in, and in, and in. Thousands and thousands of visits within a couple of hours was NOT what we were expecting! (So we quickly stuck a Facebook Like button on there.) And they continued. Over 11k on the first day, and half that on the next. Monday and Tuesday saw an unexpected resurgence in people wanting to go to the pub with the Internet's permission. Tuesday being the busiest say yet with 11.5k visits.

We've had around 40k unique visitors, over 2k Facebook Likes and 56k page views. Over 75% of traffic came from StumbleUpon and 8% from Facebook. Overwhelmingly out visitors choose Firefox and Chrome to browser the web - perhaps the high Firefox percentage is linked to the handy StumbleUpon plugin.

This has certainly been an interesting lesson.

Categories: Fun

View Comments


About me

Hi, I'm Ben Tappin.

I make stuff, mainly for the web. You can read more about me.

And you can hire me for freelance work.