Skip to main content

This is for representation of Python data (int, bytes, str) in the desired base system, like base62 system.

Project description

This module is for expressing Python data in the desired base system, such as 62 base system.

Introduce

It supports up 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.)

It started with inspiration from base62 and numpy.base_repr 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

Installing Base Repr

Base Repr is available on PyPI:

$ python -m pip install base-repr

Base Repr supports Python 3.6+

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'

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 class to create and use the desired base system.

>>> 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.

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

base_repr-1.0.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

base_repr-1.0.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file base_repr-1.0.2.tar.gz.

File metadata

  • Download URL: base_repr-1.0.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for base_repr-1.0.2.tar.gz
Algorithm Hash digest
SHA256 2f5672f04335ce45fe4476702aad8372b2a77954806756c151ef80ac73f72212
MD5 0cab7cf35af9c0ab9d16c5987c266cdc
BLAKE2b-256 0a41a5d8b687e40c94088274e3d33ac5cfb2aa71b6bdf3c3013beb36b0625962

See more details on using hashes here.

File details

Details for the file base_repr-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: base_repr-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for base_repr-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0dd3300d91e02ac99fab45bcdd174476ec3669585fba45122e4fe02cacc3cbe6
MD5 18600a3bc7c97f5b3a4f2b744dcc2932
BLAKE2b-256 82ebe9da1c48a5d0912da701ba4468695bc365045102323ba710024dbc36bd3e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page