diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2013-05-17 13:28:46 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2013-06-08 14:45:39 +0200 |
commit | 301ba25dfc30384653fec153134673f68a3ca875 (patch) | |
tree | c498794b624d2d8aa8c9c5572155619df6b13b29 /stdnum/hu/anum.py | |
parent | 31f268433bfec7d01ae8071cb0c37084e455d150 (diff) |
Implement validate() for Hungarian numbers
Diffstat (limited to 'stdnum/hu/anum.py')
-rw-r--r-- | stdnum/hu/anum.py | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/stdnum/hu/anum.py b/stdnum/hu/anum.py index f00a9ef..e6d9315 100644 --- a/stdnum/hu/anum.py +++ b/stdnum/hu/anum.py @@ -1,7 +1,7 @@ # anum.py - functions for handling Hungarian VAT numbers # coding: utf-8 # -# Copyright (C) 2012 Arthur de Jong +# Copyright (C) 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 @@ -23,14 +23,15 @@ The ANUM is the Hungarian VAT (Közösségi adószám) number. It is an 8-digit taxpayer registration number that includes a weighted checksum. ->>> compact('HU-12892312') +>>> validate('HU-12892312') '12892312' ->>> is_valid('HU-12892312') -True ->>> is_valid('HU-12892313') # invalid check digit -False +>>> validate('HU-12892313') # invalid check digit +Traceback (most recent call last): + ... +InvalidChecksum: ... """ +from stdnum.exceptions import * from stdnum.util import clean @@ -49,11 +50,23 @@ def checksum(number): return sum(weights[i] * int(n) for i, n in enumerate(number)) % 10 +def validate(number): + """Checks to see if the number provided is a valid VAT number. This + checks the length, formatting and check digit.""" + number = compact(number) + if not number.isdigit(): + raise InvalidFormat() + if len(number) != 8: + raise InvalidLength() + if checksum(number) != 0: + raise InvalidChecksum() + return number + + def is_valid(number): """Checks to see if the number provided is a valid VAT number. This checks the length, formatting and check digit.""" try: - number = compact(number) - except: + return bool(validate(number)) + except ValidationError: return False - return number.isdigit() and len(number) == 8 and checksum(number) == 0 |