Skip to main content

A lightning-fast, zero-dependency Base62 encoder/decoder for Python

Project description

b62

PyPI version Python Version Total downloads License Build Status

Because the world desperately needed another Base62 library.

What 🤔

Converts integers to strings and back. Revolutionary stuff.

  • Uses Rust 🦀 (because Python is apparently too slow for basic arithmetic)
  • Zero dependencies (we're very proud of this achievement)
  • Type hints (for people who forgot what integers look like)

Install

pip install b62

Shocking, we know.

Usage

import b62

# Turn number into string
encoded = b62.encode(123456789)  # "8M0kX"

# Turn string back into number  
decoded = b62.decode("8M0kX")    # 123456789

# It throws errors when you give it garbage
try:
    b62.decode("not_base62!")
except ValueError:
    print("Surprise! Invalid input breaks things.")

Batch Operations

For when you have many numbers to convert:

# Encode multiple integers (in parallel, because waiting is hard)
encoded = b62.encode_batch([1, 62, 123456789])  # ['1', '10', '8M0kX']

# Decode multiple strings (also in parallel)
decoded = b62.decode_batch(['1', '10', '8M0kX'])  # [1, 62, 123456789]

Performance ⚡

It's fast. Here are some numbers to make you feel better about your life choices:

  • Decode: ~52ns per operation (congratulations, you saved nanoseconds)
  • Encode: ~90ns per operation (your URL shortener will thank you)
  • Batch operations: Uses all your CPU cores (because why not)

Benchmarks run on a machine that probably costs more than your car. 💸

API

encode(num: int) -> str

Converts integer to Base62 string. Rocket science.

decode(string: str) -> int

Converts Base62 string back to integer. PhD not required.

encode_batch(nums: list[int]) -> list[str]

Like encode() but for people with lists.

decode_batch(strings: list[str]) -> list[int]

Like decode() but for people with more lists.

Character Set

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Yes, we counted. There are 62 of them.

Use Cases

  • URL shortening (because long URLs hurt feelings)
  • Database ID obfuscation (security through obscurity, naturally)
  • Compact serialization (for when JSON is "too verbose")
  • Impressing coworkers with Rust integration

Development

make test    # Run tests (they probably pass)
make ci      # Pretend you care about CI
make build   # Build the thing

License

MIT - Because we're not monsters. 😇


Now go forth and encode responsibly.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

b62-1.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (323.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

b62-1.1.3-cp312-cp312-macosx_11_0_arm64.whl (279.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file b62-1.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for b62-1.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0fa51f467ddc44a36588c33ea765004ef9723063bbf594165d5b8120ef6f3175
MD5 e03cd243e0693a6c5e2a479083e02f6a
BLAKE2b-256 e0ab1062dac4a6bb464f34595fba9a8367d8fa4b6870411ed02a77b58408001f

See more details on using hashes here.

File details

Details for the file b62-1.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

  • Download URL: b62-1.1.3-cp312-cp312-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 279.2 kB
  • Tags: CPython 3.12, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for b62-1.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b356589e679d5d2ac737b8d27c2fcf26041c7e40e263428cb44ee4ce27c8c856
MD5 80cf29e095cece56d2870c424196f07d
BLAKE2b-256 f9966e2beec3e47b61eedd0b518222da7a591974ea0c493695e096b443963b7b

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