lists.arthurdejong.org
RSS feed

python-stdnum commit: r134 - in python-stdnum: . stdnum stdnum/ie tests

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

python-stdnum commit: r134 - in python-stdnum: . stdnum stdnum/ie tests



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

Log:
add a VAT (Irish VAT number) module

Added:
   python-stdnum/stdnum/ie/
   python-stdnum/stdnum/ie/__init__.py
   python-stdnum/stdnum/ie/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 17 17:01:58 2012        (r133)
+++ python-stdnum/README        Fri Feb 17 17:10:02 2012        (r134)
@@ -46,6 +46,7 @@
  * IČ DPH (Identifikačné číslo pre daň z pridanej hodnoty, Slovak VAT number)
  * ALV nro (Arvonlisäveronumero, Finnish VAT number)
  * DIČ (Daňové identifikační číslo, Czech VAT number)
+ * VAT (Irish VAT number)
  * ANUM (Közösségi adószám, Hungarian VAT number)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)

Modified: python-stdnum/stdnum/__init__.py
==============================================================================
--- python-stdnum/stdnum/__init__.py    Fri Feb 17 17:01:58 2012        (r133)
+++ python-stdnum/stdnum/__init__.py    Fri Feb 17 17:10:02 2012        (r134)
@@ -60,6 +60,7 @@
  * IČ DPH (Identifikačné číslo pre daň z pridanej hodnoty, Slovak VAT number)
  * ALV nro (Arvonlisäveronumero, Finnish VAT number)
  * DIČ (Daňové identifikační číslo, Czech VAT number)
+ * VAT (Irish VAT number)
  * ANUM (Közösségi adószám, Hungarian VAT number)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)

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

Added: python-stdnum/stdnum/ie/vat.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/ie/vat.py      Fri Feb 17 17:10:02 2012        (r134)
@@ -0,0 +1,73 @@
+# vat.py - functions for handling Irish 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 Irish VAT numbers.
+
+The number consists of 8 digits. The last digit is a check letter, the
+second may be a number, letter, "+" or "*".
+
+>>> compact('IE 6433435F')
+'6433435F'
+>>> is_valid('6433435F')
+True
+>>> is_valid('6433435E')  # incorrect check digit
+False
+>>> is_valid('8D79739I')  # old style number
+True
+>>> is_valid('8?79739J')  # incorrect old style
+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('IE'):
+        number = number[2:]
+    return number
+
+
+def calc_check_digit(number):
+    """Calculate the check digit. The number passed should not have the
+    check digit included."""
+    alphabet = 'WABCDEFGHIJKLMNOPQRSTUV'
+    number = (7 - len(number)) * '0' + number
+    return alphabet[sum((8 - i) * int(n) for i, n in enumerate(number)) % 23]
+
+
+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) != 8 or not number[0].isdigit() or \
+       not number[2:7].isdigit():
+         return False
+    if number[:7].isdigit():
+        # new system
+        return number[-1] == calc_check_digit(number[:-1])
+    if number[1] in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ+*':
+        # old system
+        return number[-1] == calc_check_digit(number[2:-1] + number[0])
+    return False

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Fri Feb 17 17:01:58 2012        
(r133)
+++ python-stdnum/tests/test_robustness.doctest Fri Feb 17 17:10:02 2012        
(r134)
@@ -38,6 +38,7 @@
 >>> from stdnum.fr import siren
 >>> from stdnum.gr import vat as gr_vat
 >>> from stdnum.hu import anum
+>>> from stdnum.ie import vat as ie_vat
 >>> from stdnum.iso7064 import mod_11_10, mod_11_2, mod_37_2, mod_37_36, 
 >>> mod_97_10
 >>> from stdnum.it import iva
 >>> from stdnum.lu import tva
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/