|
| 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:])) |
0 commit comments