python-stdnum branch master updated. 1.13-4-g353db92
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
python-stdnum branch master updated. 1.13-4-g353db92
- From: Commits of the python-stdnum project <python-stdnum-commits [at] lists.arthurdejong.org>
- To: python-stdnum-commits [at] lists.arthurdejong.org
- Reply-to: python-stdnum-users [at] lists.arthurdejong.org, python-stdnum-commits [at] lists.arthurdejong.org
- Subject: python-stdnum branch master updated. 1.13-4-g353db92
- Date: Sun, 1 Mar 2020 19:24:11 +0100 (CET)
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".
The branch, master has been updated
via 353db92b2da365381d590dbc535e1b2b68db0732 (commit)
from efa70f1ece9f30bf7e6a8600534bbb37e751c53e (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=353db92b2da365381d590dbc535e1b2b68db0732
commit 353db92b2da365381d590dbc535e1b2b68db0732
Author: Aleksi Hoffman <aleksi@hoffman.fi>
Date: Mon Feb 24 08:54:15 2020 +0200
Support validating temporary Finnish personal identity codes
This adds an optional allow_temporary parameter, which defaults to
False, to the validate() function.
Closes https://github.com/arthurdejong/python-stdnum/pull/191
diff --git a/stdnum/fi/hetu.py b/stdnum/fi/hetu.py
index 3fda6a9..334985f 100644
--- a/stdnum/fi/hetu.py
+++ b/stdnum/fi/hetu.py
@@ -3,6 +3,7 @@
#
# Copyright (C) 2011 Jussi Judin
# Copyright (C) 2012, 2013 Arthur de Jong
+# Copyright (C) 2020 Aleksi Hoffman
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -71,9 +72,12 @@ def _calc_checksum(number):
return '0123456789ABCDEFHJKLMNPRSTUVWXY'[int(number) % 31]
-def validate(number):
+def validate(number, allow_temporary=False):
"""Check if the number is a valid HETU. It checks the format, whether a
- valid date is given and whether the check digit is correct."""
+ valid date is given and whether the check digit is correct. Allows
+ temporary identifier range for individuals (900-999) if allow_temporary
+ is True.
+ """
number = compact(number)
match = _hetu_re.search(number)
if not match:
@@ -92,7 +96,7 @@ def validate(number):
if individual < 2:
raise InvalidComponent()
# this range is for temporary identifiers
- if 900 <= individual <= 999:
+ if 900 <= individual <= 999 and not allow_temporary:
raise InvalidComponent()
checkable_number = '%02d%02d%02d%03d' % (day, month, year, individual)
if match.group('control') != _calc_checksum(checkable_number):
@@ -100,10 +104,10 @@ def validate(number):
return number
-def is_valid(number):
+def is_valid(number, allow_temporary=False):
"""Check if the number is a valid HETU."""
try:
- return bool(validate(number))
+ return bool(validate(number, allow_temporary))
except ValidationError:
return False
diff --git a/tests/test_fi_hetu.doctest b/tests/test_fi_hetu.doctest
index b5b3f87..96701fd 100644
--- a/tests/test_fi_hetu.doctest
+++ b/tests/test_fi_hetu.doctest
@@ -2,6 +2,7 @@ test_fi_hetu.doctest - more detailed doctests for
stdnum.fi.hetu module
Copyright (C) 2011 Jussi Judin
Copyright (C) 2013 Arthur de Jong
+Copyright (C) 2020 Aleksi Hoffman
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -78,8 +79,10 @@ Traceback (most recent call last):
InvalidFormat: ...
-Invalid individual number: (for historical reasons individual IDs start from
-002 and the range from 900 to 999 is used as temporary identifiers)
+Test for invalid individual numbers. For historical reasons individual IDs
+start from 002. The range from 900 to 999 is used as temporary identifiers
+(which should only be allowed when `allow_temporary` parameter is explicitly
+set to True).
>>> hetu.validate('131052-000V')
Traceback (most recent call last):
@@ -89,9 +92,13 @@ InvalidComponent: ...
Traceback (most recent call last):
...
InvalidComponent: ...
+>>> hetu.validate('131052-9993', allow_temporary=True)
+'131052-9993'
compact() and format() don't do much special:
>>> hetu.compact('131052a308t')
'131052A308T'
+>>> hetu.format('131052a308t')
+'131052A308T'
-----------------------------------------------------------------------
Summary of changes:
stdnum/fi/hetu.py | 14 +++++++++-----
tests/test_fi_hetu.doctest | 11 +++++++++--
2 files changed, 18 insertions(+), 7 deletions(-)
hooks/post-receive
--
python-stdnum
- python-stdnum branch master updated. 1.13-4-g353db92,
Commits of the python-stdnum project