1
1
# cnp.py - functions for handling Croatian OIB numbers
2
2
# coding: utf-8
3
3
#
4
- # Copyright (C) 2012 Arthur de Jong
4
+ # Copyright (C) 2012, 2013 Arthur de Jong
5
5
#
6
6
# This library is free software; you can redistribute it and/or
7
7
# modify it under the terms of the GNU Lesser General Public
24
24
entities in Croatia. It has 11 digits (sometimes prefixed by HR), contains
25
25
no personal information and uses the ISO 7064 Mod 11, 10 checksum algorithm.
26
26
27
- >>> compact ('HR 33392005961')
27
+ >>> validate ('HR 33392005961')
28
28
'33392005961'
29
- >>> is_valid('33392005961')
30
- True
31
- >>> is_valid('33392005962') # invalid check digit
32
- False
29
+ >>> validate('33392005962') # invalid check digit
30
+ Traceback (most recent call last):
31
+ ...
32
+ InvalidChecksum: ...
33
33
"""
34
34
35
- from stdnum .util import clean
35
+ from stdnum .exceptions import *
36
36
from stdnum .iso7064 import mod_11_10
37
+ from stdnum .util import clean
37
38
38
39
39
40
def compact (number ):
@@ -45,12 +46,22 @@ def compact(number):
45
46
return number
46
47
47
48
49
+ def validate (number ):
50
+ """Checks to see if the number provided is a valid OIB number. This
51
+ checks the length, formatting and check digit."""
52
+ number = compact (number )
53
+ if not number .isdigit ():
54
+ raise InvalidFormat ()
55
+ if len (number ) != 11 :
56
+ raise InvalidLength ()
57
+ mod_11_10 .validate (number )
58
+ return number
59
+
60
+
48
61
def is_valid (number ):
49
62
"""Checks to see if the number provided is a valid OIB number. This
50
63
checks the length, formatting and check digit."""
51
64
try :
52
- number = compact ( number )
53
- except :
65
+ return bool ( validate ( number ) )
66
+ except ValidationError :
54
67
return False
55
- return len (number ) == 11 and number .isdigit () and \
56
- mod_11_10 .is_valid (number )
0 commit comments