Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/dk
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2013-05-17 11:47:20 +0200
committerArthur de Jong <arthur@arthurdejong.org>2013-06-08 14:45:39 +0200
commit8caecc5c3d0036faf9d2344b052d0e7ab25ce533 (patch)
tree8b33a947e7d1e6bf93943eda5b415faf8818685e /stdnum/dk
parent360480bc26844e8c8e1e4fbbc154204b30a9337b (diff)
Implement validate() for Danish numbers
Diffstat (limited to 'stdnum/dk')
-rw-r--r--stdnum/dk/cpr.py49
-rw-r--r--stdnum/dk/cvr.py32
2 files changed, 51 insertions, 30 deletions
diff --git a/stdnum/dk/cpr.py b/stdnum/dk/cpr.py
index 7a4cc0d..9be2cec 100644
--- a/stdnum/dk/cpr.py
+++ b/stdnum/dk/cpr.py
@@ -1,6 +1,6 @@
# cpr.py - functions for handling Danish CPR numbers
#
-# 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
@@ -27,14 +27,14 @@ digit of the sequence number indicates the century.
The numbers used to validate using a checksum but since the sequence
numbers ran out this was abandoned in 2007.
->>> compact('211062-5629')
+>>> validate('211062-5629')
'2110625629'
->>> is_valid('211062-5629')
-True
>>> checksum('2110625629')
0
->>> is_valid('511062-5629') # invalid date
-False
+>>> validate('511062-5629') # invalid date
+Traceback (most recent call last):
+ ...
+InvalidComponent: ...
>>> get_birth_date('2110620629')
datetime.date(1962, 10, 21)
>>> get_birth_date('2110525629')
@@ -45,6 +45,7 @@ datetime.date(2052, 10, 21)
import datetime
+from stdnum.exceptions import *
from stdnum.util import clean
@@ -72,25 +73,33 @@ def get_birth_date(number):
year += 1900
else:
year += 2000
- return datetime.date(year, month, day)
+ try:
+ return datetime.date(year, month, day)
+ except ValueError:
+ raise InvalidComponent()
-def is_valid(number):
- """Checks to see if the number provided is a valid VAT number. This checks
- the length, formatting and check digit."""
- try:
- number = compact(number)
- except:
- return False
- if not number.isdigit() or len(number) != 10:
- return False
+def validate(number):
+ """Checks to see if the number provided is a valid CPR number. This
+ checks the length, formatting, embedded date and check digit."""
+ number = compact(number)
+ if not number.isdigit():
+ raise InvalidFormat()
+ if len(number) != 10:
+ raise InvalidLength()
# check if birth date is valid
+ birth_date = get_birth_date(number)
+ # TODO: check that the birth date is not in the future
+ return number
+
+
+def is_valid(number):
+ """Checks to see if the number provided is a valid CPR number. This
+ checks the length, formatting, embedded date and check digit."""
try:
- birth_date = get_birth_date(number)
- # TODO: check that the birth date is not in the future
- except ValueError:
+ return bool(validate(number))
+ except ValidationError:
return False
- return True
def format(number):
diff --git a/stdnum/dk/cvr.py b/stdnum/dk/cvr.py
index 6dbbfe8..7ded1ac 100644
--- a/stdnum/dk/cvr.py
+++ b/stdnum/dk/cvr.py
@@ -1,6 +1,6 @@
# cvr.py - functions for handling Danish CVR numbers
#
-# 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
@@ -22,14 +22,15 @@
The CVR (Momsregistreringsnummer, VAT) is an 8 digit number with a
straightforward check mechanism.
->>> compact('DK 13585628')
+>>> validate('DK 13585628')
'13585628'
->>> is_valid('DK 13585628')
-True
->>> is_valid('DK 13585627')
-False
+>>> validate('DK 13585627')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
"""
+from stdnum.exceptions import *
from stdnum.util import clean
@@ -48,12 +49,23 @@ def checksum(number):
return sum(weights[i] * int(n) for i, n in enumerate(number)) % 11
+def validate(number):
+ """Checks to see if the number provided is a valid VAT number. This checks
+ the length, formatting and check digit."""
+ number = compact(number)
+ if not number.isdigit() or number[0] == '0':
+ raise InvalidFormat()
+ if len(number) != 8:
+ raise InvalidLength()
+ if checksum(number) != 0:
+ raise InvalidChecksum()
+ return number
+
+
def is_valid(number):
"""Checks to see if the number provided is a valid VAT 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) == 8 and number.isdigit() and \
- number[0] != '0' and checksum(number) == 0