Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/in_/epic.py
blob: 14ec89def42cc2c6fefce3676a9ef462752b7e05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# epic.py - functions for handling Indian voter identification numbers
#
# Copyright (C) 2021 Gaurav Chauhan
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""EPIC (Electoral Photo Identity Card, Indian Voter ID).

The Electoral Photo Identity Card (EPIC) is an identity document issued by
the Election Commission of India (ECI) only to the India citizens who have
reached the age of 18.

Each EPIC contains an unique 10 digit alphanumeric identifier known as EPIC
number or Voter ID number.

Every EPIC number begins with a Functional Unique Serial Number (FUSN), a 3
letter unique identifier for each Assembly Constituency. FUSN is followed by
a 6 digit serial number and 1 check digit of the serial number calculated
using Luhn algorithm.

More information:

* https://en.wikipedia.org/wiki/Voter_ID_(India)
* https://www.kotaksecurities.com/ksweb/voter-id/serial-number-in-elctoral-roll

>>> validate('WKH1186253')
'WKH1186253'
>>> validate('WKH118624')
Traceback (most recent call last):
    ...
InvalidLength: ...
>>> validate('1231186253')
Traceback (most recent call last):
    ...
InvalidFormat: ...
>>> validate('WKH1186263')
Traceback (most recent call last):
    ...
InvalidChecksum: ...
"""

import re

from stdnum import luhn
from stdnum.exceptions import *
from stdnum.util import clean


_EPIC_RE = re.compile(r'^[A-Z]{3}[0-9]{7}$')


def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    return clean(number, ' -').upper().strip()


def validate(number):
    """Check if the number provided is a valid EPIC number. This checks the
    length, formatting and checksum."""
    number = compact(number)
    if len(number) != 10:
        raise InvalidLength
    if not _EPIC_RE.match(number):
        raise InvalidFormat()
    luhn.validate(number[3:])
    return number


def is_valid(number):
    """Check if the number provided is a valid EPIC number. This checks the
    length, formatting and checksum."""
    try:
        return bool(validate(number))
    except ValidationError:
        return False