8000 Add Argentinian DNI · yyht/python-stdnum@d7f7b8e · GitHub
[go: up one dir, main page]

Skip to content

Commit d7f7b8e

Browse files
committed
Add Argentinian DNI
Closes arthurdejong#90
1 parent 375f63b commit d7f7b8e

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

stdnum/ar/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020

2121
"""Collection of Argentinian numbers."""
2222

23-
# provide vat as an alias
23+
# provide aliases
2424
from stdnum.ar import cuit as vat # noqa: F401
25+
from stdnum.ar import dni as personalid # noqa: F401

stdnum/ar/dni.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# dni.py - functions for handling Argentinian national identifiers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2018 Arthur de Jong
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+
"""DNI (Documento Nacional de Identidad, Argentinian national identity nr.).
22+
23+
The DNI number is the number that appears on the Argentinian national
24+
identity document and is used to identify citizen and foreigners residing in
25+
the country.
26+
27+
More information:
28+
29+
* https://en.wikipedia.org/wiki/Documento_Nacional_de_Identidad_(Argentina)
30+
31+
>>> validate('20.123.456')
32+
'20123456'
33+
>>> validate('2012345699')
34+
Traceback (most recent call last):
35+
...
36+
InvalidLength: ...
37+
>>> format('20123456')
38+
'20.123.456'
39+
"""
40+
41+
from stdnum.exceptions import *
42+
from stdnum.util import clean
43+
44+
45+
def compact(number):
46+
"""Convert the number to the minimal representation. This strips the
47+
number of any valid separators and removes surrounding whitespace."""
48+
return clean(number, ' .').strip()
49+
50+
51+
def validate(number):
52+
"""Check if the number is a valid DNI."""
53+
number = compact(number)
54+
if not number.isdigit():
55+
raise InvalidFormat()
56+
if len(number) not in (7, 8):
57+
raise InvalidLength()
58+
return number
59+
60+
61+
def is_valid(number):
62+
"""Check if the number is a valid DNI."""
63+
try:
64+
return bool(validate(number))
65+
except ValidationError:
66+
return False
67+
68+
69+
def format(number):
70+
"""Reformat the number to the standard presentation format."""
71+
number = compact(number)
72+
return '.'.join((number[:-6], number[-6:-3], number[-3:]))

0 commit comments

Comments
 (0)
0