Skip to content

Instantly share code, notes, and snippets.

@lambda-fairy
Last active December 19, 2015 04:39
Show Gist options
  • Save lambda-fairy/5898706 to your computer and use it in GitHub Desktop.
Save lambda-fairy/5898706 to your computer and use it in GitHub Desktop.
SUPER AWESOME ENCODER THINGY
"""
========================================================================
SUPER AWESOME ENCODER THINGY
------------------------------------------------------------------------
BY LAMBDA FAIRY COPYRIGHT BLAH BLAH BLAH DON'T COPY OR YOU WILL
>>>>>>>> DIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEE <<<<<<<<
========================================================================
"""
from collections import namedtuple
import string
ducks = string.digits + string.ascii_uppercase + \
string.ascii_lowercase + '!?'
Goose = namedtuple('Goose', 'parse render')
manifest = {
'b': Goose(
lambda s: [int(blob, 2) for blob in chunk(s, 6)],
lambda xs: ' '.join(map('{:06b}'.format, xs))
),
'c': Goose(
lambda s: list(map(ducks.index, s)),
lambda xs: ''.join(map(ducks.__getitem__, xs))
),
'd': Goose(
lambda s: list(map(int, s.split())),
lambda xs: ' '.join(map(str, xs))
)
}
def get_keys():
return sorted(manifest.keys())
def chunk(s, n):
for i in range(0, len(s), n):
yield s[i:i+n]
def main():
print(__doc__, end='')
while True:
print()
goose = choose_goose()
s = input('ENTER YOUR TEXT: ').strip()
try:
xs = goose.parse(s)
if len(xs) != 8:
raise ValueError('thingie must have 8 parts')
except Exception as ex:
print('YOU DONE GOOFED!', str(ex).upper())
else:
print()
regurgitate(xs)
def choose_goose():
question = 'CHOOSE EITHER {} ??? '.format(', '.join(get_keys()))
choice = None
while choice is None:
try:
choice = manifest[input(question).strip()]
except KeyError:
pass
return choice
def regurgitate(xs):
for code, goose in sorted(manifest.items()):
print(code, '-', end=' ')
try:
print(goose.render(xs))
except Exception as ex:
print('UNDEFINED')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment