Category: Programming

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


What's the world listening to?

July 8, 2010

A while back I crafted a Processing sketch that displayed a map of the world and popped up the most popular artist for the country the mouse was hovering over. Bored last night, I decided to give the newish Google Maps API a whirl and see if I could recreate the sketch for the web.

It uses fxb's javascript-last.fm-api library which made the artist lookup incredibly easy. The whole thing took no more than 2 hours.

Categories: Fun, Programming, Visualization

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.