Sri Lankan national identity card number validator and parser.
This parser is simple and lightweight. Rules of parser are extracted from this article in Wikipedia. Following table is copied from that article. It is an example for parse national identity card numbers.
Birth by year | Birth day of the year | Serial number | Check digit | Special letter | |
---|---|---|---|---|---|
Old NIC number | 74 | 192 | 275 | 7 | V |
New NIC number | 1964 | 104 | 0275 | 7 | - |
© Wikipedia - license
In the name NicParser. Nic means national identity card. This Idea came because I saw a project named NICParser(not NicParser) written in PHP programming language. Tests are extracted from that.
You need following things to use this tool.
- Python(install from here) Python 2 and 3 versions are supported.
No Installation. Just copy the code in nicparser.py to your code file or download nicparser.py and include on your project directory/folder.
for Python 3(This should work for most situations.)
# paste the code on nicparser.py here, or place nicparser.py in same directory/folder and uncomment(remove #) following line.
#from nicparser import NICParser
nic = '721245677v'
parsed_nic = NICParser(nic)
print('birthday: ' + str(parsed_nic.birth_date))
print('birth year: ' + str(parsed_nic.birth_year))
print('can vote: ' + ('Yes' if parsed_nic.can_vote else 'No'))
print('check digit' + str(parsed_nic.check_digit))
print('gender: ' + ('Male' if parsed_nic.gender else 'Female'))
print('serial number: ' + parsed_nic.serial_number)
print('special letter: ' + parsed_nic.special_letter)
print('is Id old or new?: ' + ('New' if parsed_nic.id_type else 'Old'))
for Python 2
# paste the code on nicparser.py here, or place nicparser.py in same directory/folder and uncomment(remove #) following line.
#from nicparser import NICParser
nic = '721245677v'
parsed_nic = NICParser(nic)
print 'birthday: ' + str(parsed_nic.birth_date)
print 'birth year: ' + str(parsed_nic.birth_year)
print 'can vote: ' + ('Yes' if parsed_nic.can_vote else 'No')
print 'check digit' + str(parsed_nic.check_digit)
print 'gender: ' + ('Male' if parsed_nic.gender else 'Female')
print 'serial number: ' + parsed_nic.serial_number
print 'special letter: ' + parsed_nic.special_letter
print 'is Id old or new?: ' + ('New' if parsed_nic.id_type else 'Old')
NICParser(nic)
pass NIC number as string(str
) or integear(int
). returns parsed data as Struct
object.
eg:-
result = NICParser('721245677v')
result has following attributes.
-
birth_date
- birth day of NIC number owner. asdatetime.datetime
object.print(result.birth_date)
datetime.datetime
objectdatetime.year
Get the birth year of NIC owner. Same to thebirth_year
attribute of result.print(result.birth_date.year)
datetime.month
Get the month of birth of NIC owner. Returns integer (int
).monthnames = [None, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] print(monthnames[result.birth_date.month])
datetime.day
Between 1 and the number of days in the given month of the given year.print(result.birth_date.day)
date.weekday()
Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example,result.birth_date.weekday() == 2
, a Wednesday. See alsoisoweekday()
.daynames = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] print(daynames[result.birth_date.weekday()])
date.isoweekday()
Return the day of the week as an integer, where Monday is 1 and Sunday is 7. For example,date(2002, 12, 4).isoweekday() == 3
, a Wednesday. See alsoweekday()
.daynames = [None, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] print(daynames[result.birth_date.weekday()])
for more info - Python Docs
-
birth_year
- Get the birth year of NIC owner. Same to thebirth_date.year
attribute of result. Returns integer(int
).print(result.birth_year)
-
can_vote
- eligibility of the NIC owner to vote in local area as boolean(bool
).True
means can andFalse
means can't.print(result.can_vote)
-
check_digit
- Single digit.print(result.check_digit)
-
gender
- Gender of NIC owner as boolean(bool
).True
means gender is male andFalse
means gender is female.print(result.gender)
-
serial_number
- Serial number of the issued day as string(str
).print(result.serial_number)
Note: Don't convert serial number to integer(
int
). It makes0735
same to0735
. -
special_letter
- The final letter of old type. NIC numbers. It is generally a 'V' which indicates that the holder is eligible to vote in the area. In some cases the it can be 'X' which usually indicates the holder is not eligible to vote; possibly because they were not permanent residents of Sri Lanka when applying for an NIC. See alsocan_vote
.special_letter
is always capitalized. If no special letter, it isNone
. So it is alwaysNone
for new NIC numbers.print(result.special_letter)
-
id_type
- Type of the NIC number.- New(
True
) - NIC number issued from 1 January 2016. - Old(
False
) - NIC number issued before 1 January 2016.
print(result.id_type)
- New(
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated. We specially accepts new language implementations(NICParser in java, c, ect). Even giving a star, opening issue or sharing are great contributions.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
git commands are placed in brackets for command line git users
Distributed under the MIT License. See LICENSE
for more information.