We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Smithy Code

Fredrik Lundh | April 2006 | Originally posted to online.effbot.org

$ more smithy_code.py

from string import uppercase as alphabet
from itertools import izip, cycle
import sys


def fib(n): # from the python tutorial
    a, b = 0, 1
    while b < n:
        yield b
        a, b = b, a+b

def decode(code):
    for c, d in izip(code, cycle(fib(len(alphabet)))):
        yield alphabet[(alphabet.index(c) + d - 1) % len(alphabet)]


$ python smithy_code.py