Skip to content

Instantly share code, notes, and snippets.

@markng
Created July 23, 2011 21:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markng/1101904 to your computer and use it in GitHub Desktop.
Save markng/1101904 to your computer and use it in GitHub Desktop.
Interactive translation tool.
#!/usr/bin/env python
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
filename = sys.argv[1]
f = open(filename, 'r')
text = f.read()
translate = {}
for char in text:
# just print if it's a space.
if char.isspace():
sys.stdout.write(char)
elif char not in translate:
# we haven't translated this character yet
sys.stdout.write(char)
old_trans_char = None
trans_char = ''
tty.setraw(sys.stdin.fileno())
trans_char = sys.stdin.read(1)
termios.tcsetattr(fd, termios.TCSANOW, old_settings)
old_trans_char = trans_char
if not trans_char.isspace():
# if someone presses space, they're not guessing yet. Otherwise, commit translation, remove old char
translate[char] = trans_char
sys.stdout.write('\b')
sys.stdout.write(trans_char)
else:
# translated
sys.stdout.write(translate[char])
except Exception, e:
print e
finally:
termios.tcsetattr(fd, termios.TCSANOW, old_settings)
# print out the translate dictionary, to get it back
print translate
### The first part of steve's document has a translation dictionary thus :
translate = {
'!': '5',
'#': '6',
'"': '/',
'%': '0',
'$': '2',
"'": 'M',
'&': '1',
')': '.',
'(': 's',
'+': 'l',
'*': 'E',
'-': 'a',
',': 'e',
'/': 'o',
'.': 'n',
'1': 'S',
'0': 'r',
'3': 'x',
'2': 'i',
'5': 'Y',
'4': 'N',
'7': 't',
'6': '$',
'9': 'P',
'8': 'v',
';': '4',
':': 'd',
'=': 'h',
'<': 'J',
'?': 'A',
'>': 'R',
'A': '3',
'@': 'Z',
'C': 'C',
'B': 'D',
'E': 'm',
'D': 'W',
'G': 'b',
'F': 'G',
'I': 'L',
'H': 'K',
'K': 'u',
'J': 'w',
'M': 'k',
'L': 'c',
'O': 'T',
'N': 'H',
'Q': 'B',
'P': 'X',
'S': '8',
'R': 'p',
'U': 'I',
'T': 'g',
'W': 'F',
'V': '9',
'Y': 'O',
'X': 'y',
'[': '7',
'Z': 'U',
']': 'f',
'\\': 'z',
'_': 'V',
'^': '&',
'a': ',',
'`': 'Q',
'c': 'q',
'b': 'j',
'e': '"',
'd': 'W',
'g': ')',
'f': '(',
'i': ';',
'j': '"'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment