Skip to main content

base62

Build Status Coveralls PyPI

A Python module for base62 encoding. Ported from PHP code that I wrote in mid-2000, which can be found here.

Rationale

When writing a web application, often times we would like to keep the URLs short.

http://localhost/posts/V1Biicwt

This certainly gives a more concise look than the following.

http://localhost/posts/109237591284123

This was the original motivation to write this module, but there shall be much more broader potential use cases of this module. The main advantage of base62 is that it is URL-safe (as opposed to base64) due to the lack of special characters such as ‘/’ or ‘=’. Another key aspect is that the alphabetical orders of the original (unencoded) data is preserved when encoded. In other words, encoded data can be sorted without being decoded at all.

Installation

base62 can be installed via pypi. Unfortunately, the package name base62 on pypi had already been occupied by someone else, so we had to go by pybase62.

pip install pybase62

Alternatively, you may clone the code to manually install it.

git clone https://github.com/suminb/base62
cd base62 && python setup.py install

Usage

The following section describes a basic usage of base62.

>>> import base62

>>> base62.encode(34441886726)
'base62'

>>> base62.decode('base62')
34441886726

From version 0.2.0, base62 supports bytes array encoding as well.

>>> base62.encodebytes(b'\0')
0

>>> base62.encodebytes(b'\xff\xff')
H31

>>> base62.decodebytes('0')
b''

>>> base62.decodebytes('1')
b'\x01'

Some may be inclined to assume that they both take bytes types as input due to their namings. However, encodebytes() takes bytes types whereas decodebytes() takes str types as an input. They are intended to be commutative, so that a roundtrip between both functions yields the original value.

Formally speaking, we say function f and g commute if f∘g = g∘f where f(g(x)) = (f∘g)(x).

Therefore, we may expect the following relationships:

  • value == encodebytes(decodebytes(value))

  • value == decodebytes(encodebytes(value))

Tests

You may run some test cases to ensure all functionalities are operational.

pytest -v

If pytest is not installed, you may want to run the following command:

pip install -r tests/requirements.txt

Deployment

Deploy a source package (to pypi) as follows:

python setup.py sdist upload

Release files for pybase62 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for pybase62 1.0.0
File Interpreter ABI Platform
pybase62-1.0.0-py3-none-any.whl Python 3 none any Details

Release files / pybase62-1.0.0-py3-none-any.whl

Download URL pybase62-1.0.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60539ad956ec9e9de091bc7ae88c9550bc2fa17f503050cf34d021b75e73cb27
BLAKE2b-256 checksum
How to use checksums
11078fe498201daece90af1725cf48d5de9d1a87ea8d457df52c986ef926ff33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release
Image

1.0.0 This release

1 release file

Image

0.6.0

2 release files

Image

0.5.0

2 release files

Image

0.4.3

2 release files

Image

0.4.2

1 release file

Image

0.4.1

1 release file

Image

0.4.0

1 release file

Image

0.3.3

1 release file

Image

0.3.2

1 release file

Image

0.3.1

1 release file

Image

0.3.0

1 release file

Image

0.2.0

1 release file

Image

0.1.3

1 release file

Image

0.1.2

1 release file

Image

0.1.1

1 release file

Image

0.1.0

1 release file

Image Anthropic, PBC Visionary sponsor Image Bloomberg Visionary sponsor Image Hudson River Trading Visionary sponsor Image Meta Visionary sponsor Image NVIDIA Visionary sponsor Image Microsoft Sustainability sponsor Image Depot Continuous Integration Image AWS Cloud computing and Security Sponsor Image Datadog Monitoring Image Fastly CDN Image Google Download Analytics Image Sentry Error logging Image StatusPage Status page