Metadata-Version: 2.4
Name: slnic
Version: 0.1.0
Summary: Parse, validate, convert, and generate Sri Lankan NIC (National Identity Card) numbers
Author-email: Ravindu Pabasara Karunarathna <karurpabe@gmail.com>
Maintainer-email: Ravindu Pabasara Karunarathna <karurpabe@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Ravindu Pabasara Karunarathna
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE. 
Project-URL: Homepage, https://github.com/RavinduPabasara/slnic
Project-URL: Documentation, https://github.com/RavinduPabasara/slnic#readme
Project-URL: Repository, https://github.com/RavinduPabasara/slnic
Project-URL: Bug Tracker, https://github.com/RavinduPabasara/slnic/issues
Keywords: sri lanka,nic,national identity card,validation,parsing,kyc,identity
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=6.0; extra == "dev"
Requires-Dist: pytest-cov>=2.0; extra == "dev"
Requires-Dist: black>=21.0; extra == "dev"
Requires-Dist: flake8>=3.8; extra == "dev"
Requires-Dist: mypy>=0.800; extra == "dev"
Dynamic: license-file

# slnic

Parse, validate, convert, and generate **Sri Lankan NIC (National Identity Card) numbers** in Python.

[![PyPI version](https://img.shields.io/pypi/v/slnic.svg)](https://pypi.org/project/slnic/)
[![Python versions](https://img.shields.io/pypi/pyversions/slnic.svg)](https://pypi.org/project/slnic/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

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

```bash
pip install slnic
```

## Quick start

```python
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

```python
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

```python
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

```python
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()` and
> `is_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 `19` to the year and left-pad the serial with `0`.

## 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

```bash
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](LICENSE).

## Author

**Ravindu Pabasara Karunarathna** — also the author of [sinhaladate](https://pypi.org/project/sinhaladate/).
