Skip to main content

A library to validate and extract information from national id numbers

Project description

A library for validating national id numbers and extracting any embedded data from them.

Currenty only South Africa and Zimbabwe are implemented but more countries will be added over time

Installation

pip install id-validation

Usage

from id_validation import ValidatorFactory
validator = ValidatorFactory.get_validator("ZW")

# Use the validate method to test whether a number is valid or not according to country-specific rules
assert validator.validate("50-025544-Q-12")

# The extract data method returns any data that might be encoded into the id number. This is country specific.
data = validator.extract_data("50-025544-Q-12")
assert data["registration_region"] == "Mutasa"
assert data["district"] == "Chivi"
assert data["sequence_number"] == "025544"

Countries

The following codes are available:

BW - Botswana
NG - Nigeria
ZA - South Africa
ZA_OLD - South African (Apartheid-era). See the note below for more information
ZW - Zimbabwe

Botswana (BW)

Note - the validation logic has been implemented from anecdotal information available online and not against official documentation.

>>> import id_validation
>>> from id_validation import ValidatorFactory
>>> validator = ValidatorFactory.get_validator("BW")
>>> validator.validate("379219515")
True
>>> validator.extract_data("379219515")
{'gender': 'Male'}

Nigeria

Nigerian id numbers consist of 11 randomly selected digits. Find the regulations here.

>>> import id_validation
>>> from id_validation import ValidatorFactory
>>> validator = ValidatorFactory.get_validator("NG")
>>> validator.validate("35765421356")
True

South Africa (ZA)

South African ids contain the following information:

  • Date of birth
  • Gender
  • Citizenship (citizen or permanent resident)
>>> import id_validation
>>> from id_validation import ValidatorFactory
>>> validator = ValidatorFactory.get_validator("ZA")
>>> validator.validate("7106245929185")
True
>>> validator.extract_data("7106245929185")
{'dob': datetime.datetime(1971, 6, 24, 0, 0), 'gender': <GENDER.MALE: 1>, 'checksum': 5, 'citizenship': <CITIZENSHIP_TYPE.PERMANENT_RESIDENT: 1>}

South Africa - Apartheid-era (ZA_OLD)

Apartheid-era South African ids contain the following information:

  • Date of birth
  • Gender
  • Race
>>> import id_validation
>>> from id_validation import ValidatorFactory
>>> validator = ValidatorFactory.get_validator("ZA_OLD")
>>> validator.validate("7106245929185")
True
>>> validator.extract_data("7106245929185")
{'dob': datetime.datetime(1971, 6, 24, 0, 0), 'gender': <GENDER.MALE: 1>, 'checksum': 5, 'race': <RACE.CAPE_COLOURED: 1>}

Note

These id numbers were used during the Apartheid-era. They encoded the race of the ID holder. The 1986 Identification Act removed this identifier and all id numbers were changed to the more modern version which only encodes citizenship. This validator is included for completeness. I have never seen an old id number in any dataset I have ever worked with, so avoid using it unless you are sure that your ids are pre-1986. More information can be found here

Zimbabwe (ZW)

Zimbabwe IDs contain the following information:

  • Registration region
  • Father's district
>>> import id_validation
>>> from id_validation import ValidatorFactory
>>> validator = ValidatorFactory.get_validator("ZW")
>>> validator.validate("50-025544-Q-12")
True
>>> validator.extract_data("50-025544-Q-12")
{'registration_region': 'Mutasa', 'district': 'Chivi', 'sequence_number': '025544'}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

id-validation-0.5.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

id_validation-0.5.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file id-validation-0.5.0.tar.gz.

File metadata

  • Download URL: id-validation-0.5.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for id-validation-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e4432dcb07551a14e5909025341a4f57e022ef30798cabf2ede435839d3aba79
MD5 6dfca5465259b358bd41971da5600857
BLAKE2b-256 8caabb5d50b0fa0cd7ed092687c404d2faaf866c269f24420e31ba81db915581

See more details on using hashes here.

File details

Details for the file id_validation-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for id_validation-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a35fa86ceed742a26d65f76d805d773a38eb30fb6b982debd8d23d69a78338c4
MD5 a09c6af8df9451bd2ec36cdab7b61d68
BLAKE2b-256 991f9f3ee1f32928501545891bcf682f5b76921181c8029950407b0a70c9edd7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page