https://github.com/ksengine/nicparser
Sri Lankan national identity card number validator and parser.
https://github.com/ksengine/nicparser
Last synced: over 1 year ago
JSON representation
Sri Lankan national identity card number validator and parser.
- Host: GitHub
- URL: https://github.com/ksengine/nicparser
- Owner: Ksengine
- License: mit
- Created: 2020-12-29T17:39:58.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-31T15:20:41.000Z (over 5 years ago)
- Last Synced: 2025-01-15T07:17:11.102Z (over 1 year ago)
- Language: Python
- Size: 28.3 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NicParser

[](https://github.com/Ksengine/NicParser/issues)
[](https://github.com/Ksengine/NicParser/network)
[](https://github.com/Ksengine/NicParser/stargazers)
[](https://github.com/Ksengine/NicParser/LICENSE)

[](https://twitter.com/intent/tweet?text=Sri%20Lankan%20national%20identity%20card%20number%20validator%20and%20parser.:&url=https%3A%2F%2Fgithub.com%2FKsengine%2FNicParser)
Sri Lankan national identity card number validator and parser.
## About
This parser is simple and lightweight. Rules of parser are extracted from this [article](https://en.wikipedia.org/wiki/National_identity_card_%28Sri_Lanka%29) 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](https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_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.
## Getting Started
### Prerequisites
You need following things to use this tool.
- Python(install from [here](https://python.org))
_Python 2 and 3 versions are supported._
## Installation
No Installation. Just copy the code in [nicparser.py](nicparser.py) to your code file or download [nicparser.py](nicparser.py) and include on your project directory/folder.
## Example
for Python 3(This should work for most situations.)
```python
# 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
```python
# 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')
```
## Usage
**`NICParser(nic)`**
pass NIC number as string(`str`) or integear(`int`). returns parsed data as `Struct` object.
eg:-
```python
result = NICParser('721245677v')
```
result has following attributes.
- `birth_date` - birth day of NIC number owner. as `datetime.datetime` object.
```python
print(result.birth_date)
```
`datetime.datetime` object
- `datetime.year`
Get the birth year of NIC owner. Same to the `birth_year` attribute of result.
```python
print(result.birth_date.year)
```
- `datetime.month`
Get the month of birth of NIC owner. Returns integer (`int`).
```python
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.
```python
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 also `isoweekday()`.
```python
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 also `weekday()`.
```python
daynames = [None, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
print(daynames[result.birth_date.weekday()])
```
for more info - [Python Docs]([https://docs.python.org/3/library/datetime.html#datetime-objects](https://docs.python.org/3/library/datetime.html#datetime-objects))
- `birth_year` - Get the birth year of NIC owner. Same to the `birth_date.year` attribute of result. Returns integer(`int`).
```python
print(result.birth_year)
```
- `can_vote` - eligibility of the NIC owner to vote in local area as boolean(`bool`). `True` means can and `False` means can't.
```python
print(result.can_vote)
```
- `check_digit` - Single digit.
```python
print(result.check_digit)
```
- `gender` - Gender of NIC owner as boolean(`bool`). `True` means gender is male and `False` means gender is female.
```python
print(result.gender)
```
- `serial_number` - Serial number of the issued day as string(`str`).
```python
print(result.serial_number)
```
> Note: Don't convert serial number to integer(`int`). It makes `0735` same to `0735`.
- `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 also `can_vote`. `special_letter` is always capitalized. If no special letter, it is `None`. So it is always `None` for new NIC numbers.
```python
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.
```python
print(result.id_type)
```
## Roadmap
See the [open issues](issues) for a list of proposed features (and known issues).
## Contributing
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.
1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
_git commands are placed in brackets for command line git users_
## License
Distributed under the MIT License. See [`LICENSE`](LICENSE) for more information.