lists.arthurdejong.org
RSS feed

python-stdnum commit: r150 - in python-stdnum: stdnum tests

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

python-stdnum commit: r150 - in python-stdnum: stdnum tests



Author: arthur
Date: Sun Feb 19 10:11:44 2012
New Revision: 150
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=150&view=revision

Log:
use introspection to find number modules and test them

Modified:
   python-stdnum/stdnum/util.py
   python-stdnum/tests/test_robustness.doctest

Modified: python-stdnum/stdnum/util.py
==============================================================================
--- python-stdnum/stdnum/util.py        Sat Feb 18 22:52:42 2012        (r149)
+++ python-stdnum/stdnum/util.py        Sun Feb 19 10:11:44 2012        (r150)
@@ -19,6 +19,8 @@
 
 """Common functions for other stdnum modules."""
 
+import pkgutil
+
 
 def clean(number, deletechars):
     """Remove the specified characters from the supplied number.
@@ -27,3 +29,15 @@
     '123456789'
     """
     return ''.join(x for x in number if x not in deletechars)
+
+
+def get_number_modules(base='stdnum'):
+    """Yield all the module and package names under the specified module."""
+    module = __import__(base, globals(), locals(), [base])
+    for loader, name, is_pkg in pkgutil.walk_packages(
+                    module.__path__, module.__name__ + '.',
+                    onerror=lambda x: None
+                ):
+        module = __import__(name, globals(), locals(), [name])
+        if hasattr(module, 'is_valid'):
+            yield module

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Sat Feb 18 22:52:42 2012        
(r149)
+++ python-stdnum/tests/test_robustness.doctest Sun Feb 19 10:11:44 2012        
(r150)
@@ -23,44 +23,11 @@
 invalid junk.
 
 >>> testvalues = ( None, '*&^%$', '', 0, False, object(), 'Q', 'QQ', '3')
+>>> from stdnum.util import get_number_modules
 
->>> from stdnum import grid, iban, imei, imsi, isan, isbn, isil, ismn, issn, 
ean
->>> from stdnum import luhn, meid, verhoeff
->>> from stdnum.at import uid
->>> from stdnum.be import vat as be_vat
->>> from stdnum.bg import egn, pnf, vat as bg_vat
->>> from stdnum.br import cpf
->>> from stdnum.cy import vat as cy_vat
->>> from stdnum.cz import dic, rc
->>> from stdnum.de import vat as de_vat
->>> from stdnum.dk import cvr
->>> from stdnum.ee import kmkr
->>> 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
->>> 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.lt import pvm
->>> from stdnum.lu import tva as lu_tva
->>> from stdnum.lv import pvn
->>> from stdnum.mt import vat as mt_vat
->>> from stdnum.nl import bsn, onderwijsnummer, btw
->>> from stdnum.pl import nip
->>> from stdnum.pt import nif as pt_nif
->>> from stdnum.ro import cf, cnp
->>> from stdnum.se import vat as se_vat
->>> from stdnum.si import ddv
->>> from stdnum.sk import dph
->>> from stdnum.us import ssn
+Go over each module and try every value.
 
-Go over each imported module and try every value.
-
->>> for mod in locals().values():
-...     if hasattr(mod, 'is_valid'):
-...         results = [ x for x in testvalues if mod.is_valid(x) != False ]
-...         if results:
-...             print mod.__name__, results
+>>> for mod in get_number_modules():
+...     results = [ x for x in testvalues if mod.is_valid(x) != False ]
+...     if results:
+...         print mod.__name__, results
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/