lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.6-16-g707b63c

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

python-stdnum branch master updated. 1.6-16-g707b63c



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  707b63c42493ce700a1c3f8c624626e969702766 (commit)
      from  a71a1acedaa81c95042337856e4f5ecef84dfdd6 (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=707b63c42493ce700a1c3f8c624626e969702766

commit 707b63c42493ce700a1c3f8c624626e969702766
Author: Mohammed Salman <mohammed@holvi.com>
Date:   Thu Aug 31 16:40:35 2017 +0300

    Add validator for German steuernummer

diff --git a/stdnum/de/idnr.py b/stdnum/de/idnr.py
new file mode 100644
index 0000000..a9952fd
--- /dev/null
+++ b/stdnum/de/idnr.py
@@ -0,0 +1,94 @@
+# idnr.py - functions for handling German tax id
+# coding: utf-8
+#
+# Copyright (C) 2017 Holvi Payment Services Oy
+# Copyright (C) 2017 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
+
+"""IdNr (Steuerliche Identifikationsnummer, German personal tax number).
+
+The IdNr (or Steuer-IdNr) is a personal identification number that is
+assigned to individuals in Germany for tax purposes and is meant to replace
+the Steuernummer. The number consists of 11 digits and does not embed any
+personal information.
+
+More information:
+
+* https://de.wikipedia.org/wiki/Steuerliche_Identifikationsnummer
+* http://www.identifikationsmerkmal.de/
+
+>>> validate('36 574 261 809')
+'36574261809'
+>>> validate('36574261890')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('36554266806')  # more digits repeated
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> format('36574261809')
+'36 574 261 809'
+"""
+
+from collections import defaultdict
+
+from stdnum.exceptions import *
+from stdnum.iso7064 import mod_11_10
+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, ' -./,').strip()
+
+
+def validate(number):
+    """Checks to see if the number provided is a valid tax identification
+    number. This checks the length, formatting and check digit."""
+    number = compact(number)
+    if len(number) != 11:
+        raise InvalidLength()
+    if not number.isdigit():
+        raise InvalidFormat()
+    if number.startswith('0'):
+        raise InvalidFormat()
+    # In the first 10 digits exactly one digit must be repeated two or
+    # three times and other digits can appear only once.
+    counter = defaultdict(int)
+    for n in number[:10]:
+        counter[n] += 1
+    counts = [c for c in counter.values() if c > 1]
+    if len(counts) != 1 or counts[0] not in (2, 3):
+        raise InvalidFormat()
+    return mod_11_10.validate(number)
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid tax identification
+    number. This checks the length, formatting and check digit."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
+
+
+def format(number):
+    """Reformat the passed number to the standard format."""
+    number = compact(number)
+    return ' '.join((number[:2], number[2:5], number[5:8], number[8:]))
diff --git a/tests/test_de_idnr.doctest b/tests/test_de_idnr.doctest
new file mode 100644
index 0000000..ecd65ed
--- /dev/null
+++ b/tests/test_de_idnr.doctest
@@ -0,0 +1,66 @@
+test_de_idnr.doctest - more detailed doctests for stdnum.de.idnr module
+
+Copyright (C) 2017 Holvi Payment Services Oy
+Copyright (C) 2017 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
+
+
+This file contains more detailed doctests for the stdnum.de.idnr module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.de import idnr
+
+
+The number should be 11 digits long, should contain only numbers and cannot
+start with a zero.
+
+>>> idnr.validate('116574261809')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> idnr.validate('A6574261809')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> idnr.validate('01234567896')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+
+
+The first 10 digits of the IdNr is supposed to contain extactly one digit
+twice (or three times since 2016) and all other digits in the number can only
+appear once. This tries to catch some corner cases. Note that only the first
+10 digits are considered for this.
+
+>>> idnr.validate('1234567890 3')  # each digit once
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> idnr.validate('1123456789 0')  # one digit twice
+'11234567890'
+>>> idnr.validate('1112345678 6')  # one digit three times
+'11123456786'
+>>> idnr.validate('1111234567 8')  # one digit four times
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> idnr.validate('1122345678 5')  # two digits more than once
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...

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

Summary of changes:
 stdnum/de/idnr.py          | 94 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_de_idnr.doctest | 66 ++++++++++++++++++++++++++++++++
 2 files changed, 160 insertions(+)
 create mode 100644 stdnum/de/idnr.py
 create mode 100644 tests/test_de_idnr.doctest


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