Skip to main content

Generate YouTube-style short IDs from numbers. Lightweight, fast, and reversible base62 encoder with optional obfuscation.

Project description

YouTubeID for Python

Generate YouTube-style short IDs from numbers. Lightweight, fast, and reversible base62 encoder with optional obfuscation.

PyPI version Python 3.10+ License: MIT Coverage

Other programming languages

Features

  • Lightweight - Zero dependencies, pure Python
  • Fast - Simple base62 encoding/decoding
  • Reversible - Encode and decode without data loss
  • Obfuscation - Optional secure key to shuffle the dictionary
  • Type-safe - Full type hints for IDE support

Installation

pip install youtube-id

Or with Poetry:

poetry add youtube-id

Quick Start

import yid_py

# Encode a number to a short string
yid_py.to_alphanumeric(12345)  # -> 'dnh'

# Decode back to number
yid_py.to_numeric('dnh')  # -> 12345

Usage

Basic Encoding/Decoding

import yid_py

# Number to alphanumeric
yid_py.to_alphanumeric(0)        # -> 'a'
yid_py.to_alphanumeric(61)       # -> 'Z'
yid_py.to_alphanumeric(62)       # -> 'ba'
yid_py.to_alphanumeric(12345)    # -> 'dnh'
yid_py.to_alphanumeric(999999)   # -> 'eGGf'

# Alphanumeric to number
yid_py.to_numeric('dnh')         # -> 12345

With Secure Key (Obfuscation)

Use a secure key to shuffle the dictionary, making IDs harder to predict:

import yid_py

# Without key
yid_py.to_alphanumeric(12345)                      # -> 'dnh'

# With secure key (different output)
yid_py.to_alphanumeric(12345, secure_key='secret') # -> 'UDJ'

# Decode with the same key
yid_py.to_numeric('UDJ', secure_key='secret')      # -> 12345

Case Transformation

from yid_py import to_alphanumeric, Transform

to_alphanumeric(12345, transform=Transform.UPPER)  # -> 'DNH'
to_alphanumeric(12345, transform=Transform.LOWER)  # -> 'dnh'

Encoder Factory

For repeated operations with the same settings, use the Encoder class:

import yid_py

# Create encoder with preset options
enc = yid_py.create(secure_key='my-secret', transform=yid_py.Transform.UPPER)

# Encode
enc.encode(12345)      # -> 'HQJ' (transformed for display)
enc.encode_raw(12345)  # -> 'hqj' (raw for storage/decoding)

# Decode
enc.decode('hqj')      # -> 12345

API Reference

Functions

to_alphanumeric(number, pad_up=0, secure_key=None, transform=Transform.NONE)

Convert a number to a short alphanumeric string.

Parameter Type Default Description
number int required The number to convert
pad_up int 0 Padding value
secure_key str | None None Key to shuffle dictionary
transform Transform NONE Case transformation

to_numeric(alphanumeric, pad_up=0, secure_key=None)

Convert an alphanumeric string back to a number.

Parameter Type Default Description
alphanumeric str required The string to convert
pad_up int 0 Padding value (must match encoding)
secure_key str | None None Key (must match encoding)

create(pad_up=0, secure_key=None, transform=Transform.NONE)

Create a reusable Encoder instance with preset options.

Classes

Encoder

Reusable encoder with preset options.

  • encode(number) - Convert number to alphanumeric (with transform)
  • encode_raw(number) - Convert number to alphanumeric (without transform)
  • decode(alphanumeric) - Convert alphanumeric to number

Transform

Enum for case transformation:

  • Transform.NONE - No transformation
  • Transform.UPPER - Uppercase output
  • Transform.LOWER - Lowercase output

Use Cases

  • URL shorteners - Convert database IDs to short URLs
  • Public IDs - Hide sequential database IDs from users
  • Share codes - Generate readable codes for sharing
  • Invite links - Create short invitation tokens

Performance

The library uses base62 encoding (a-z, 0-9, A-Z) which provides:

Number Range Output Length
0 - 61 1 character
62 - 3,843 2 characters
3,844 - 238,327 3 characters
238,328 - 14,776,335 4 characters

Contributing

Contributions are welcome! Please read our Contributing Guidelines and Code of Conduct.

# Clone and setup
git clone https://github.com/wow-apps/yid-py.git
cd yid-py
python3.10 -m venv venv
source venv/bin/activate
poetry install --with dev

# Run tests
pytest --cov=yid_py

Credits

A Python port of the YouTube-style ID generator originally created by Kevin van Zonneveld and contributors.

License

MIT © Oleksii Samara, Kevin van Zonneveld

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

youtube_id-1.0.4.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

youtube_id-1.0.4-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file youtube_id-1.0.4.tar.gz.

File metadata

  • Download URL: youtube_id-1.0.4.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.10.20 Linux/6.17.0-1008-azure

File hashes

Hashes for youtube_id-1.0.4.tar.gz
Algorithm Hash digest
SHA256 b9c4cf76d65bfaa020cf948566a5cc16751771dc558dcbc1417c53a8f8878525
MD5 7f9827d89b825e5f64380302d4398292
BLAKE2b-256 7d0cee469720ec20b8d220d7c1c605dee2ad89b69a29095dc25c83c019fcf1c8

See more details on using hashes here.

File details

Details for the file youtube_id-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: youtube_id-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.10.20 Linux/6.17.0-1008-azure

File hashes

Hashes for youtube_id-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8c230c5865a88311472c0bb84f483f151daf25bc39a9ad2457c0e7effd901fa3
MD5 3441c7480c71ae5202dee6d72a14f79b
BLAKE2b-256 353a620e19dac782f5b59545f064fdb478b4a2325cf7a42238c573b1af2b0e26

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