diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2013-06-08 14:46:38 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2013-06-08 14:46:38 +0200 |
commit | 999f2c38f6fdef19254e505db35c9ab722a9f1af (patch) | |
tree | 210f07b9e359bdcf508ca9a7a35a7832ac90acb1 /stdnum/hr | |
parent | 99586c9258b77d4855e874c79055ce8e0dc479f3 (diff) | |
parent | cb699211eead9e9d40eee8fc3d94249934476130 (diff) |
Provide a validate() function in all modules
This provides an additional means of doing number validation that allows
applications calling this library to get more information about why the
validation failed and present more informative messages to the user.
This introduces a collection of exceptions which will be raised by the
validate() function in each module. All modules have been updated to
provide this new function.
Diffstat (limited to 'stdnum/hr')
-rw-r--r-- | stdnum/hr/oib.py | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/stdnum/hr/oib.py b/stdnum/hr/oib.py index fad0707..c78dede 100644 --- a/stdnum/hr/oib.py +++ b/stdnum/hr/oib.py @@ -1,7 +1,7 @@ # cnp.py - functions for handling Croatian OIB 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 @@ -24,16 +24,17 @@ The personal identification number is used to identify persons and legal entities in Croatia. It has 11 digits (sometimes prefixed by HR), contains no personal information and uses the ISO 7064 Mod 11, 10 checksum algorithm. ->>> compact('HR 33392005961') +>>> validate('HR 33392005961') '33392005961' ->>> is_valid('33392005961') -True ->>> is_valid('33392005962') # invalid check digit -False +>>> validate('33392005962') # invalid check digit +Traceback (most recent call last): + ... +InvalidChecksum: ... """ -from stdnum.util import clean +from stdnum.exceptions import * from stdnum.iso7064 import mod_11_10 +from stdnum.util import clean def compact(number): @@ -45,12 +46,22 @@ def compact(number): return number +def validate(number): + """Checks to see if the number provided is a valid OIB number. This + checks the length, formatting and check digit.""" + number = compact(number) + if not number.isdigit(): + raise InvalidFormat() + if len(number) != 11: + raise InvalidLength() + mod_11_10.validate(number) + return number + + def is_valid(number): """Checks to see if the number provided is a valid OIB number. This checks the length, formatting and check digit.""" try: - number = compact(number) - except: + return bool(validate(number)) + except ValidationError: return False - return len(number) == 11 and number.isdigit() and \ - mod_11_10.is_valid(number) |