A lightning-fast, zero-dependency Base62 encoder/decoder for Python
Project description
b62
🎉 A lightning-fast, zero-dependency and friendly Base62 encoder/decoder for Python! Tame your data with style and a smile.
Features
- ⚡ High-performance Base62 encode/decode for integers
- 🐍 Friendly Python interface backed by Rust
- 🔧 Seamless Rust-Python integration via PyO3
- 🛡️ Type-safe with comprehensive error handling
- 🧪 Thoroughly tested with property-based testing
- 📦 Zero runtime dependencies
Installation
From PyPI
pip install b62
Usage
import b62
# Encode an integer to Base62
encoded = b62.encode(123456789)
print(encoded) # Output: "8M0kX"
# Decode a Base62 string back to integer
decoded = b62.decode("8M0kX")
print(decoded) # Output: 123456789
# Error handling
try:
b62.decode("invalid!")
except ValueError as e:
print(f"Invalid Base62 string: {e}")
# Round-trip validation
original = 987654321
encoded = b62.encode(original)
decoded = b62.decode(encoded)
assert original == decoded # Always True!
Performance
b62 is built with Rust for maximum performance and delivers exceptional speed:
Benchmark Results
Single Operations (nanoseconds per operation):
- Decode large string: ~113ns (8,872 ops/sec)
- Encode large number: ~203ns (4,934 ops/sec)
- Decode edge cases: ~396ns (2,525 ops/sec)
- Encode edge cases: ~796ns (1,256 ops/sec)
Batch Operations (100,000 operations):
- Encoding: ~0.014s (7.2M ops/sec)
- Decoding: ~0.020s (5.1M ops/sec)
- Round-trip: ~0.033s (3.0M ops/sec)
Performance Characteristics:
- Encoding: ~10-15x faster than pure Python implementations
- Decoding: ~15-20x faster than pure Python implementations
- Memory: Minimal memory footprint with zero allocations for small numbers
- CPU: Optimized for both small and large integers
- Scalability: Consistent performance across number ranges (0 to 2^63-1)
Technical Implementation
The library uses a highly optimized Rust implementation with PyO3 bindings:
- Character Set:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz - Algorithm: Efficient division/modulo operations with pre-allocated buffers
- Error Handling: Comprehensive validation with detailed error messages
- Type Safety: Full type annotations and runtime validation
- Memory Management: Zero-copy operations where possible
API Reference
b62.encode(num: int) -> str
Encodes an integer to Base62 string representation.
Parameters:
num(int): Integer to encode (must be non-negative)
Returns:
str: Base62 encoded string
Raises:
OverflowError: If the integer is too large for u64
b62.decode(s: str) -> int
Decodes a Base62 string back to an integer.
Parameters:
s(str): Base62 string to decode
Returns:
int: Decoded integer
Raises:
ValueError: If the string contains invalid Base62 characters
Development
Running Tests
make test
Code Quality
make ci
Building
make build_package
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Guidelines
- Follow PEP 8 style guidelines
- Add tests for new functionality
- Update documentation as needed
- Ensure all tests pass before submitting
License
MIT License - free and open for all! 🎉
Why b62?
Keep your integer conversions speedy and stylish! Perfect for:
- URL shortening
- Database ID encoding
- Compact data serialization
- Performance-critical applications
🦀🐍💨
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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file b62-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: b62-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 238.3 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3bbee2b2b7ca6b95d03cb39840dc2223f8fc482e3a8ada1926177f0a8b44636
|
|
| MD5 |
d135185f21d55d8a47b1a32e161f57c6
|
|
| BLAKE2b-256 |
85bcc95c3b84802d99298472f6cbfcef3e68b759b7b555d8299f7ae3a63ad4d0
|
File details
Details for the file b62-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: b62-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 211.8 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a28367b63f0fa52c07023a55fb3cb4c633a1a4b0647451b2d4c279e775293e54
|
|
| MD5 |
91ea9da082cb69740b14334ad8d258bb
|
|
| BLAKE2b-256 |
98c7dea9f246cada0c58c895cf560a664ac2d9d55bd708ece932c8f0621977a3
|