8000 Add CC (Número de Cartão de Cidadão, Portuguese Identity number) · cedk/python-stdnum@4516748 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4516748

Browse files
davidmgvazarthurdejong
authored andcommitted
Add CC (Número de Cartão de Cidadão, Portuguese Identity number)
Closes arthurdejong#265
1 parent 4c51860 commit 4516748

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

stdnum/pt/cc.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# cc.py - functions for handling Portuguese Identity numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2021 David Vaz
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2.1 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with this library; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19+
# 02110-1301 USA
20+
21+
"""CC (Número de Cartão de Cidadão, Portuguese Identity number).
22+
23+
The Portuguese Identity Number is alphanumeric and consists of the numeric
24+
Número de Identificação Civil, a two-letter version and a check digit.
25+
26+
More information:
27+
28+
* https://pt.wikipedia.org/wiki/Cartão_de_cidadão
29+
* https://www.autenticacao.gov.pt/documents/20126/115760/Validação+de+Número+de+Documento+do+Cartão+de+Cidadão.pdf
30+
31+
>>> validate('00000000 0 ZZ4')
32+
'000000000ZZ4'
33+
>>> validate('00000000 A ZZ4') # invalid format
34+
Traceback (most recent call last):
35+
...
36+
InvalidFormat: ...
37+
>>> validate('00000000 0 ZZ3') # invalid check digits
38+
Traceback (most recent call last):
39+
...
40+
InvalidChecksum: ...
41+
>>> format('000000000ZZ4')
42+
'00000000 0 ZZ4'
43+
"""
44+
45+
import re
46+
47+
from stdnum.exceptions import *
48+
from stdnum.util import clean
49+
50+
51+
_cc_re = re.compile(r'^\d*[A-Z0-9]{2}\d$')
52+
53+
54+
def compact(number):
55+
"""Convert the number to the minimal representation. This strips the
56+
number of any valid separators and removes surrounding whitespace."""
57+
number = clean(number, ' ').upper().strip()
58+
return number
59+
60+
61+
def calc_check_digit(number):
62+
"""Calculate the check digit for the number."""
63+
alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
64+
cutoff = lambda x: x - 9 if x > 9 else x
65+
s = sum(
66+
cutoff(alphabet.index(n) * 2) if i % 2 == 0 else alphabet.index(n)
67+
for i, n in enumerate(number[::-1]))
68+
return str((10 - s) % 10)
69+
70+
71+
def validate(number):
72+
"""Check if the number is a valid cartao de cidadao number."""
73+
number = compact(number)
74+
if not _cc_re.match(number):
75+
raise InvalidFormat()
76+
if calc_check_digit(number[:-1]) != number[-1]:
77+
raise InvalidChecksum()
78+
return number
79+
80+
81+
def is_valid(number):
82+
"""Check if the number is a valid cartao de cidadao number."""
83+
try:
84+
return bool(validate(number))
85+
except ValidationError:
86+
return False
87+
88+
89+
def format(number):
90+
"""Reformat the number to the standard presentation format."""
91+
number = compact(number)
92+
return ' '.join([number[:-4], number[-4], number[-3:]])

tests/test_pt_cc.doctest

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
test_pt_cc.doctest - more detailed doctests for stdnum.pt.cc module
2+
3+
Copyright (C) 2021 Arthur de Jong
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
02110-1301 USA
19+
20+
21+
This file contains more detailed doctests for the stdnum.pt.cc module. It
22+
tries to test more corner cases and detailed functionality that is not really
23+
useful as module documentation.
24+
25+
>>> from stdnum.pt import cc
26+
27+
28+
These have been found online and should all be valid numbers.
29+
30+
>>> numbers = '''
31+
...
32+
... 04521224 4 ZZ 7
33+
... 054546796ZY7
34+
... 101490801ZY0
35+
...
36+
... '''
37+
>>> [x for x in numbers.splitlines() if x and not cc.is_valid(x)]
38+
[]

0 commit comments

Comments
 (0)
0