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.1.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.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: melli_code-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 32ece187d11689532ff6349fd35e6e7ee8e8daa7dee109de17da4e050053b137
MD5 5e1165e68682e70dbef40a67fae29d0e
BLAKE2b-256 16b64257085d2aea557563465021f48c7d71f409fe5147ff1c9f32757c859f37

See more details on using hashes here.

File details

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

File metadata

  • Download URL: melli_code-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3fc1106e371181a7d7a0df3ea7b066e9c6ffd01464585be7e4acac79f4ac2b69
MD5 9ebc86d44fafa6e7ca3f81203fdef09b
BLAKE2b-256 bea81536842fc816561634e55b0f0e21c9754bcb7df286e636f56747d6742e37

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