lists.arthurdejong.org
RSS feed

python-stdnum commit: r144 - in python-stdnum: . stdnum stdnum/gb tests

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

python-stdnum commit: r144 - in python-stdnum: . stdnum stdnum/gb tests



Author: arthur
Date: Sat Feb 18 17:08:18 2012
New Revision: 144
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=144&view=revision

Log:
add a VAT (United Kingdom (and Isle of Man) VAT registration number) module

Added:
   python-stdnum/stdnum/gb/
   python-stdnum/stdnum/gb/__init__.py
   python-stdnum/stdnum/gb/vat.py
   python-stdnum/tests/test_gb_vat.doctest
Modified:
   python-stdnum/README
   python-stdnum/stdnum/__init__.py
   python-stdnum/tests/test_robustness.doctest

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Sat Feb 18 17:02:36 2012        (r143)
+++ python-stdnum/README        Sat Feb 18 17:08:18 2012        (r144)
@@ -56,6 +56,7 @@
  * NIP (Numer Identyfikacji Podatkowej, Polish VAT number)
  * ID za DDV (Davčna številka, Slovenian VAT number)
  * VAT (Moms, Mervärdesskatt, Swedish VAT number)
+ * VAT (United Kingdom (and Isle of Man) VAT registration 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    Sat Feb 18 17:02:36 2012        (r143)
+++ python-stdnum/stdnum/__init__.py    Sat Feb 18 17:08:18 2012        (r144)
@@ -70,6 +70,7 @@
  * NIP (Numer Identyfikacji Podatkowej, Polish VAT number)
  * ID za DDV (Davčna številka, Slovenian VAT number)
  * VAT (Moms, Mervärdesskatt, Swedish VAT number)
+ * VAT (United Kingdom (and Isle of Man) VAT registration number)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)
  * MEID (Mobile Equipment Identifier)

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

Added: python-stdnum/stdnum/gb/vat.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/gb/vat.py      Sat Feb 18 17:08:18 2012        (r144)
@@ -0,0 +1,89 @@
+# vat.py - functions for handling United Kingdom 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 United Kingdom (and Isle of Man) VAT registration
+numbers.
+
+The number can either be a 9-digit standard number, a 12-digit standard
+number followed by a 3-digit branch identifyier, a 5-digit number for
+government departments (first two digits are GD) or a 5-digit number for
+health authorities (first two digits are HA). The 9-digit part contains a
+weighted checksum.
+
+>>> compact('GB 980 7806 84')
+'980780684'
+>>> is_valid('980780684')
+True
+>>> is_valid('802311781')  # invalid check digit
+False
+>>> format('980780684')
+'980 7806 84'
+"""
+
+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('GB'):
+        number = number[2:]
+    return number
+
+
+def checksum(number):
+    """Calculate the checksum."""
+    weights = (8, 7, 6, 5, 4, 3, 2, 10, 1)
+    return sum(weights[i] * int(n) for i, n in enumerate(number)) % 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
+    if len(number) == 5 and number.startswith('GD') and number[2:].isdigit():
+        # government department
+        return int(number[2:]) < 500
+    if len(number) == 5 and number.startswith('HA') and number[2:].isdigit():
+        # health authority
+        return int(number[2:]) >= 500
+    if len(number) == 12 and number.isdigit():
+        # branch trader: nnn nnnn nn nnn (ignore the last thee digits)
+        return checksum(number[:-3]) in (0, 42)
+    if len(number) == 9 and number.isdigit():
+        # standard number: nnn nnnn nn
+        return checksum(number) in (0, 42)
+    return False
+
+
+def format(number):
+    """Reformat the passed number to the standard format."""
+    number = compact(number)
+    if len(number) == 5:
+        # government department or health authority
+        return number
+    if len(number) == 12:
+        # includes branch number
+        return number[:3] + ' ' + number[3:7] + ' ' + number[7:9] + ' ' + 
number[9:]
+    # standard number: nnn nnnn nn
+    return number[:3] + ' ' + number[3:7] + ' ' + number[7:]

Added: python-stdnum/tests/test_gb_vat.doctest
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/tests/test_gb_vat.doctest     Sat Feb 18 17:08:18 2012        
(r144)
@@ -0,0 +1,53 @@
+test_gb_vat.doctest - more detailed doctests for stdnum.gb.vat module
+
+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
+
+
+This file contains more detailed doctests for the stdnum.gb.vat. It
+tries to cover more corner cases and detailed functionality that is not
+really useful as module documentation.
+
+>>> from stdnum.gb import vat
+
+
+Normal values that should just work.
+
+>>> vat.is_valid('980780684')  # standard number
+True
+>>> vat.is_valid('242338087388')  # branch trader
+True
+>>> vat.is_valid('GD100')  # government department
+True
+>>> vat.is_valid('HA501')  # health authority
+True
+
+
+Invalid checksum:
+
+>>> vat.is_valid('802311781')  # invalid check digit
+False
+
+
+Formatting tests:
+
+>>> vat.format('980780684')  # standard number
+'980 7806 84'
+>>> vat.format('HA501')  # health authority
+'HA501'
+>>> vat.format('242338087388')  # branch trader
+'242 3380 87 388'

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Sat Feb 18 17:02:36 2012        
(r143)
+++ python-stdnum/tests/test_robustness.doctest Sat Feb 18 17:08:18 2012        
(r144)
@@ -37,6 +37,7 @@
 >>> from stdnum.es import cif, dni, nie, nif as es_nif
 >>> from stdnum.fi import hetu, alv
 >>> from stdnum.fr import siren, tva as fr_tva
+>>> from stdnum.gb import vat as gb_vat
 >>> from stdnum.gr import vat as gr_vat
 >>> from stdnum.hu import anum
 >>> from stdnum.ie import vat as ie_vat
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/