''' newbase60.py - Python implementation of Tantek's NewBase60
http://ttk.me/w/NewBase60
by Sean Summers <seansummers@gmail.com>
License: https://creativecommons.org/licenses/by/4.0/
'''
import functools
import itertools
import string
AMBIGUOUS_CHARACTERS = {
'I': '1',
'O': '0',
'l': '1',
}
CHARACTERS = ''.join(
character
for character in itertools.chain(
string.digits,
string.ascii_uppercase,
'_',
string.ascii_lowercase,
) if character not in AMBIGUOUS_CHARACTERS
)
BASE = len(CHARACTERS)
def _accumulate(n, c):
c = AMBIGUOUS_CHARACTERS[c] if c in AMBIGUOUS_CHARACTERS else c
return n * BASE + max(0, CHARACTERS.find(c))
def _values(n):
while n:
n, c = divmod(n, BASE)
yield c
def num_to_sxg(n):
sxg = [CHARACTERS[c] for c in _values(n)] or '0'
return ''.join(reversed(sxg))
def sxg_to_num(s):
return functools.reduce(_accumulate, s, 0)