Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/it/aic.py
blob: 777f98d927d326a076113418ddebde386121acc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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'
"""  # noqa: E501

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