Parse, validate, convert, and generate Sri Lankan NIC (National Identity Card) numbers
Project description
slnic
Parse, validate, convert, and generate Sri Lankan NIC (National Identity Card) numbers in Python.
A Sri Lankan NIC number encodes the holder's birth date and gender. slnic understands both the old 10-character format (853400123V) and the new 12-digit format (198534000123), and gives you a clean, typed API to work with them — zero dependencies.
Installation
pip install slnic
Quick start
import slnic
nic = slnic.parse("853400123V")
nic.birth_date # datetime.date(1985, 12, 5)
nic.gender # <Gender.MALE: 'male'>
nic.age() # 40 (as of today)
nic.format # <NICFormat.OLD: 'old'>
nic.voter_eligible # True ('V' suffix; 'X' → False; None for new format)
nic.serial # '012'
nic.check_digit # '3'
str(nic.to_new()) # '198534000123'
Validate
slnic.is_valid("853400123V") # True
slnic.is_valid("858400123v") # True (case-insensitive, female)
slnic.is_valid("859990123V") # False (invalid day-of-year segment)
# parse() raises InvalidNICError with a helpful message:
slnic.parse("870600012V")
# InvalidNICError: day of year 60 means 29 February, but 1987 is not a leap year
Convert between formats
old = slnic.parse("853400123V")
new = old.to_new() # NIC('198534000123')
# New → old works when the birth year is 1900-1999 and the serial fits.
# The V/X letter was dropped in the new format, so supply it (default 'V'):
back = new.to_old(voter_eligible=True) # NIC('853400123V')
Generate test data
from datetime import date
from slnic import generate
nic = generate(date(1992, 7, 14), gender="female", nic_format="new")
str(nic) # e.g. '199269604823'
Note: the NIC checksum algorithm has never been published, so generated numbers are structurally valid (great for test fixtures and demos) but may not correspond to any real, issued NIC. For the same reason,
parse()andis_valid()do not verify the check digit — no library can.
How NIC numbers work
| Segment | Old format (853400123V) |
New format (198534000123) |
|---|---|---|
| Birth year | 85 → 1985 (always 19xx) |
1985 |
| Day of year | 340 |
340 |
| Serial | 012 (3 digits) |
0012 (4 digits) |
| Check digit | 3 |
3 |
| Voter letter | V eligible / X not |
(dropped) |
- Gender: if the day-of-year segment is greater than 500, the holder is female — subtract 500 to get the actual day (e.g.
840→ day 340). - February always counts as 29 days in the NIC calendar, regardless of leap year. So 1 March is always day 61, and day 60 (29 Feb) is only valid for leap-year births.
- Old → new conversion: prepend
19to the year and left-pad the serial with0.
API
| Function / attribute | Description |
|---|---|
slnic.parse(s) -> NIC |
Parse either format; raises InvalidNICError |
slnic.is_valid(s) -> bool |
Structural validity check |
slnic.generate(birth_date, ...) -> NIC |
Build a structurally valid NIC (test data) |
NIC.birth_date, .gender, .serial, .check_digit, .voter_eligible, .format, .number |
Parsed fields |
NIC.birth_year, .day_of_year, .age(on=None) |
Derived values |
NIC.to_new(), NIC.to_old(voter_eligible=True) |
Format conversion (raises ConversionError when impossible) |
NIC is a frozen (immutable, hashable) dataclass, and the package ships type hints (py.typed).
Development
git clone https://github.com/RavinduPabasara/slnic
cd slnic
make install-dev
make test
Privacy note
NIC numbers are personal data. This library only decodes what the number format itself encodes; take care when logging or storing NICs in your own applications.
License
MIT — see LICENSE.
Author
Ravindu Pabasara Karunarathna — also the author of sinhaladate.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file slnic-0.1.0.tar.gz.
File metadata
- Download URL: slnic-0.1.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efdfe25183c9828bb4ed8c76d5fdc4aceda5dac7f503e6cb24766e079d895009
|
|
| MD5 |
0c516f772fb8da4c4850f2b42e837b66
|
|
| BLAKE2b-256 |
a625dec4d24f4af43c12ca32d53c8d0b8c1e71435d501edbb37e57af1c05b227
|
File details
Details for the file slnic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: slnic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8c2aa50a0994ce5b1dfe3ce577654a98dbb312115ec15a9fffbf56e07de55e8
|
|
| MD5 |
c72052fe34b7bdb649a40e9ea40c97d1
|
|
| BLAKE2b-256 |
687c8ca4abbec1e21ae98f9e54a5a44d757fdf7d6210b9c89ab7feee09720da5
|