Skip to content

Instantly share code, notes, and snippets.

@corpit
corpit / upc_check_digit.py
Created January 1, 2014 02:59
Calculate UPC-A check digit in Python
def add_check_digit(upc_str):
"""
Returns a 12 digit upc-a string from an 11-digit upc-a string by adding
a check digit
>>> add_check_digit('02345600007')
'023456000073'
>>> add_check_digit('21234567899')
'212345678992'
>>> add_check_digit('04210000526')
@corpit
corpit / upca_to_upce.py
Last active August 8, 2019 22:58
UPC-A to UPC-E converter in Python
def upca_to_upce(upca):
"""
Takes a upc-a code (12 digits), including check digit,
as a string and returns upc-e representation.
UPC-E representation will be eight digits, including check digit
Returns None if no upc-e representation is possible
Rules from http://mdn.morovia.com/kb/UPCE-Specification-10634.html