-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtr.py
More file actions
executable file
·28 lines (24 loc) · 736 Bytes
/
tr.py
File metadata and controls
executable file
·28 lines (24 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
""" Generates translation table for a given key string """
from string import digits, ascii_letters
import sys
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: %s <key> <phrase>" % sys.argv[0])
sys.exit(1)
k = sys.argv[1]
p = sys.argv[2]
if len(k) != len(p):
print("Key and phrase must be the same length.")
sys.exit(1)
d={}
for key in digits + ascii_letters:
d[key] = key # default table
for key, val in zip(k, p):
d[key]=val
for key in sorted(d):
if d[key] in (' ', '!', '?', '"', "'"): # escape these
print('\%s' % d[key], end='')
else:
print(d[key], end='')
print()