8000 Add Pakistani ID card number · unho/python-stdnum@fa62ea3 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa62ea3

Browse files
committed
Add Pakistani ID card number
Based on the implementation provided by Quantum Novice (Syed Haseeb Shah). Closes arthurdejong#306 Closes arthurdejong#304
1 parent feccaff commit fa62ea3

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

stdnum/pk/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# __init__.py - collection of Pakistani numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2022 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+
"""Collection of Pakistani numbers."""

stdnum/pk/cnic.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# cnic.py - functions for handling Pakistani CNIC numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2022 Syed Haseeb Shah
5+
# Copyright (C) 2022 Arthur de Jong
6+
#
7+
# This library is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU Lesser General Public
9+
# License as published by the Free Software Foundation; either
10+
# version 2.1 of the License, or (at your option) any later version.
11+
#
8000 12+
# This library is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
# Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public
18+
# License along with this library; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20+
# 02110-1301 USA
21+
22+
"""CNIC number (Pakistani Computerised National Identity Card number).
23+
24+
The CNIC (Computerised National Identity Card, قومی شناختی کارڈ) or SNIC
25+
(Smart National Identity Card) is issued by by Pakistan's NADRA (National
26+
Database and Registration Authority) to citizens of 18 years or older.
27+
28+
The number consists of 13 digits and encodes the person's locality (5
29+
digits), followed by 7 digit serial number an a single digit indicating
30+
gender.
31+
32+
More Information:
33+
34+
* https://en.wikipedia.org/wiki/CNIC_(Pakistan)
35+
* https://www.nadra.gov.pk/identity/identity-cnic/
36+
37+
>>> validate('34201-0891231-8')
38+
'3420108912318'
39+
>>> validate('42201-0397640-8')
40+
'4220103976408'
41+
>>> get_gender('42201-0397640-8')
42+
'F'
43+
>>> get_province('42201-0397640-8')
44+
'Sindh'
45+
>>> format('3420108912318')
46+
'34201-0891231-8'
47+
"""
48+
49+
from stdnum.exceptions import *
50+
from stdnum.util import clean, isdigits
51+
52+
53+
def compact(number):
54+
"""Convert the number to the minimal representation. This strips the
55+
number of any valid separators and removes surrounding whitespace."""
56+
return clean(number, '-').strip()
57+
58+
59+
def get_gender(number):
60+
"""Get the person's birth gender ('M' or 'F')."""
61+
number = compact(number)
62+
if number[-1] in '13579':
63+
return 'M'
64+
elif number[-1] in '2468':
65+
return 'F'
66+
67+
68+
# Valid Province IDs
69+
PROVINCES = {
70+
'1': 'Khyber Pakhtunkhwa',
71+
'2': 'FATA',
72+
'3': 'Punjab',
73+
'4': 'Sindh',
74+
'5': 'Balochistan',
75+
'6': 'Islamabad',
76+
'7': 'Gilgit-Baltistan',
77+
}
78+
79+
80+
def get_province(number):
81+
"""Get the person's birth gender ('M' or 'F')."""
82+
number = compact(number)
83+
return PROVINCES.get(number[0])
84+
85+
86+
def validate(number):
87+
"""Check if the number is a valid CNIC. This checks the length, formatting
88+
and some digits."""
89+
number = compact(number)
90+
if not isdigits(number):
91+
raise InvalidFormat()
92+
if len(number) != 13:
93+
raise InvalidLength()
94+
if not get_gender(number):
95+
raise InvalidComponent()
96+
if not get_province(number):
97+
raise InvalidComponent()
98+
return number
99+
100+
101+
def is_valid(number):
102+
"""Check if the number is a valid CNIC."""
103+
try:
104+
return bool(validate(number))
105+
except ValidationError:
106+
return False
107+
108+
109+
def format(number):
110+
"""Reformat the number to the standard presentation format."""
111+
number = compact(number)
112+
return '-'.join((number[:5], number[5:12], number[12:]))

tests/test_pk_cnic.doctest

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
test_pk_cnic.doctest - more detailed doctests for stdnum.pk.cnic
2+
3+
Copyright (C) 2022 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.pk.cnic module.
22+
23+
>>> from stdnum.pk import cnic
24+
25+
26+
>>> cnic.validate('34201-0891231-8')
27+
'3420108912318'
28+
>>> cnic.validate('34201-0891231-0') # invalid gender
29+
Traceback (most recent call last):
30+
...
31+
InvalidComponent: ...
32+
>>> cnic.validate('94201-0891231-8') # invalid province
33+
Traceback (most recent call last):
34+
...
35+
InvalidComponent: ...
36+
37+
>>> cnic.get_gender('34201-0891231-8')
38+
'F'
39+
>>> cnic.get_gender('34201-0891231-9')
40+
'M'

0 commit comments

Comments
 (0)
0