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

Skip to content

Commit 31f2684

Browse files
committed
Implement validate() for Croatian numbers
1 parent 1932f69 commit 31f2684

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

stdnum/hr/oib.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# cnp.py - functions for handling Croatian OIB 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
@@ -24,16 +24,17 @@
2424
entities in Croatia. It has 11 digits (sometimes prefixed by HR), contains
2525
no personal information and uses the ISO 7064 Mod 11, 10 checksum algorithm.
2626
27-
>>> compact('HR 33392005961')
27+
>>> validate('HR 33392005961')
2828
'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: ...
3333
"""
3434

35-
from stdnum.util import clean
35+
from stdnum.exceptions import *
3636
from stdnum.iso7064 import mod_11_10
37+
from stdnum.util import clean
3738

3839

3940
def compact(number):
@@ -45,12 +46,22 @@ def compact(number):
4546
return number
4647

4748

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+
4861
def is_valid(number):
4962
"""Checks to see if the number provided is a valid OIB number. This
5063
checks the length, formatting and check digit."""
5164
try:
52-
number = compact(number)
53-
except:
65+
return bool(validate(number))
66+
except ValidationError:
5467
return False
55-
return len(number) == 11 and number.isdigit() and \
56-
mod_11_10.is_valid(number)

0 commit comments

Comments
 (0)
0