helakit.nic
helakit.nic
NIC (National Identity Card) validation, decoding, and conversion.
NICBatchResult
dataclass
NICBatchResult(results: list[ValidationResult], duplicates: dict[str, list[int]] = dict(), summary: NICSummary = (lambda: NICSummary(0, 0, 0, 0, 0, 0, 0))(), df: Any | None = None)
The outcome of validating a list / dataframe of NICs.
Attributes:
| Name | Type | Description |
|---|---|---|
results |
list[ValidationResult]
|
One :class: |
duplicates |
dict[str, list[int]]
|
Mapping from canonical NIC (uppercased, V/X stripped) to the row indices it appeared at. Only entries with two or more indices are included. |
summary |
NICSummary
|
Roll-up counts. |
df |
Any | None
|
When the input was a pandas or polars DataFrame, this is a
copy of that frame with helakit's per-row columns appended.
|
NICDecoded
dataclass
NICDecoded(format: Literal['old', 'new'], dob: date, gender: Literal['male', 'female'], age: int, year: int, day_code: int, serial: int, check_digit: int, voting_eligible: bool | None)
Structured fields extracted from a valid NIC.
Attributes:
| Name | Type | Description |
|---|---|---|
format |
Literal['old', 'new']
|
Either |
dob |
date
|
Decoded date of birth. |
gender |
Literal['male', 'female']
|
Either |
age |
int
|
Age in completed years at the time of validation. |
year |
int
|
Full birth year. |
day_code |
int
|
Day-of-year encoding with the female 500 offset removed. |
serial |
int
|
Serial number assigned on the registration day. |
check_digit |
int
|
Final check digit (not currently verified — see
:mod: |
voting_eligible |
bool | None
|
|
NICError
Bases: HelakitError
Base class for NIC-related errors raised by helakit.
NICFormatError
Bases: NICError
Raised when an input cannot be parsed as either NIC format.
Used by :func:helakit.nic.convert_nic because conversion has no
sensible ValidationResult to return — failure must propagate as
an exception.
NICSummary
dataclass
NICSummary(total: int, valid: int, invalid: int, duplicate_groups: int, duplicate_rows: int, dob_mismatches: int, gender_mismatches: int)
Aggregate counts for a batch validation run.
Attributes:
| Name | Type | Description |
|---|---|---|
total |
int
|
Number of input rows processed. |
valid |
int
|
Rows whose NIC passed structural validation. |
invalid |
int
|
Rows that failed structural validation. |
duplicate_groups |
int
|
Number of distinct NICs that appear in more than one row (after canonicalisation that strips V/X suffixes). |
duplicate_rows |
int
|
Total rows participating in any duplicate group. |
dob_mismatches |
int
|
Rows where the supplied DOB differed from the NIC-decoded DOB (only counted when both were available). |
gender_mismatches |
int
|
Same idea for gender. |
convert_nic
convert_nic(value: str, *, century: int = ...) -> str
convert_nic(value: list[str] | tuple[str, ...], *, century: int = ..., errors: ErrorMode = ...) -> list[str | None]
convert_nic(value: Any, *, century: int = ..., nic_col: str | None = ..., errors: ErrorMode = ..., error_col: str | None = ...) -> Any
convert_nic(value: Any, *, century: int = DEFAULT_OLD_NIC_CENTURY, nic_col: str | None = None, errors: ErrorMode = 'raise', error_col: str | None = None) -> Any
Convert an old-format NIC to the new 12-digit format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Either a single NIC string, a list of strings, a pandas
or polars Series, or a pandas/polars DataFrame. For Series
input the function returns a Series of the same length,
preserving index/name. For DataFrame input |
required |
century
|
int
|
Century to assume for two-digit years on old NICs. |
DEFAULT_OLD_NIC_CENTURY
|
nic_col
|
str | None
|
Required for tabular input. |
None
|
errors
|
ErrorMode
|
How to handle individual values that cannot be converted.
|
'raise'
|
error_col
|
str | None
|
Column name for per-row error messages (DataFrame
input only). When supplied, the returned frame gets an extra
column with the failure message or |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The same shape as the input — string for string, list for list, |
Any
|
DataFrame for DataFrame. |
Raises:
| Type | Description |
|---|---|
NICFormatError
|
If a value cannot be parsed (e.g. wrong length,
non-numeric content) and |
InvalidInputError
|
For unsupported input types or invalid
|
is_valid_nic
is_valid_nic(value: str, *, format: FormatHint = 'any') -> bool
Return True if value is a structurally valid NIC.
Scalar-only — for batch checks use :func:validate_nic and inspect
each ValidationResult.
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
If |
validate_nic
validate_nic(data: str, *, format: FormatHint = ..., century: int = ...) -> ValidationResult
validate_nic(data: list[str] | list[dict[str, Any]] | tuple[str, ...], *, format: FormatHint = ..., nic_col: str | None = ..., dob_col: str | None = ..., gender_col: str | None = ..., century: int = ..., errors: BatchErrorMode = ...) -> NICBatchResult
validate_nic(data: Any, *, format: FormatHint = ..., nic_col: str | None = ..., dob_col: str | None = ..., gender_col: str | None = ..., century: int = ..., errors: BatchErrorMode = ...) -> NICBatchResult
validate_nic(data: Any, *, format: FormatHint = 'any', nic_col: str | None = None, dob_col: str | None = None, gender_col: str | None = None, century: int = DEFAULT_OLD_NIC_CENTURY, errors: BatchErrorMode = 'raise') -> ValidationResult | NICBatchResult
Validate one or many Sri Lankan NIC numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
A single NIC string, a |
required |
format
|
FormatHint
|
Restrict to |
'any'
|
nic_col
|
str | None
|
Column name holding NICs (required for tabular input). |
None
|
dob_col
|
str | None
|
Column name holding dates of birth. When supplied each row is cross-checked and the per-row result records whether the decoded DOB matched. |
None
|
gender_col
|
str | None
|
Column name holding gender ( |
None
|
century
|
int
|
Century to assume for two-digit years on old NICs.
Defaults to |
DEFAULT_OLD_NIC_CENTURY
|
errors
|
BatchErrorMode
|
Batch-only. |
'raise'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
ValidationResult | NICBatchResult
|
class: |
ValidationResult | NICBatchResult
|
class: |
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
For unsupported input types, an invalid
|