Skip to content

Instantly share code, notes, and snippets.

@hornc
Created July 2, 2020 03:10
Show Gist options
  • Save hornc/19884c1bf853a8d81e3324b439648ff1 to your computer and use it in GitHub Desktop.
Save hornc/19884c1bf853a8d81e3324b439648ff1 to your computer and use it in GitHub Desktop.
testing some barcode check digit validations
# Library codabar (14 digit barcode) check digit validator
# (1 digit):material type + (4 digits):library code + (8 digits):item code + (1 digit): checksum
# source: http://www.makebarcode.com/specs/codabar.html
# - GOOD!
def vcodabar(n):
"""Validate the checkdigit of a 14 digit library barcode."""
check = int(n[-1])
data = n[:-1]
total = 0
for i, c in enumerate(data):
c = int(c)
total += c if (i % 2) else (2 * c - 9) if (2 * c > 9) else 2 * c
calc = total % 10
calc = 10 - calc if calc else 0
print(calc)
return calc == check
# source: http://www.makebarcode.com/specs/code_11.html
# possibly buggy / useless. Can't find a library barcode where this actually works.
# Suspect that maybe the checkdigit is only used in barcode scanning and isn't part
# of the code stored in MARC records?
# ??? likely wrong, at least no good with the data I'm looking at
def vcode11(n):
check = int(n[-1])
data = n[:-1]
total = 0
for i, c in enumerate(data[::-1]):
total += int(c) * (i + 1)
calc = total % 11
print(calc)
return calc == check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment