lists.arthurdejong.org
RSS feed

Re: UPC / EAN

[Date Prev][Date Next] [Thread Prev][Thread Next]

Re: UPC / EAN



On Sun, 2011-06-19 at 12:05 +0200, Jochen Garcke wrote:
> Maybe of interest, feel free to use the code.
> 
> I needed to validate barcodes of the form UPC or EAN-13, with potential
> additional EAN-2 or EAN-5 for periodicals, this code validates it.

Thanks. For the calculation of the checksum I currently use in ISBN-13 I
have the following quite compact piece of code:

def _calc_isbn13_check_digit(number):
    """Calculate the ISBN check digit for 13-digit numbers. The number passed
    should not have the check bit included."""
    return str((10 - sum( (2 * (i % 2) + 1) * int(n) for i, n in 
enumerate(number))) % 10)

which can be easily turned into something that validates arbitrary
length EAN codes:

def calc_check_digit(number):
    """Calculate the EAN check digit for 13-digit numbers. The number passed
    should not have the check bit included."""
    return str((10 - sum( (3 - 2 * (i % 2)) * int(n) for i, n in 
enumerate(reversed(number)))) % 10)

> Not sure if for a general code one would/should do the checks to
> remove EAN5 and EAN2 though, one might get into trouble doing this
> always.

I've shied away from stripping the EAN-2 and EAN-5 supplements in my
code for now. Perhaps an extra parameter to do the stripping can be
introduced. Have a look:

http://arthurdejong.org/trac/python-stdnum/browser/python-stdnum/stdnum/ean.py

-- 
-- arthur - arthur@arthurdejong.org - http://arthurdejong.org --
-- 
To unsubscribe send an email to
python-stdnum-users-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-users