diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2013-05-17 17:02:15 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2013-06-08 14:45:42 +0200 |
commit | 1aaf9028a5a3c411a51f243bbbeeebee738e811c (patch) | |
tree | 293e7613d5f4239f37255042fc8ece8fa99655eb | |
parent | 8982d1e8bdbc27844319edad4d968826048518d6 (diff) |
Implement validate() for Slovak numbers
-rw-r--r-- | stdnum/sk/dph.py | 41 | ||||
-rw-r--r-- | stdnum/sk/rc.py | 32 |
2 files changed, 45 insertions, 28 deletions
diff --git a/stdnum/sk/dph.py b/stdnum/sk/dph.py index 669e109..3654316 100644 --- a/stdnum/sk/dph.py +++ b/stdnum/sk/dph.py @@ -1,7 +1,7 @@ # vat.py - functions for handling Slovak 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 IČ DPH (Identifikačné číslo pre daň z pridanej hodnoty) is a 10-digit number used for VAT purposes. It has a straightforward checksum. ->>> compact('SK 202 274 96 19') +>>> validate('SK 202 274 96 19') '2022749619' ->>> is_valid('SK 202 274 96 19') -True ->>> is_valid('SK 202 274 96 18') # invalid check digits -False +>>> validate('SK 202 274 96 18') # invalid check digits +Traceback (most recent call last): + ... +InvalidChecksum: ... """ +from stdnum.exceptions import * from stdnum.sk import rc from stdnum.util import clean @@ -49,16 +50,28 @@ def checksum(number): return int(number) % 11 +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) != 10: + raise InvalidLength() + # it is unclear whether the RČ can be used as a valid VAT number + if rc.is_valid(number): + return number + if number[0] == '0' or int(number[2]) not in (2, 3, 4, 7, 8, 9): + raise InvalidFormat() + 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 - if not number.isdigit() or len(number) != 10: - return False - # it is unclear whether the RČ can be used as a valid VAT number - return rc.is_valid(number) or \ - (number[0] != '0' and int(number[2]) in (2, 3, 4, 7, 8, 9) and - checksum(number) == 0) diff --git a/stdnum/sk/rc.py b/stdnum/sk/rc.py index 9efbdcb..d0ffa8f 100644 --- a/stdnum/sk/rc.py +++ b/stdnum/sk/rc.py @@ -1,7 +1,7 @@ # rc.py - functions for handling Slovak birth 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 @@ -27,23 +27,27 @@ person and their gender. This number is identical to the Czech counterpart. ->>> compact('710319/2745') +>>> validate('710319/2745') '7103192745' ->>> is_valid('7103192745') -True ->>> is_valid('991231123') -True ->>> is_valid('7103192746') # invalid check digit -False ->>> is_valid('1103492745') # invalid date -False ->>> is_valid('590312/123') # 9 digit number in 1959 -False +>>> validate('991231123') +'991231123' +>>> validate('7103192746') # invalid check digit +Traceback (most recent call last): + ... +InvalidChecksum: ... +>>> validate('1103492745') # invalid date +Traceback (most recent call last): + ... +InvalidComponent: ... +>>> validate('590312/123') # 9 digit number in 1959 +Traceback (most recent call last): + ... +InvalidLength: ... >>> format('7103192745') '710319/2745' """ # since this number is essentially the same as the Czech counterpart # (until 1993 the Czech Republic and Slovakia were one country) -from stdnum.cz.rc import compact, is_valid, format -__all__ = ['compact', 'is_valid', 'format'] +from stdnum.cz.rc import compact, validate, is_valid, format +__all__ = ['compact', 'validate', 'is_valid', 'format'] |