python Tantek的NewBase60的Python实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Tantek的NewBase60的Python实现相关的知识,希望对你有一定的参考价值。

''' 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)

以上是关于python Tantek的NewBase60的Python实现的主要内容,如果未能解决你的问题,请参考以下文章

CSS Normalizar CSS(Tantek Celik)+ equiparar EMs y PXs

hCard 能理解编码吗?

python实现简单随机模拟——抛呀抛硬币

独特的 ptr 和 move 语义

请澄清 const 限定符传播

显示输出协议