Welcome, guest | Sign In | My Account | Store | Cart

A python class I wrote to add or validate a GTIN check digit.

Python, 67 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67``` ```#!/usr/bin/env python class GTIN(object): def __init__(self, barcode=''): self.barcode = barcode def __checkDigit(self, digits): total = sum(digits) + sum(map(lambda d: d*2, digits[-1::-2])) return (10 - (total % 10)) % 10 def validateCheckDigit(self, barcode=''): barcode = (barcode if barcode else self.barcode) if len(barcode) in (8,12,13,14) and barcode.isdigit(): digits = map(int, barcode) checkDigit = self.__checkDigit( digits[0:-1] ) return checkDigit == digits[-1] return False def addCheckDigit(self, barcode=''): barcode = (barcode if barcode else self.barcode) if len(barcode) in (7,11,12,13) and barcode.isdigit(): digits = map(int, barcode) return barcode + str(self.__checkDigit(digits)) return '' if __name__ == '__main__': # validateCheckDigit() gtin = GTIN() if gtin.validateCheckDigit('1'*11 +'7') == True: print 'Pass' else: print 'Fail' gtin = GTIN() if gtin.validateCheckDigit('1'*11 +'7') == True: print 'Pass' else: print 'Fail' if GTIN('1'*11 +'7').validateCheckDigit() == True: print 'Pass' else: print 'Fail' if GTIN().validateCheckDigit('1'*11 +'6') == False: print 'Pass' else: print 'Fail' # addCheckDigit() gtin = GTIN() if GTIN().addCheckDigit('1' *11) == '1'*11 +'7': print 'Pass' else: print 'Fail' gtin = GTIN('1'*7) if gtin.addCheckDigit() == '1'*7 +'5': print 'Pass' else: print 'Fail' if GTIN('1'*12).addCheckDigit() == '1'*12 +'6': print 'Pass' else: print 'Fail' if GTIN().addCheckDigit('1'*11) == '1'*11 + '7': print 'Pass' else: print 'Fail' ```
 Created by Richard Thieman on Sat, 26 Nov 2011 (MIT)

### Required Modules

• (none specified)