8000 Implement validate() for Cypriot numbers · odony/python-stdnum@fce6196 · GitHub
[go: up one dir, main page]

Skip to content

Commit fce6196

Browse files
committed
Implement validate() for Cypriot numbers
1 parent 14e382f commit fce6196

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

stdnum/cy/vat.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# vat.py - functions for handling Cypriot VAT numbers
22
# coding: utf-8
33
#
4-
# Copyright (C) 2012 Arthur de Jong
4+
# Copyright (C) 2012, 2013 Arthur de Jong
55
#
66
# This library is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU Lesser General Public
@@ -25,12 +25,15 @@
2525
2626
>>> compact('CY-10259033P')
2727
'10259033P'
28-
>>> is_valid('CY-10259033P ')
29-
True
30-
>>> is_valid('CY-10259033Z') # invalid check digit
31-
False
28+
>>> validate('CY-10259033P ')
29+
'10259033P'
30+
>>> validate('CY-10259033Z') # invalid check digit
31+
Traceback (most recent call last):
32+
...
33+
InvalidChecksum: ...
3234
"""
3335

36+
from stdnum.exceptions import *
3437
from stdnum.util import clean
3538

3639

@@ -56,13 +59,25 @@ def calc_check_digit(number):
5659
) % 26]
5760

5861

62+
def validate(number):
63+
"""Checks to see if the number provided is a valid VAT number. This
64+
checks the length, formatting and check digit."""
65+
number = compact(number)
66+
if not number[:-1].isdigit():
67+
raise InvalidFormat()
68+
if len(number) != 9:
69+
raise InvalidLength()
70+
if number[0:2] == '12':
71+
raise InvalidComponent()
72+
if number[-1] != calc_check_digit(number[:-1]):
73+
raise InvalidChecksum()
74+
return number
75+
76+
5977
def is_valid(number):
6078
"""Checks to see if the number provided is a valid VAT number. This
6179
checks the length, formatting and check digit."""
6280
try:
63-
number = compact(number)
64-
except:
81+
return bool(validate(number))
82+
except ValidationError:
6583
return False
66-
return len(number) == 9 and number[:-1].isdigit() and \
67-
number[0:2] != '12' and \
68-
number[-1] == calc_check_digit(number[:-1])

0 commit comments

Comments
 (0)
0