|
| 1 | +# epic.py - functions for handling Indian voter identification numbers |
| 2 | +# |
| 3 | +# Copyright (C) 2021 Gaurav Chauhan |
| 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 | +"""EPIC (Electoral Photo Identity Card, Indian Voter ID). |
| 21 | +
|
| 22 | +The Electoral Photo Identity Card (EPIC) is an identity document issued by |
| 23 | +the Election Commission of India (ECI) only to the India citizens who have |
| 24 | +reached the age of 18. |
| 25 | +
|
| 26 | +Each EPIC contains an unique 10 digit alphanumeric identifier known as EPIC |
| 27 | +number or Voter ID number. |
| 28 | +
|
| 29 | +Every EPIC number begins with a Functional Unique Serial Number (FUSN), a 3 |
| 30 | +letter unique identifier for each Assembly Constituency. FUSN is followed by |
| 31 | +a 6 digit serial number and 1 check digit of the serial number calculated |
| 32 | +using Luhn algorithm. |
| 33 | +
|
| 34 | +More information: |
| 35 | +
|
| 36 | +* https://en.wikipedia.org/wiki/Voter_ID_(India) |
| 37 | +* https://www.kotaksecurities.com/ksweb/voter-id/serial-number-in-elctoral-roll |
| 38 | +
|
| 39 | +>>> validate('WKH1186253') |
| 40 | +'WKH1186253' |
| 41 | +>>> validate('WKH118624') |
| 42 | +Traceback (most recent call last): |
| 43 | + ... |
| 44 | +InvalidLength: ... |
| 45 | +>>> validate('1231186253') |
| 46 | +Traceback (most recent call last): |
| 47 | + ... |
| 48 | +InvalidFormat: ... |
| 49 | +>>> validate('WKH1186263') |
| 50 | +Traceback (most recent call last): |
| 51 | + ... |
| 52 | +InvalidChecksum: ... |
| 53 | +""" |
| 54 | + |
| 55 | +import re |
| 56 | + |
| 57 | +from stdnum import luhn |
| 58 | +from stdnum.exceptions import * |
| 59 | +from stdnum.util import clean |
| 60 | + |
| 61 | + |
| 62 | +_EPIC_RE = re.compile(r'^[A-Z]{3}[0-9]{7}$') |
| 63 | + |
| 64 | + |
| 65 | +def compact(number): |
| 66 | + """Convert the number to the minimal representation. This strips the |
| 67 | + number of any valid separators and removes surrounding whitespace.""" |
| 68 | + return clean(number, ' -').upper().strip() |
| 69 | + |
| 70 | + |
| 71 | +def validate(number): |
| 72 | + """Check if the number provided is a valid EPIC number. This checks the |
| 73 | + length, formatting and checksum.""" |
| 74 | + number = compact(number) |
| 75 | + if len(number) != 10: |
| 76 | + raise InvalidLength |
| 77 | + if not _EPIC_RE.match(number): |
| 78 | + raise InvalidFormat() |
| 79 | + luhn.validate(number[3:]) |
| 80 | + return number |
| 81 | + |
| 82 | + |
| 83 | +def is_valid(number): |
| 84 | + """Check if the number provided is a valid EPIC number. This checks the |
| 85 | + length, formatting and checksum.""" |
| 86 | + try: |
| 87 | + return bool(validate(number)) |
| 88 | + except ValidationError: |
| 89 | + return False |
0 commit comments