lists.arthurdejong.org
RSS feed

python-stdnum commit: r92 - in python-stdnum: . stdnum stdnum/fi tests

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum commit: r92 - in python-stdnum: . stdnum stdnum/fi tests



Author: arthur
Date: Mon Nov  7 23:07:15 2011
New Revision: 92
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=92&view=revision

Log:
add a HETU (Finnish personal identity code) module as provided by Jussi Judin 
(#5)

Added:
   python-stdnum/stdnum/fi/   (props changed)
   python-stdnum/stdnum/fi/__init__.py
   python-stdnum/stdnum/fi/hetu.py
   python-stdnum/tests/test_fi_hetu.doctest
Modified:
   python-stdnum/README
   python-stdnum/stdnum/__init__.py
   python-stdnum/tests/test_robustness.doctest

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Fri Sep 30 10:45:13 2011        (r91)
+++ python-stdnum/README        Mon Nov  7 23:07:15 2011        (r92)
@@ -18,6 +18,7 @@
  * CPF (Cadastro de Pessoas Físicas, the Brazillian national identification
    number)
  * SSN (U.S. Social Security Number)
+ * HETU (Finnish personal identity code)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)
  * MEID (Mobile Equipment Identifier)

Modified: python-stdnum/stdnum/__init__.py
==============================================================================
--- python-stdnum/stdnum/__init__.py    Fri Sep 30 10:45:13 2011        (r91)
+++ python-stdnum/stdnum/__init__.py    Mon Nov  7 23:07:15 2011        (r92)
@@ -32,6 +32,7 @@
  * CPF (Cadastro de Pessoas Físicas, the Brazillian national identification
    number)
  * SSN (U.S. Social Security Number)
+ * HETU (Finnish personal identity code)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)
  * MEID (Mobile Equipment Identifier)

Added: python-stdnum/stdnum/fi/__init__.py
==============================================================================

Added: python-stdnum/stdnum/fi/hetu.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/fi/hetu.py     Mon Nov  7 23:07:15 2011        (r92)
@@ -0,0 +1,94 @@
+# hetu.py - functions for handling Finnish personal identity codes
+# coding: utf-8
+#
+# Copyright (C) 2011 Jussi Judin
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""Module for handling Finnish personal identity codes (HETU, Henkilötunnus).
+
+See http://www.vaestorekisterikeskus.fi/default.aspx?id=45 for checksum
+calculation details and http://tarkistusmerkit.teppovuori.fi/tarkmerk.htm#hetu1
+for historical details.
+
+>>> is_valid('131052-308T')
+True
+>>> is_valid('131052-308U')
+False
+>>> is_valid('310252-308Y')
+False
+>>> compact('131052a308t')
+'131052A308T'
+"""
+
+import re
+import datetime
+
+
+_century_codes = {
+    '+': 1800,
+    '-': 1900,
+    'A': 2000,
+    }
+
+# Finnish personal identity codes are composed of date part, century
+# indicating sign, individual number and control character.
+# ddmmyyciiiC
+_hetu_re = re.compile(r'^(?P<day>[0123]\d)(?P<month>[01]\d)(?P<year>\d\d)'
+                      r'(?P<century>[-+A])(?P<individual>\d\d\d)'
+                      r'(?P<control>[0-9ABCDEFHJKLMNPRSTUVWXY])$')
+
+
+def compact(number):
+    """Convert the HETU to the minimal representation. This strips
+    surrounding whitespace and converts it to upper case."""
+    return number.strip().upper()
+
+
+def _calc_checksum(number):
+    return '0123456789ABCDEFHJKLMNPRSTUVWXY'[int(number) % 31]
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid HETU. It checks the
+    format, whether a valid date is given and whether the check digit is
+    correct."""
+    try:
+        match = _hetu_re.search(compact(number))
+        if not match:
+            return False
+    except:
+        return False
+    day = int(match.group('day'))
+    month = int(match.group('month'))
+    year = int(match.group('year'))
+    century = _century_codes[match.group('century')]
+    individual = int(match.group('individual'))
+    # check if birth date is valid
+    try:
+        datetime.date(century + year, month, day)
+    except ValueError, e:
+        return False
+    # for historical reasons individual IDs start from 002
+    if individual < 2:
+        return False
+    checkable_number = '%02d%02d%02d%03d' % (day, month, year, individual)
+    return match.group('control') == _calc_checksum(checkable_number)
+
+
+# This is here just for completeness as there are no different length forms
+# of Finnish personal identity codes:
+format = compact

Added: python-stdnum/tests/test_fi_hetu.doctest
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/tests/test_fi_hetu.doctest    Mon Nov  7 23:07:15 2011        
(r92)
@@ -0,0 +1,77 @@
+test_fi_hetun.doctest - more detailed doctests for stdnum.fi.hetu module
+
+Copyright (C) 2011 Jussi Judin
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA
+
+
+This file contains more detailed doctests for the stdnum.fi.hetu. It
+tries to cover more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum.fi import hetu
+
+
+Normal values that should just work.
+
+>>> hetu.is_valid('131052-308T')
+True
+>>> hetu.is_valid('131052+308T')
+True
+>>> hetu.is_valid('131052A308T')
+True
+>>> hetu.is_valid('131052a308t')
+True
+
+
+Invalid checksum:
+
+>>> hetu.is_valid('131052-308U')
+False
+
+
+Invalid century indicator:
+
+>>> hetu.is_valid('131052/308T')
+False
+>>> hetu.is_valid('131052T308T')
+False
+
+
+Invalid birth date:
+
+>>> hetu.is_valid('310252-308Y')
+False
+>>> hetu.is_valid('130052-308R')
+False
+
+Leaving out the first zero is wrong:
+
+>>> hetu.is_valid('10101-0101')
+False
+
+
+Invalid individual number:
+(for historical reasons individual IDs start from 002)
+
+>>> hetu.is_valid('131052-000V')
+False
+
+
+compact() and format() don't do much special:
+
+>>> hetu.compact('131052a308t')
+'131052A308T'

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Fri Sep 30 10:45:13 2011        
(r91)
+++ python-stdnum/tests/test_robustness.doctest Mon Nov  7 23:07:15 2011        
(r92)
@@ -30,6 +30,7 @@
 >>> from stdnum.nl import bsn
 >>> from stdnum.br import cpf
 >>> from stdnum.us import ssn
+>>> from stdnum.fi import hetu
 
 Go over each imported module and try every value.
 
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/