Skip to main content

Base Representation

This module is for represent Python data in the desired base system, like Base36 and Base62.

Introduce

This supports from 2 base systems to 62 base systems that can be expressed only with digits and case sensitive alphabets.

This module was developed to make it easier to use for URLs or other data fields requiring length restrictions by expressing large numbers or hash values to a shorter length.

You can also encode strings or decode encoded strings. (Strings are not guaranteed to be shortened.)

This started with inspiration from base62 idea and numpy.base_repr library and aim to be universally used in multiple systems such as Base36 in systems where Base62 cannot be applied because it is case-insensitive systems.

https://img.shields.io/pypi/v/base-repr https://img.shields.io/pypi/wheel/base-repr https://img.shields.io/pypi/l/base-repr https://img.shields.io/pypi/pyversions/base-repr https://img.shields.io/pypi/dm/base-repr

Installing Base Representation

Base Representation is available on PyPI:

$ python -m pip install base-repr

Base Representation is tested on Python 3.7+

How to use

Using functions

>>> import base_repr
# Number to string representation
>>> base_repr.int_to_repr(1234, base=62)
'Ju'
>>> base_repr.repr_to_int('Ju', base=62)
1234

# Bytes to string representation
# bytes([1, 2, 3, 4]) == b'\x01\x02\x03\x04'
>>> base_repr.bytes_to_repr(bytes([1, 2, 3, 4]), base=62, byteorder='little')
'4YPMP'
>>> base_repr.repr_to_bytes('4YPMP', base=62, byteorder='little')
b'\x01\x02\x03\x04'

# sha256
import hashlib
# hashlib.sha256(b'Hello World!').hexdigest() == '7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069'
>>> base_repr.bytes_to_repr(hashlib.sha256(b'Hello World!').digest(), base=62, byteorder='little')
'P20z9unz25ZdkS9kKi65j9Rb8jqP5knHG08sDpvsQDn'

# string
>>> base_repr.str_to_repr('Hello', base=62, byteorder='little', encoding='utf-8')
'8QMwioi'
>>> base_repr.repr_to_str('8QMwioi', base=62, byteorder='little', encoding='utf-8')
'Hello'

# simple wrapper to union above 3 functions for data type int, bytes and str.
>>> base_repr.to_repr('int or bytes or str', base=62)
'3xDXjUL88hX1Dq95EbXKCI7cOP'

Choose your base to represent (or from representation)

You can choose base system from 2 to 62. 2 is used by default.

>>> import base_repr
>>> base_repr.to_repr(1234)
'10011010010'
>>> base_repr.to_repr(1234, base=2)
'10011010010'
>>> base_repr.to_repr(1234, base=16)
'4D2'
>>> base_repr.to_repr(1234, base=62)
'Ju'

Choose padding to represent

You can use padding to fit the number of digits you want. The default is 0.

>>> base_repr.to_repr(10, padding=10)
'0000001010'
>>> base_repr.repr_to_int('0000001010')
10

Choose your byteorder which ‘little’ or ‘big’

You can choose byteorder from ‘little’ or ‘big’ for bytes or str data type. System values (sys.byteorder) are used by default.

>>> import base_repr
>>> base_repr.to_repr('Hello', base=62, byteorder='little')
'8QMwioi'
>>> base_repr.repr_to_str('8QMwioi', base=62, byteorder='little')
'Hello'

>>> base_repr.to_repr('Hello', base=62, byteorder='big')
'5TP3P3v'
>>> base_repr.repr_to_str('5TP3P3v', base=62, byteorder='big')
'Hello'

Choose your encoding

You can choose encoding to represent (from representation) str data type. The default is ‘utf-8’.

>>> import base_repr
>>> base_repr.to_repr('English', base=62, encoding='utf-8')
'2AeWfKYcJ3'
>>> base_repr.repr_to_str('2AeWfKYcJ3', base=62, encoding='utf-8')
'English'

>>> base_repr.to_repr('한글', base=62, encoding='utf-8')
'eBkBTHSP'
>>> base_repr.repr_to_str('eBkBTHSP', base=62, encoding='utf-8')
'한글'

>>> base_repr.to_repr('🍺', base=62, encoding='utf-16')
'17m0y4sjX'
>>> base_repr.repr_to_str('17m0y4sjX', base=62, encoding='utf-16')
'🍺'

Using class

You can use classes to reduce repetitive parameters.

>>> from base_repr import Base62
>>> base62 = Base62(byteorder='little', encoding='utf-8')
>>> base62.bytes_to_repr(bytes([1, 2, 3, 4]))
'4YPMP'
>>> base62.repr_to_bytes('4YPMP')
b'\x01\x02\x03\x04'
>>> base62.to_repr('int or bytes or str')
'3xDXjUL88hX1Dq95EbXKCI7cOP'

You can also inherit a base class to create your own desired base system simply.

>>> from base_repr import BaseRepr
>>> class Base36(BaseRepr):
... def __init__(self, padding: int = 0, byteorder: str = sys.byteorder, encoding: str = 'utf-8'):
...     super().__init__(36, padding, byteorder, encoding)

>>> base = Base36(padding=0, byteorder='big', encoding='utf-8')
>>> base.int_to_repr(1234)
'YA'
>>> base.repr_to_int('YA')
1234

Base62 and Base36 are already defined in module and ready to use.

Metadata

Release files for base-repr 1.0.7

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

Source distribution (sdist)

Source distribution for base-repr 1.0.7
File Size Uploaded Provenance Actions
base_repr-1.0.7.tar.gz 7.1 kB Without provenance Details for base_repr-1.0.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for base-repr 1.0.7
File Interpreter ABI Platform Actions
base_repr-1.0.7-py3-none-any.whl Python 3 none any Details for base_repr-1.0.7-py3-none-any.whl

Total release size: 15.5 kB

Release files / base_repr-1.0.7.tar.gz

Download URL base_repr-1.0.7.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f25795b7d977f23ec6970e07a876fb38546eed1b1708a0f223fedd921d7e1600
BLAKE2b-256 checksum
How to use checksums
b70cdc2c25f9461bd627df4a5e6208fdbb127d37b310089c1741559a87eed7b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.3

Release files / base_repr-1.0.7-py3-none-any.whl

Download URL base_repr-1.0.7-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
efd520972645710889736c87d60d0b554a58393f89f2ad993a694c829e954247
BLAKE2b-256 checksum
How to use checksums
72d2d62282a621dfe4902f49e947cea7ebb31ac3c551b3e443fc44f3a0f08a4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.3

Security for base-repr 1.0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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