Skip to content

Instantly share code, notes, and snippets.

@w00kie
Created November 15, 2011 07:25
Show Gist options
  • Save w00kie/1366388 to your computer and use it in GitHub Desktop.
Save w00kie/1366388 to your computer and use it in GitHub Desktop.
Check validity of parcel tracking number from Yamato, Sagawa and JapanPost Yu-pack.
#!/usr/bin/env python
# encoding: utf-8
"""
checkdigit.py
-------------
Check validity of parcel tracking number from Yamato, Sagawa and JapanPost Yu-pack.
Using the "seven check" method described on http://okwave.jp/qa/q1785259.html
Apply the weights 46231546231 to each digit but the last. Sum it all up and divide by 7.
Remainder must be equal to the last digit of the tracking number.
Created by Francois Rejete on 2011-11-15.
Copyright (c) 2011 w00kie.com All rights reserved.
mailto:[email protected]
"""
import sys
import argparse
WEIGHTS = "46231546231"
def main(argv=None):
if argv is None:
argv = sys.argv
# Make some pretty command line arguments
parser = argparse.ArgumentParser(description="Check validity of parcel tracking number from Yamato, Sagawa and JapanPost Yu-pack.",
epilog="Coded by Francois Rejete - [email protected]")
parser.add_argument("tracking", metavar="TRACKINGNUMBER", nargs='+', help="Tracking number.")
args = parser.parse_args()
for tracking in map(lambda x: x.strip().replace('-',''), args.tracking):
weighted = sum(int(digit)*int(weight) for digit, weight in zip(tracking[:-1], WEIGHTS))
if weighted%7 == int(tracking[-1:]):
print tracking, "=> SUCCESS!!"
else:
print tracking, "=> FAILED... orz"
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment