lists.arthurdejong.org
RSS feed

python-stdnum commit: r109 - in python-stdnum: . stdnum stdnum/be tests

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

python-stdnum commit: r109 - in python-stdnum: . stdnum stdnum/be tests



Author: arthur
Date: Fri Feb 10 14:33:23 2012
New Revision: 109
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=109&view=revision

Log:
add a BTW, TVA, NWSt (Belgian VAT number) module

Added:
   python-stdnum/stdnum/be/
   python-stdnum/stdnum/be/__init__.py
   python-stdnum/stdnum/be/vat.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:27:39 2012        (r108)
+++ python-stdnum/README        Fri Feb 10 14:33:23 2012        (r109)
@@ -26,6 +26,7 @@
  * HETU (Finnish personal identity code)
  * FPA, ΦΠΑ (Foros Prostithemenis Aksias, the Greek VAT number)
  * Ust ID Nr. (Umsatzsteur Identifikationnummer, the German VAT number)
+ * BTW, TVA, NWSt (Belgian VAT number)
  * 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 Feb 10 14:27:39 2012        (r108)
+++ python-stdnum/stdnum/__init__.py    Fri Feb 10 14:33:23 2012        (r109)
@@ -40,6 +40,7 @@
  * HETU (Finnish personal identity code)
  * FPA, ΦΠΑ (Foros Prostithemenis Aksias, the Greek VAT number)
  * Ust ID Nr. (Umsatzsteur Identifikationnummer, the German VAT number)
+ * BTW, TVA, NWSt (Belgian VAT number)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)
  * MEID (Mobile Equipment Identifier)

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

Added: python-stdnum/stdnum/be/vat.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/be/vat.py      Fri Feb 10 14:33:23 2012        (r109)
@@ -0,0 +1,56 @@
+# vat.py - functions for handling Belgian VAT numbers
+#
+# 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 Belgian VAT (BTW TVA NWSt) numbers.
+
+>>> compact('BE403019261')
+'0403019261'
+>>> is_valid('BE 428759497')
+True
+>>> is_valid('BE431150351')
+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."""
+    number = clean(number, ' -./').upper().strip()
+    if number.startswith('BE'):
+        number = number[2:]
+    if len(number) == 9:
+        number = '0' + number  # old format had 9 digits
+    return number
+
+
+def checksum(number):
+    """Calculate the checksum."""
+    return (int(number[:-2]) + int(number[-2:])) % 97
+
+
+def is_valid(number):
+    """Checks to see if the number provided is a valid VAT number. This checks
+    the length, formatting and check digit."""
+    try:
+        number = compact(number)
+    except:
+        return False
+    return len(number) == 10 and number.isdigit() and checksum(number) == 0

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Fri Feb 10 14:27:39 2012        
(r108)
+++ python-stdnum/tests/test_robustness.doctest Fri Feb 10 14:33:23 2012        
(r109)
@@ -26,6 +26,7 @@
 
 >>> from stdnum import grid, iban, imei, imsi, isan, isbn, isil, ismn, issn, 
 >>> ean
 >>> from stdnum import luhn, meid, verhoeff
+>>> from stdnum.be import vat as be_vat
 >>> from stdnum.br import cpf
 >>> from stdnum.cz import rc
 >>> from stdnum.de import vat as de_vat
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/