|
| 1 | +# aic.py - functions for handling Italian AIC codes |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# This file is based on pyAIC Python library. |
| 5 | +# https://github.com/FabrizioMontanari/pyAIC |
| 6 | +# |
| 7 | +# Copyright (C) 2020 Fabrizio Montanari |
| 8 | +# |
| 9 | +# This library is free software; you can redistribute it and/or |
| 10 | +# modify it under the terms of the GNU Lesser General Public |
| 11 | +# License as published by the Free Software Foundation; either |
| 12 | +# version 2.1 of the License, or (at your option) any later version. |
| 13 | +# |
| 14 | +# This library is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | +# Lesser General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU Lesser General Public |
| 20 | +# License along with this library; if not, write to the Free Software |
| 21 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 22 | +# 02110-1301 USA |
| 23 | + |
| 24 | +"""AIC (Italian code for identification of drugs). |
| 25 | +
|
| 26 | +AIC codes are used to identify drugs allowed to be sold in Italy. Codes are |
| 27 | +issued by the Italian Drugs Agency (AIFA, Agenzia Italiana del Farmaco), the |
| 28 | +government authority responsible for drugs regulation in Italy. |
| 29 | +
|
| 30 | +The number consists of 9 digits and includes a check digit. |
| 31 | +
|
| 32 | +More information: |
| 33 | +
|
| 34 | +* 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 |
| 35 | +
|
| 36 | +>>> validate('000307052') # BASE10 format |
| 37 | +'000307052' |
| 38 | +>>> validate('009CVD') # BASE32 format is converted |
| 39 | +'000307052' |
| 40 | +>>> validate_base10('000307052') |
| 41 | +'000307052' |
| 42 | +>>> validate_base32('009CVD') |
| 43 | +'000307052' |
| 44 | +>>> to_base32('000307052') |
| 45 | +'009CVD' |
| 46 | +>>> from_base32('009CVD') |
| 47 | +'000307052' |
| 48 | +""" |
| 49 | + |
| 50 | +from stdnum.exceptions import * |
| 51 | +from stdnum.util import clean, isdigits |
| 52 | + |
| 53 | + |
| 54 | +# the table of AIC BASE32 allowed chars. |
| 55 | +_base32_alphabet = '0123456789BCDFGHJKLMNPQRSTUVWXYZ' |
| 56 | + |
| 57 | + |
| 58 | +def compact(number): |
| 59 | + """Convert the number to the minimal representation.""" |
| 60 | + return clean(number, ' ').upper().strip() |
| 61 | + |
| 62 | + |
| 63 | +def from_base32(number): |
| 64 | + """Convert a BASE32 representation of an AIC to a BASE10 one.""" |
| 65 | + number = compact(number) |
| 66 | + if not all(x in _base32_alphabet for x in number): |
| 67 | + raise InvalidFormat() |
| 68 | + s = sum(_base32_alphabet.index(n) * 32 ** i |
| 69 | + for i, n in enumerate(reversed(number))) |
| 70 | + return str(s).zfill(9) |
| 71 | + |
| 72 | + |
| 73 | +def to_base32(number): |
| 74 | + """Convert a BASE10 representation of an AIC to a BASE32 one.""" |
| 75 | + number = compact(number) |
| 76 | + if not isdigits(number): |
| 77 | + raise InvalidFormat() |
| 78 | + res = '' |
| 79 | + remainder = int(number) |
| 80 | + while remainder > 31: |
| 81 | + res = _base32_alphabet[remainder % 32] + res |
| 82 | + remainder = remainder // 32 |
| 83 | + res = _base32_alphabet[remainder] + res |
| 84 | + return res.zfill(6) |
| 85 | + |
| 86 | + |
| 87 | +def calc_check_digit(number): |
| 88 | + """Calculate the check digit for the BASE10 AIC code.""" |
| 89 | + number = compact(number) |
| 90 | + weights = (1, 2, 1, 2, 1, 2, 1, 2) |
| 91 | + return str(sum((x // 10) + (x % 10) |
| 92 | + for x in (w * int(n) for w, n in zip(weights, number))) % 10) |
| 93 | + |
| 94 | + |
| 95 | +def validate_base10(number): |
| 96 | + """Check if a string is a valid BASE10 representation of an AIC.""" |
| 97 | + number = compact(number) |
| 98 | + if len(number) != 9: |
| 99 | + raise InvalidLength() |
| 100 | + if not isdigits(number): |
| 101 | + raise InvalidFormat() |
| 102 | + if number[0] != '0': |
| 103 | + raise InvalidComponent() |
| 104 | + if calc_check_digit(number) != number[-1]: |
| 105 | + raise InvalidChecksum() |
| 106 | + return number |
| 107 | + |
| 108 | + |
| 109 | +def validate_base32(number): |
| 110 | + """Check if a string is a valid BASE32 representation of an AIC.""" |
| 111 | + number = compact(number) |
| 112 | + if len(number) != 6: |
| 113 | + raise InvalidLength() |
| 114 | + return validate_base10(from_base32(number)) |
| 115 | + |
| 116 | + |
| 117 | +def validate(number): |
| 118 | + """Check if a string is a valid AIC. BASE10 is the canonical form and |
| 119 | + is 9 chars long, while BASE32 is 6 chars.""" |
| 120 | + number = compact(number) |
| 121 | + if len(number) == 6: |
| 122 | + return validate_base32(number) |
| 123 | + else: |
| 124 | + return validate_base10(number) |
| 125 | + |
| 126 | + |
| 127 | +def is_valid(number): |
| 128 | + """Check if the given string is a valid AIC code.""" |
| 129 | + try: |
| 130 | + return bool(validate(number)) |
| 131 | + except ValidationError: |
| 132 | + return False |
0 commit comments