lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.13-18-g8433821

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

python-stdnum branch master updated. 1.13-18-g8433821



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  843382165bc81471b7b037521be9712455412fb3 (commit)
      from  f7b968c986ad6163f92e1c72da20050f961f8954 (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=843382165bc81471b7b037521be9712455412fb3

commit 843382165bc81471b7b037521be9712455412fb3
Author: FabrizioMontanari <ffabrizio.montanari@gmail.com>
Date:   Tue Mar 3 18:16:45 2020 +0100

    Add Italian AIC codes
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/193

diff --git a/stdnum/it/aic.py b/stdnum/it/aic.py
new file mode 100644
index 0000000..4c6afc7
--- /dev/null
+++ b/stdnum/it/aic.py
@@ -0,0 +1,132 @@
+# aic.py - functions for handling Italian AIC codes
+# coding: utf-8
+#
+# This file is based on pyAIC Python library.
+# https://github.com/FabrizioMontanari/pyAIC
+#
+# Copyright (C) 2020 Fabrizio Montanari
+#
+# 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
+
+"""AIC (Italian code for identification of drugs).
+
+AIC codes are used to identify drugs allowed to be sold in Italy. Codes are
+issued by the Italian Drugs Agency (AIFA, Agenzia Italiana del Farmaco), the
+government authority responsible for drugs regulation in Italy.
+
+The number consists of 9 digits and includes a check digit.
+
+More information:
+
+* 
https://www.gazzettaufficiale.it/do/atto/serie_generale/caricaPdf?cdimg=14A0566800100010110001&dgu=2014-07-18&art.dataPubblicazioneGazzetta=2014-07-18&art.codiceRedazionale=14A05668&art.num=1&art.tiposerie=SG
+
+>>> validate('000307052')  # BASE10 format
+'000307052'
+>>> validate('009CVD')  # BASE32 format is converted
+'000307052'
+>>> validate_base10('000307052')
+'000307052'
+>>> validate_base32('009CVD')
+'000307052'
+>>> to_base32('000307052')
+'009CVD'
+>>> from_base32('009CVD')
+'000307052'
+"""
+
+from stdnum.exceptions import *
+from stdnum.util import clean, isdigits
+
+
+# the table of AIC BASE32 allowed chars.
+_base32_alphabet = '0123456789BCDFGHJKLMNPQRSTUVWXYZ'
+
+
+def compact(number):
+    """Convert the number to the minimal representation."""
+    return clean(number, ' ').upper().strip()
+
+
+def from_base32(number):
+    """Convert a BASE32 representation of an AIC to a BASE10 one."""
+    number = compact(number)
+    if not all(x in _base32_alphabet for x in number):
+        raise InvalidFormat()
+    s = sum(_base32_alphabet.index(n) * 32 ** i
+            for i, n in enumerate(reversed(number)))
+    return str(s).zfill(9)
+
+
+def to_base32(number):
+    """Convert a BASE10 representation of an AIC to a BASE32 one."""
+    number = compact(number)
+    if not isdigits(number):
+        raise InvalidFormat()
+    res = ''
+    remainder = int(number)
+    while remainder > 31:
+        res = _base32_alphabet[remainder % 32] + res
+        remainder = remainder // 32
+    res = _base32_alphabet[remainder] + res
+    return res.zfill(6)
+
+
+def calc_check_digit(number):
+    """Calculate the check digit for the BASE10 AIC code."""
+    number = compact(number)
+    weights = (1, 2, 1, 2, 1, 2, 1, 2)
+    return str(sum((x // 10) + (x % 10)
+               for x in (w * int(n) for w, n in zip(weights, number))) % 10)
+
+
+def validate_base10(number):
+    """Check if a string is a valid BASE10 representation of an AIC."""
+    number = compact(number)
+    if len(number) != 9:
+        raise InvalidLength()
+    if not isdigits(number):
+        raise InvalidFormat()
+    if number[0] != '0':
+        raise InvalidComponent()
+    if calc_check_digit(number) != number[-1]:
+        raise InvalidChecksum()
+    return number
+
+
+def validate_base32(number):
+    """Check if a string is a valid BASE32 representation of an AIC."""
+    number = compact(number)
+    if len(number) != 6:
+        raise InvalidLength()
+    return validate_base10(from_base32(number))
+
+
+def validate(number):
+    """Check if a string is a valid AIC. BASE10 is the canonical form and
+    is 9 chars long, while BASE32 is 6 chars."""
+    number = compact(number)
+    if len(number) == 6:
+        return validate_base32(number)
+    else:
+        return validate_base10(number)
+
+
+def is_valid(number):
+    """Check if the given string is a valid AIC code."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_it_aic.doctest b/tests/test_it_aic.doctest
new file mode 100644
index 0000000..b3f13e4
--- /dev/null
+++ b/tests/test_it_aic.doctest
@@ -0,0 +1,128 @@
+test_it_aic.doctest - tests for the stdnum.it.aic module
+
+Copyright (C) 2020 Fabrizio Montanari
+
+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.it.aic module.
+
+>>> from stdnum.it import aic
+>>> from stdnum.exceptions import *
+
+
+Some valid codes with their BASE10/BASE32 representations.
+
+>>> aic10_valid = ['000307052', '000307037', '001738032', '001738020', 
'042645046', '045359015']
+>>> aic32_valid = ['009CVD', '009CUX', '01P19J', '01P194', '18PFKQ', '1C87X7']
+>>> [x for x in aic10_valid + aic32_valid if x and not aic.is_valid(x)]
+[]
+>>> [aic.to_base32(x) for x in aic10_valid] == aic32_valid
+True
+>>> [aic.from_base32(x) for x in aic32_valid] == aic10_valid
+True
+
+
+We offer conversion functions between BASE10 and BASE32 formats.
+
+>>> aic.to_base32('000307037')
+'009CUX'
+>>> aic.from_base32('009CUX')
+'000307037'
+>>> aic.to_base32('009CUX')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> aic.from_base32('009CV$')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+
+
+Check digit calculation corner cases.
+
+>>> aic.calc_check_digit('00030705')
+'2'
+>>> aic.calc_check_digit('010307052')
+'4'
+
+
+Tests for various corner cases.
+
+>>> aic.validate('000307052')
+'000307052'
+>>> aic.validate('00030705.')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> aic.validate('00307052')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> aic.validate('000307053')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> aic.validate(307053)  # not a string type
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> aic.validate('100307053')  # does not start with 0
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> aic.validate('0003070.3')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> aic.validate('009CVD')
+'000307052'
+>>> aic.validate('09CVD')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> aic.validate('2ZP43F')  # BASE10 format does not start with 0
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+>>> aic.validate('009CV$')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+
+
+We can also validate BASE10 or BASE32 format explicitly.
+
+>>> aic.validate_base10('009CVD')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> aic.validate_base32('009CVD1')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> aic.validate_base32('009CVL')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> aic.validate_base32('00$CVD')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> aic.validate_base32('100307052')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...

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

Summary of changes:
 stdnum/it/aic.py          | 132 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_it_aic.doctest | 128 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 260 insertions(+)
 create mode 100644 stdnum/it/aic.py
 create mode 100644 tests/test_it_aic.doctest


hooks/post-receive
-- 
python-stdnum