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

Skip to content

Commit 083993b

Browse files
committed
Implement validate() for Irish numbers
1 parent 301ba25 commit 083993b

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

stdnum/ie/pps.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pps.py - functions for handling Irish PPS numbers
22
#
3-
# Copyright (C) 2012 Arthur de Jong
3+
# Copyright (C) 2012, 2013 Arthur de Jong
44
#
55
# This library is free software; you can redistribute it and/or
66
# modify it under the terms of the GNU Lesser General Public
@@ -24,18 +24,19 @@
2424
be followed by an extra letter that can be a 'W', 'T' or an 'X' and is
2525
ignored for the check algorithm.
2626
27-
>>> compact('6433435F')
27+
>>> validate('6433435F')
2828
'6433435F'
29-
>>> is_valid('6433435F')
30-
True
31-
>>> is_valid('6433435E') # incorrect check digit
32-
False
29+
>>> validate('6433435E') # incorrect check digit
30+
Traceback (most recent call last):
31+
...
32+
InvalidChecksum: ...
3333
"""
3434

3535
import re
3636

37-
from stdnum.util import clean
37+
from stdnum.exceptions import *
3838
from stdnum.ie import vat
39+
from stdnum.util import clean
3940

4041

4142
pps_re = re.compile('^\d{7}[A-W][WTX]?$')
@@ -48,13 +49,21 @@ def compact(number):
4849
return clean(number, ' -').upper().strip()
4950

5051

52+
def validate(number):
53+
"""Checks to see if the number provided is a valid PPS number. This
54+
checks the length, formatting and check digit."""
55+
number = compact(number)
56+
if not pps_re.match(number):
57+
raise InvalidFormat()
58+
if number[7] != vat.calc_check_digit(number[:7]):
59+
raise InvalidChecksum()
60+
return number
61+
62+
5163
def is_valid(number):
5264
"""Checks to see if the number provided is a valid PPS number. This
5365
checks the length, formatting and check digit."""
5466
try:
55-
number = compact(number)
56-
except:
57-
return False
58-
if not pps_re.match(number):
67+
return bool(validate(number))
68+
except ValidationError:
5969
return False
60-
return number[7] == vat.calc_check_digit(number[:7])

stdnum/ie/vat.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vat.py - functions for handling Irish VAT numbers
22
#
3-
# Copyright (C) 2012 Arthur de Jong
3+
# Copyright (C) 2012, 2013 Arthur de Jong
44
#
55
# This library is free software; you can redistribute it and/or
66
# modify it under the terms of the GNU Lesser General Public
@@ -22,18 +22,21 @@
2222
The Irish VAT number consists of 8 digits. The last digit is a check
2323
letter, the second digit may be a number, a letter, "+" or "*".
2424
25-
>>> compact('IE 6433435F')
25+
>>> validate('IE 6433435F')
2626
'6433435F'
27-
>>> is_valid('6433435F')
28-
True
29-
>>> is_valid('6433435E') # incorrect check digit
30-
False
31-
>>> is_valid('8D79739I') # old style number
32-
True
33-
>>> is_valid('8?79739J') # incorrect old style
34-
False
27+
>>> validate('6433435E') # incorrect check digit
28+
Traceback (most recent call last):
29+
...
30+
InvalidChecksum: ...
31+
>>> validate('8D79739I') # old style number
32+
'8D79739I'
33+
>>> validate('8?79739J') # incorrect old style
34+
Traceback (most recent call last):
35+
...
36+
InvalidFormat: ...
3537
"""
3638

39+
from stdnum.exceptions import *
3740
from stdnum.util import clean
3841

3942

@@ -54,20 +57,31 @@ def calc_check_digit(number):
5457
return alphabet[sum((8 - i) * int(n) for i, n in enumerate(number)) % 23]
5558

5659

57-
def is_valid(number):
60+
def validate(number):
5861
"""Checks to see if the number provided is a valid VAT number. This checks
5962
the length, formatting and check digit."""
60-
try:
61-
number = compact(number)
62-
except:
63-
return False
64-
if len(number) != 8 or not number[0].isdigit() or \
65-
not number[2:7].isdigit():
66-
return False
63+
number = compact(number)
64+
if not number[:1].isdigit() or not number[2:7].isdigit():
65+
raise InvalidFormat()
66+
if len(number) != 8:
67+
raise InvalidLength()
6768
if number[:7].isdigit():
6869
# new system
69-
return number[-1] == calc_check_digit(number[:-1])
70-
if number[1] in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ+*':
70+
if number[-1] != calc_check_digit(number[:-1]):
71+
raise InvalidChecksum()
72+
elif number[1] in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ+*':
7173
# old system
72-
return number[-1] == calc_check_digit(number[2:-1] + number[0])
73-
return False
74+
if number[-1] != calc_check_digit(number[2:-1] + number[0]):
75+
raise InvalidChecksum()
76+
else:
77+
raise InvalidFormat()
78+
return number
79+
80+
81+
def is_valid(number):
82+
"""Checks to see if the number provided is a valid VAT number. This checks
83+
the length, formatting and check digit."""
84+
try:
85+
return bool(validate(number))
86+
except ValidationError:
87+
return False

0 commit comments

Comments
 (0)
0