forked from mrda/junkcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse.py
More file actions
executable file
·117 lines (109 loc) · 3.25 KB
/
morse.py
File metadata and controls
executable file
·117 lines (109 loc) · 3.25 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
#
# morse.py - Convert the input string to morse code, or do the inverse
#
# Copyright (C) 2015 Michael Davies (michael@the-davies.net)
#
# e.g. Encoding example:
# mrda@carbon:~/src/python$ ./morse.py hello
# . . . . . . - . . . - . . - - - - . . . . . . - . .
#
# e.g. Decoding example
# mrda@carbon:~/src/python$ ./morse.py dec ". . . . . . - . . . - . . - - - - . . . . . . - . ." # noqa
# hello there
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
import os
import sys
import string
LETTER_SPACE = ' ' * 1
WORD_SPACE = ' / '
table = {
'a': '.-',
'b': '-...',
'c': '-.-.',
'd': '-..',
'e': '.',
'f': '..-.',
'g': '--.',
'h': '....',
'i': '..',
'j': '.---',
'k': '-.-',
'l': '.-..',
'm': '--',
'n': '-.',
'o': '---',
'p': '.--.',
'q': '--.-',
'r': '.-.',
's': '...',
't': '-',
'u': '..-',
'v': '...-',
'w': '.--',
'x': '-..-',
'y': '-.--',
'z': '--..',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
'0': '-----',
' ': WORD_SPACE,
}
def encode(words):
str = ' '.join(x for x in words)
try:
encoded = LETTER_SPACE.join(x for x in [table[x.lower()]
for x in list(str)])
except KeyError:
exit("*** Unhandled letter found during encoding '%s'" % x)
# Remove superfluous gaps before a space.
return string.replace(encoded,
LETTER_SPACE + WORD_SPACE + LETTER_SPACE,
WORD_SPACE)
def decode(s):
final = ""
str = ''.join(x for x in s).rstrip()
for word in string.split(str, WORD_SPACE):
letters = string.split(word, LETTER_SPACE)
dec_word = ""
for letter in letters:
try:
dec_letter = ([key for key, value in table.items()
if value == letter][0])
except IndexError:
exit("*** Unhandled chars found during decoding '%s'" % letter)
dec_word += dec_letter
final += dec_word + " "
return final.rstrip()
if __name__ == "__main__":
if (len(sys.argv) == 1):
progname = os.path.basename(__file__)
sys.exit('Usage: %s [dec|decode] <data>' % progname)
elif (sys.argv[1] == "enc") or (sys.argv[1] == "encode"):
print encode(sys.argv[2:])
elif (sys.argv[1] == "dec") or (sys.argv[1] == "decode"):
print decode(sys.argv[2:])
else:
print encode(sys.argv[1:])