lists.arthurdejong.org
RSS feed

python-stdnum commit: r110 - in python-stdnum: . stdnum stdnum/es tests

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

python-stdnum commit: r110 - in python-stdnum: . stdnum stdnum/es tests



Author: arthur
Date: Fri Feb 10 21:49:17 2012
New Revision: 110
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=110&view=revision

Log:
add a DNI (Documento nacional de identidad, Spanish personal identity codes) 
module

Added:
   python-stdnum/stdnum/es/
   python-stdnum/stdnum/es/__init__.py
   python-stdnum/stdnum/es/dni.py
Modified:
   python-stdnum/README
   python-stdnum/stdnum/__init__.py
   python-stdnum/tests/test_robustness.doctest

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Fri Feb 10 14:33:23 2012        (r109)
+++ python-stdnum/README        Fri Feb 10 21:49:17 2012        (r110)
@@ -24,6 +24,7 @@
    company identification number)
  * SSN (U.S. Social Security Number)
  * HETU (Finnish personal identity code)
+ * DNI (Documento nacional de identidad, Spanish personal identity codes)
  * FPA, ΦΠΑ (Foros Prostithemenis Aksias, the Greek VAT number)
  * Ust ID Nr. (Umsatzsteur Identifikationnummer, the German VAT number)
  * BTW, TVA, NWSt (Belgian VAT number)

Modified: python-stdnum/stdnum/__init__.py
==============================================================================
--- python-stdnum/stdnum/__init__.py    Fri Feb 10 14:33:23 2012        (r109)
+++ python-stdnum/stdnum/__init__.py    Fri Feb 10 21:49:17 2012        (r110)
@@ -38,6 +38,7 @@
    company identification number)
  * SSN (U.S. Social Security Number)
  * HETU (Finnish personal identity code)
+ * DNI (Documento nacional de identidad, Spanish personal identity codes)
  * FPA, ΦΠΑ (Foros Prostithemenis Aksias, the Greek VAT number)
  * Ust ID Nr. (Umsatzsteur Identifikationnummer, the German VAT number)
  * BTW, TVA, NWSt (Belgian VAT number)

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

Added: python-stdnum/stdnum/es/dni.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/es/dni.py      Fri Feb 10 21:49:17 2012        (r110)
@@ -0,0 +1,59 @@
+# dni.py - functions for handling Spanish personal identity codes
+# coding: utf-8
+#
+# Copyright (C) 2012 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
+
+"""Module for handling Spanish personal identity codes (DNI, Documento
+nacional de identidad). The DNI is a 9 digit number where the last digit is
+a checksum letter.
+
+Foreign nationals, since 2010 are issued an NIE (Número de Identificación
+de Extranjeros, Foreigner's Identity Number) instead.
+
+>>> compact('54362315-K')
+'54362315K'
+>>> is_valid('54362315-K')
+True
+>>> is_valid('54362315Z')  # invalid check digit
+False
+"""
+
+from stdnum.util import clean
+
+
+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, ' -').upper().strip()
+
+
+def calc_check_digit(number):
+    """Calculate the check digit. The number passed should not have the
+    check digit included."""
+    return 'TRWAGMYFPDXBNJZSQVHLCKE'[int(number) % 23]
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid DNI number. This
+    checks the length, formatting and check digit."""
+    try:
+        number = compact(number)
+    except:
+        return False
+    return number[:-1].isdigit() and len(number) == 9 and \
+           calc_check_digit(number[:-1]) == number[-1]

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Fri Feb 10 14:33:23 2012        
(r109)
+++ python-stdnum/tests/test_robustness.doctest Fri Feb 10 21:49:17 2012        
(r110)
@@ -30,6 +30,7 @@
 >>> from stdnum.br import cpf
 >>> from stdnum.cz import rc
 >>> from stdnum.de import vat as de_vat
+>>> from stdnum.es import dni
 >>> from stdnum.fi import hetu
 >>> from stdnum.fr import siren
 >>> from stdnum.gr import vat as gr_vat
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/