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

Uploaded Python 3

File details

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

File metadata

  • Download URL: youtube_id-1.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 a488f2dab4ade24cc6398d34040c47ce68119f3823198604254f209c603c8a84
MD5 8fcc26b239b795d20e4f8d5bd3ab86e7
BLAKE2b-256 a67aa82e31a2ca7c4727d1ebaa99ad9f3c08a91c8eeb58e7d4729927f3c0e77b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: youtube_id-1.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c8860ee12c4fd29653961d2438f0643ada78d953770c9a1617dcd16599ee247e
MD5 5aa09b8c4df7f13561d93ba90fceacbb
BLAKE2b-256 81806a63d563bf7fd21bc13b17e176f5b70504f6f999cf07b278a4a322203f90

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