Skip to main content

Replace long, unreadable ids with short human-readable code ids

Project description

Human Short Code

Encode integers into compact, human-readable codes and decode them reliably.
Useful for generating invitation codes, referral links, tracking IDs, or session tokens.

Features

  • Encode numbers into fixed-length readable codes
  • Decode codes back to original numbers
  • Validate code format
  • Customizable alphabet and length
  • Mid-string hyphen for clarity

Installation

pip install human-short-code

Or install from source:

git clone https://github.com/yourusername/human-short-code.git
cd human-short-code
python -m build
pip install dist/*.whl

Usage

from human_short_code import encode, decode, verify

number = 123456789

# Encode number
code = encode(number)
print(code)  # e.g. 'QIUN-ESAD'

# Decode code
original = decode(code)
print(original)  # 123456789

# Verify code format
print(verify(code))  # True

API

encode(id_number: int, alphabet: str = ..., length: int = ...) -> str

Convert an integer into a short, fixed-length alphanumeric code.

  • id_number: Integer to encode
  • alphabet: Optional custom alphabet (default: "ASDEIUNWRQXBOKH")
  • length: Total code length excluding hyphen (default: 8)

Returns a code string with a hyphen inserted at the center.

decode(code: str, alphabet: str = ..., length: int = ...) -> int

Decode a code string back into the original integer.

  • code: Code string to decode
  • alphabet: Optional custom alphabet used in encoding
  • length: Expected code length excluding hyphen

Raises ValueError if invalid input.

verify(code: str, alphabet: str = ..., length: int = ...) -> bool

Check if a code is valid for the given alphabet and length.

  • code: Code string to verify
  • alphabet: Optional alphabet to validate against
  • length: Maximum code length excluding hyphen

Returns True if valid, False otherwise.

Customization

custom_alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ"
custom_length = 6

code = encode(987654, custom_alphabet, custom_length)
print(code)  # e.g. 'BAC-DEF'

number = decode(code, custom_alphabet, custom_length)
print(number)  # 987654

Tests

pytest

License

MIT

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

human_short_code-1.0.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

human_short_code-1.0.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file human_short_code-1.0.2.tar.gz.

File metadata

  • Download URL: human_short_code-1.0.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.2

File hashes

Hashes for human_short_code-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f44a459da1437285f5c4581fea35680f1620921eb8b3ac16d85569db97f70062
MD5 785a9413e261dc4865d1b1a701144ed4
BLAKE2b-256 f7f7b4f8a770cb68feee2a13b72d05cfda5632dc218f2851d571e37ccafa3eed

See more details on using hashes here.

File details

Details for the file human_short_code-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for human_short_code-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dc8cfaa4c21c6b5dd69ae4a320464c1b35ad7fd3ed8744de0e074ad32a3d0be6
MD5 5fd9118a44178536c6622c62631051e4
BLAKE2b-256 34f0635700365167c4f3a86ee46b554f2ba4d65b5a1595a9e7d99155d1d9a53f

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