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

Uploaded Python 3

File details

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

File metadata

  • Download URL: youtube_id-1.0.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.19 Linux/6.11.0-1018-azure

File hashes

Hashes for youtube_id-1.0.2.tar.gz
Algorithm Hash digest
SHA256 cdd919cb1e28783289c45fcfe3290492e6b5e2100503b5bdf89a60c160d80e6d
MD5 38ed13a5299caa96477f45160516b567
BLAKE2b-256 4695a1670a62724ac3888463ed31b1d84d1ffab8dcff287fa9876276ca93e898

See more details on using hashes here.

File details

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

File metadata

  • Download URL: youtube_id-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.19 Linux/6.11.0-1018-azure

File hashes

Hashes for youtube_id-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1cf7856b488d63bf2b36a45839008b2a98c4202e6d7ef0e9ce2a9c4e99657345
MD5 b2fda67dcdeae9fa352cd5c9e871aef5
BLAKE2b-256 136db7965635f096ff7ae38702a571aa1531e8f4d1a36ce8efbb134a71ac7434

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