Module garoupa.misc.encoding.base777

Base to enable shorter ids, but at the expense of heavily adopting utf-8 (up to 2-byte) chars

777 chars provide 19.99631153679756 and 39.99262307569413 digits for 32 and 64 versions. The choice is not arbitrary. 777 is ideal to balance variability even in the most significant digit for any version (UT32.4, UT40.4, UT64.4, …) of Hosh.

Functions

def b777dec(string)
Expand source code
def b777dec(string):
    """
    Usage:

    >>> b777dec("ևև") == 777**2 - 1
    True

    Parameters
    ----------
    string

    Returns
    -------

    """
    num = 0
    for char in string:
        num = num * 777 + rev_alphabet[char]
    return num

Usage:

>>> b777dec("ևև") == 777**2 - 1
True

Parameters

string
 

Returns

def b777enc(num, digits)
Expand source code
def b777enc(num, digits):
    """
    Usage:

    >>> b777enc(123456, 10)
    '00000000ģӪ'
    >>> n = b777dec(b777enc(123456, 10))
    >>> n == 123456
    True
    """
    encoded = ""
    while num:
        num, rem = divmod(num, 777)
        encoded = alphabet[rem] + encoded
    return encoded.rjust(digits, "0")

Usage:

>>> b777enc(123456, 10)
'00000000ģӪ'
>>> n = b777dec(b777enc(123456, 10))
>>> n == 123456
True