Search code examples
pythonbase36

Uneven base-36 support in Python?


I've been working with base-36 recently and have never been satisfied with the usual answer to converting ints into base-36 strings. It looks a little imbalanced…

def to_base36(value):
    if not isinstance(value, int):
        raise TypeError("expected int, got %s: %r" % (value.__class__.__name__, value))

    if value == 0:
        return "0"

    if value < 0:
        sign = "-"
        value = -value
    else:
        sign = ""

    result = []

    while value:
        value, mod = divmod(value, 36)
        result.append("0123456789abcdefghijklmnopqrstuvwxyz"[mod])

    return sign + "".join(reversed(result))

…when compared to converting back…

def from_base36(value):
    return int(value, 36)

Does Python really not include this particular battery?


Solution

  • Correct. Not every store carries N or J batteries.