From 4753c09e81a0adb6b62d12ad60f5a5107d5033c2 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Fri, 17 May 2013 13:04:14 +0200 Subject: Implement validate() for Finnish numbers --- stdnum/fi/alv.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'stdnum/fi/alv.py') diff --git a/stdnum/fi/alv.py b/stdnum/fi/alv.py index 509ad9c..ed9457c 100644 --- a/stdnum/fi/alv.py +++ b/stdnum/fi/alv.py @@ -1,7 +1,7 @@ # vat.py - functions for handling Finnish 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 @@ -22,14 +22,15 @@ The number is an 8-digit code with a weighted checksum. ->>> compact('FI- 2077474-0') +>>> validate('FI 20774740') '20774740' ->>> is_valid('FI 20774740') -True ->>> is_valid('FI 20774741') # invalid check digit -False +>>> validate('FI 20774741') # invalid check digit +Traceback (most recent call last): + ... +InvalidChecksum: ... """ +from stdnum.exceptions import * from stdnum.util import clean @@ -48,11 +49,23 @@ def checksum(number): return sum(weights[i] * int(n) for i, n in enumerate(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) != 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