From 301ba25dfc30384653fec153134673f68a3ca875 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Fri, 17 May 2013 13:28:46 +0200 Subject: Implement validate() for Hungarian numbers --- stdnum/hu/anum.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'stdnum/hu/anum.py') 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 -- cgit v1.2.3