lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.1-14-ge88ba0b

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

python-stdnum branch master updated. 1.1-14-ge88ba0b



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  e88ba0b7cb372e2d9bf56bb291ea5ed2f53de840 (commit)
       via  243e50f063f3216392c237bcf1807b76315fbf31 (commit)
      from  7f9c94f99dcaed8c97d6288d9fbbc483a963c2d7 (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 -----------------------------------------------------------------
http://arthurdejong.org/git/python-stdnum/commit/?id=e88ba0b7cb372e2d9bf56bb291ea5ed2f53de840

commit e88ba0b7cb372e2d9bf56bb291ea5ed2f53de840
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sun Aug 16 11:20:58 2015 +0200

    Small typo and code style fixes

diff --git a/stdnum/dk/cpr.py b/stdnum/dk/cpr.py
index 9be2cec..456be21 100644
--- a/stdnum/dk/cpr.py
+++ b/stdnum/dk/cpr.py
@@ -81,7 +81,7 @@ def get_birth_date(number):
 
 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."""
+    checks the length, formatting, embedded date and check digit."""
     number = compact(number)
     if not number.isdigit():
         raise InvalidFormat()
@@ -95,7 +95,7 @@ def validate(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."""
+    checks the length, formatting, embedded date and check digit."""
     try:
         return bool(validate(number))
     except ValidationError:
diff --git a/stdnum/is_/kennitala.py b/stdnum/is_/kennitala.py
index 39aaf6e..5ffb73f 100644
--- a/stdnum/is_/kennitala.py
+++ b/stdnum/is_/kennitala.py
@@ -89,7 +89,7 @@ def validate(number):
     # check if birth date or registration data is valid
     try:
         if day >= 40:  # organisation
-            datetime.date(year, month, day-40)
+            datetime.date(year, month, day - 40)
         else:  # individual
             datetime.date(year, month, day)
     except ValueError:
diff --git a/stdnum/it/codicefiscale.py b/stdnum/it/codicefiscale.py
index 63b7f6e..25569aa 100644
--- a/stdnum/it/codicefiscale.py
+++ b/stdnum/it/codicefiscale.py
@@ -92,7 +92,7 @@ def calc_check_digit(number):
 
 
 def get_birth_date(number, minyear=1920):
-    """Get the birth date from the person's whose fiscal code.
+    """Get the birth date from the person's fiscal code.
 
     Only the last two digits of the year are stured in the number. The
     dates will be returned in the range from minyear to minyear + 100.
@@ -117,7 +117,7 @@ def get_birth_date(number, minyear=1920):
 
 
 def get_gender(number):
-    """Get the gender of the person's provided fiscal code.
+    """Get the gender of the person's fiscal code.
 
     >>> get_gender('RCCMNL83S18D969H')
     'M'

http://arthurdejong.org/git/python-stdnum/commit/?id=243e50f063f3216392c237bcf1807b76315fbf31

commit 243e50f063f3216392c237bcf1807b76315fbf31
Author: Tomas Karasek <tom.to.the.k@gmail.com>
Date:   Mon Aug 3 15:32:25 2015 +0300

    Add validation of Estonian personal ID

diff --git a/stdnum/ee/ik.py b/stdnum/ee/ik.py
new file mode 100644
index 0000000..a22c7a2
--- /dev/null
+++ b/stdnum/ee/ik.py
@@ -0,0 +1,113 @@
+# ik.py - functions for handling Estonian Personal ID numbers (IK)
+# coding: utf-8
+#
+# Copyright (C) 2015 Tomas Karasek
+# Copyright (C) 2015 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
+# 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
+
+"""Isikukood (Estonian Personcal ID number).
+
+The number consists of 11 digits: the first indicates the gender and century
+the person was born in, the following 6 digits the birth date, followed by a
+3 digit serial and a check digit.
+
+>>> validate('36805280109')
+'36805280109'
+>>> validate('36805280108')  # incorrect check digit
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> get_birth_date('36805280109')
+datetime.date(1968, 5, 28)
+"""
+
+import datetime
+
+from stdnum.util import clean
+from stdnum.exceptions import *
+
+
+def compact(number):
+    """Convert the number to the minimal representation. This strips the
+    number of any valid separators and removes surrounding whitespace."""
+    return clean(number, ' ').strip()
+
+
+def get_birth_date(number):
+    """Split the date parts from the number and return the birth date."""
+    number = compact(number)
+    if number[0] in '12':
+        century = 1800
+    elif number[0] in '34':
+        century = 1900
+    elif number[0] in '56':
+        century = 2000
+    elif number[0] in '78':
+        century = 2100
+    else:
+        raise InvalidComponent()
+    year = century + int(number[1:3])
+    month = int(number[3:5])
+    day = int(number[5:7])
+    try:
+        return datetime.date(year, month, day)
+    except ValueError:
+        raise InvalidComponent()
+
+
+def get_gender(number):
+    """Get the person's birth gender ('M' or 'F')."""
+    number = compact(number)
+    if number[0] in '1357':
+        return 'M'
+    elif number[0] in '2468':
+        return 'F'
+    else:
+        raise InvalidComponent()
+
+
+def calc_check_digit(number):
+    """Calculate the check digit."""
+    check = sum(((i % 9) + 1) * int(n)
+                for i, n in enumerate(number[:-1])) % 11
+    if check == 10:
+        check = sum((((i + 2) % 9) + 1) * int(n)
+                    for i, n in enumerate(number[:-1])) % 11
+    return str(check % 10)
+
+
+def validate(number):
+    """Checks if the number provided is valid. This checks the length,
+    formatting, embedded date and check digit."""
+    number = compact(number)
+    if not number.isdigit():
+        raise InvalidFormat()
+    if len(number) != 11:
+        raise InvalidLength()
+    get_birth_date(number)
+    if number[-1] != calc_check_digit(number):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number):
+    """Checks if the number provided is valid. This checks the length,
+    formatting, embedded date and check digit."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_ee_ik.doctest b/tests/test_ee_ik.doctest
new file mode 100644
index 0000000..b5e1453
--- /dev/null
+++ b/tests/test_ee_ik.doctest
@@ -0,0 +1,125 @@
+test_ee_ik.doctest - test for estonian personal id
+
+Copyright (C) 2015 Tomas Karasek
+Copyright (C) 2015 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
+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
+
+
+>>> from stdnum.ee import ik
+>>> import stdnum.exceptions
+
+>>> ik.validate('36805280109')
+'36805280109'
+>>> ik.is_valid('36805280109')
+True
+>>> ik.calc_check_digit('36805280109')
+'9'
+>>> ik.validate('06805280106')  # invalid first digit
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> ik.validate('36805280108')  # invalid check digit
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> ik.validate('368052801099')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+
+
+>>> ik.get_birth_date('36805280109')
+datetime.date(1968, 5, 28)
+>>> ik.get_birth_date('16805280107')
+datetime.date(1868, 5, 28)
+>>> ik.get_birth_date('51205280105')
+datetime.date(2012, 5, 28)
+>>> ik.get_birth_date('81205280108')
+datetime.date(2112, 5, 28)
+>>> ik.get_birth_date('06805280106')  # invalid first digit
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> ik.get_birth_date('38102290106')  # non-existing date
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+
+
+>>> ik.get_gender('36805280109')
+'M'
+>>> ik.get_gender('46805280103')
+'F'
+>>> ik.get_gender('98102290101')  # invalid first digit
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+
+
+>>> numbers = """
+... 36205030034
+... 36606130166
+... 38002090113
+... 36703010079
+... 36412140053
+... 37105250048
+... 35806110178
+... 38411280151
+... 38004160054
+... 37406220030
+... 37207010076
+... 46104090101
+... 47306160017
+... 35712020095
+... 35512240278
+... 37111070056
+... 36003050128
+... 34508136020
+... 37112300117
+... 37205120111
+... 36708120106
+... 36204130100
+... 36805280109
+... 36404240119
+... 37609300174
+... 38407170099
+... 35903140121
+... 36912050058
+... 36706060097
+... 37909180161
+... 37210220129
+... 35803140053
+... 37709190107
+... 36306200109
+... 36208130099
+... 37611280079
+... 35806190146
+... 44909210102
+... 37104020141
+... 35907150159
+... 36412100145
+... 49105080018
+... 37406110083
+... 36304020091
+... 37106220087
+... 34706045216
+... 37503240119
+... 38310150127
+... 46708270050
+... """
+>>> [x for x in numbers.splitlines() if x and not ik.is_valid(x)]
+[]

-----------------------------------------------------------------------

Summary of changes:
 stdnum/dk/cpr.py           |    4 +-
 stdnum/ee/ik.py            |  113 +++++++++++++++++++++++++++++++++++++++
 stdnum/is_/kennitala.py    |    2 +-
 stdnum/it/codicefiscale.py |    4 +-
 tests/test_ee_ik.doctest   |  125 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 243 insertions(+), 5 deletions(-)
 create mode 100644 stdnum/ee/ik.py
 create mode 100644 tests/test_ee_ik.doctest


hooks/post-receive
-- 
python-stdnum
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/