Skip to main content

Python library to validate and generate Iranian National Codes (Melli Code).

Project description

Melli Code - Iranian National Code (Python)

PyPI version License: MIT Python versions

Python library to validate and generate Iranian National Codes (کد ملی ایران).

This is a Python port and enhancement of the original JavaScript library majidh1/iranianNationalCode.

Features

  • Validation: Check if a given 10-digit string is a valid Iranian National Code (is_valid, validate).
  • Generation: Generate random, valid Iranian National Codes (generate).
  • Pure Python, no runtime dependencies.
  • Type-hinted and tested.

Installation

pip install melli-code

Usage

Validation

Use is_valid for a boolean check, or validate to raise InvalidNationalCode on failure.

from melli_code import is_valid, validate, InvalidNationalCode

# Using is_valid (returns True/False)
code1 = "0012345679"  # Valid
code2 = "0012345678"  # Invalid checksum
code3 = "1111111111"  # Invalid (all same digits)
code4 = "12345"       # Invalid format

print(f"'{code1}' is valid: {is_valid(code1)}")
print(f"'{code2}' is valid: {is_valid(code2)}")
print(f"'{code3}' is valid: {is_valid(code3)}")
print(f"'{code4}' is valid: {is_valid(code4)}")

# Using validate (raises exception on failure)
try:
    validate(code1)
    print(f"'{code1}' validation passed!")
except InvalidNationalCode as e:
    print(f"Validation failed for '{code1}': {e}")

try:
    validate(code2)
    print(f"'{code2}' validation passed!")
except InvalidNationalCode as e:
    print(f"Validation failed for '{code2}': {e}") # Expected output

try:
    validate(code4)
    print(f"'{code4}' validation passed!")
except InvalidNationalCode as e:
    print(f"Validation failed for '{code4}': {e}") # Expected output

Generation

Generate a new, random, valid code.

from melli_code import generate

new_code = generate()
print(f"Generated valid code: {new_code}")
# Example Output: Generated valid code: 4848948377 (will vary)

Algorithm Reference

Structure of Iranian National Code
Algorithm for Iranian National Code

(Images from the original JavaScript repository)

Development

  1. Clone the repository:
git clone https://github.com/amirhosein-vedadi/Melli_Code.git
cd melli-code-py
  1. Create virtual env:
python -m venv venv
  1. Activate:

    • Linux/macOS: source venv/bin/activate
    • Windows: venv\Scripts\activate
  2. Install editable with dev deps:

pip install -e .[dev]
  1. Run tests:
pytest tests/

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

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

melli_code-1.0.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

melli_code-1.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file melli_code-1.0.0.tar.gz.

File metadata

  • Download URL: melli_code-1.0.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.10

File hashes

Hashes for melli_code-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1c52042c17569fbb38740fa97cad019d8e1ca80a8681062b7b3c92c542a6b03f
MD5 6a39f7937e887d96fc503e882de3e205
BLAKE2b-256 2088800c6fe18513123df6119c392ebad4cd70f934881066f857848e7ee033f4

See more details on using hashes here.

File details

Details for the file melli_code-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: melli_code-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.10

File hashes

Hashes for melli_code-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72325be1cd8643b3639bc0ffa4972515350662e77db70deffecd8ce1e41cb564
MD5 b0cf6dee7c21ef5c3fef21510f665b60
BLAKE2b-256 626842d830225aa093a75a416b6726892089ee2e309fc3aeb73a7b022423c471

See more details on using hashes here.

Supported by

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