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'
Command line
slnic info 853400123V # birth date, gender, age, serial, ...
slnic info --json 853400123V # the same as JSON
slnic validate nics.txt # one NIC per line (or stdin); lists invalid ones, exits 1 if any
slnic convert 853400123V # 198534000123 (old <-> new)
slnic generate 1992-07-14 --gender female --format old -n 5
Spaces and hyphens between digit groups are accepted everywhere:
parse("853 400 123V") and parse("1985-340-00123") both work.
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 (spaces/hyphens allowed); 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.
Metadata
Release files for slnic 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| slnic-0.2.0.tar.gz | 15.3 kB | Without provenance | Details for slnic-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| slnic-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for slnic-0.2.0-py3-none-any.whl |
Total release size: 28.0 kB
Release files / slnic-0.2.0.tar.gz
| Download URL | slnic-0.2.0.tar.gz |
|---|---|
| Size | 15.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
21e9ca2fc144f37db9c67cd452f2dbfe310b50eb75e5af6865ccf7f22d7ba1ce
|
|
BLAKE2b-256 checksum How to use checksums |
429b56a84f77766bccfed1754a74e708778a17bf0da3580fa9a8bc1ee3baa8f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Release files / slnic-0.2.0-py3-none-any.whl
| Download URL | slnic-0.2.0-py3-none-any.whl |
|---|---|
| Size | 12.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
447a0d3d82baba5899ec0a245d72077072ca16100980dc308ccac14bce4f0d27
|
|
BLAKE2b-256 checksum How to use checksums |
d0184edf6e48c754ed10e8d62a69f9f5df8fb775ded3261fe0d766550633c0d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Security for slnic 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.