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

Uploaded Python 3

File details

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

File metadata

  • Download URL: human_short_code-1.0.1.tar.gz
  • Upload date:
  • Size: 4.2 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.1.tar.gz
Algorithm Hash digest
SHA256 1a01c9321c380cc8dfbf2cf6e7a74380753e8d1a95bbdcfecc8d64c43b242994
MD5 2512dad8ee4ca6ecbf100db64556790a
BLAKE2b-256 5f74e297aa57c8a498c033a9a023ae67e15e1c277e77723e7c5c81e1dc80a8d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for human_short_code-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 266bc7798a03984b023d07ef51b2669b300182056ac52a7ff28b4d044f9698df
MD5 789052fd48d9161c35d59d6e75fd5f06
BLAKE2b-256 c04e31a7393eecff5d922b4de33e0d7960ad02c94c6ef694efe98a1ca36ee074

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