diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2013-05-03 22:52:17 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2013-06-08 14:45:36 +0200 |
commit | 5c9090b369440885e2c3a68eba46c6fb8baace97 (patch) | |
tree | a0e023ec91502f6b1e40bedc5b70cd3c872f23a3 | |
parent | 9ad51399d680b4f1944662bd4a9b9e5a6d1d5420 (diff) |
Implement validate() for the Verhoeff checksum
-rw-r--r-- | stdnum/verhoeff.py | 32 | ||||
-rw-r--r-- | tests/test_verhoeff.doctest | 30 |
2 files changed, 42 insertions, 20 deletions
diff --git a/stdnum/verhoeff.py b/stdnum/verhoeff.py index 69fb4b7..4fb9315 100644 --- a/stdnum/verhoeff.py +++ b/stdnum/verhoeff.py @@ -1,6 +1,6 @@ # verhoeff.py - functions for performing the Verhoeff checksum # -# Copyright (C) 2010, 2011, 2012 Arthur de Jong +# Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -22,16 +22,21 @@ The Verhoeff algorithm uses two tables for permutations and multiplications to calculate a checksum. ->>> is_valid('1234') -False +>>> validate('1234') +Traceback (most recent call last): + ... +InvalidChecksum: ... >>> checksum('1234') 1 >>> calc_check_digit('1234') '0' ->>> is_valid('12340') -True +>>> validate('12340') +'12340' """ +from stdnum.exceptions import * + + # These are the multiplication and permutation tables used in the # Verhoeff algorithm. @@ -70,11 +75,24 @@ def checksum(number): return check -def is_valid(number): +def validate(number): """Checks to see if the number provided passes the Verhoeff checksum.""" + if not bool(number): + raise InvalidFormat() try: - return bool(number) and checksum(number) == 0 + valid = checksum(number) == 0 except: + raise InvalidFormat() + if not valid: + raise InvalidChecksum() + return number + + +def is_valid(number): + """Checks to see if the number provided passes the Verhoeff checksum.""" + try: + return bool(validate(number)) + except ValidationError: return False diff --git a/tests/test_verhoeff.doctest b/tests/test_verhoeff.doctest index 5e2faa6..4c22e9d 100644 --- a/tests/test_verhoeff.doctest +++ b/tests/test_verhoeff.doctest @@ -1,6 +1,6 @@ test_verhoeff.doctest - more detailed doctests for stdnum.verhoeff module -Copyright (C) 2010 Arthur de Jong +Copyright (C) 2010, 2011, 2013 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -37,25 +37,29 @@ These are normal variations that should just work. Calculating checksums: The same numbers but now simply ask for validation: ->>> verhoeff.is_valid('654') -False ->>> verhoeff.is_valid('1428570') -True ->>> verhoeff.is_valid('398438246238642378648236487236482734') -False +>>> verhoeff.validate('654') +Traceback (most recent call last): + ... +InvalidChecksum: ... +>>> verhoeff.validate('1428570') +'1428570' +>>> verhoeff.validate('398438246238642378648236487236482734') +Traceback (most recent call last): + ... +InvalidChecksum: ... Adding a check digit to the numbers so they are all valid: >>> verhoeff.calc_check_digit('654') '8' ->>> verhoeff.is_valid('6548') -True +>>> verhoeff.validate('6548') +'6548' >>> verhoeff.calc_check_digit('1428570') '8' ->>> verhoeff.is_valid('1428570') -True +>>> verhoeff.validate('1428570') +'1428570' >>> verhoeff.calc_check_digit('398438246238642378648236487236482734') '7' ->>> verhoeff.is_valid('3984382462386423786482364872364827347') -True +>>> verhoeff.validate('3984382462386423786482364872364827347') +'3984382462386423786482364872364827347' |