-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse.py
110 lines (101 loc) · 2.95 KB
/
morse.py
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
#!/usr/bin/env python
# __author__ = 'vageli'
import cgi, cgitb
import sys
print "Content-Type: text/html"
print
#Get all fields
form = cgi.FieldStorage()
MESSAGE = form.getvalue('string')
CODE = {'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': '--..',
'0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..',
'9': '----.', ' ': '_'
}
# From: https://en.wikipedia.org/wiki/Morse_code#Representation.2C_timing_and_speeds
DIT = 60
DAH = DIT * 3
INTER = DIT # Gap between sounds in letter
SHORT = DIT * 3 # Between letters
MED = DIT * 7 # Between words
def to_csv(msg):
"""Takes a msg in morse code and converts to a signal"""
csv = []
for each in msg:
if each is '.':
csv.append(DIT)
csv.append(INTER)
elif each == '..':
csv.append(DIT)
csv.append(INTER)
csv.append(DIT)
csv.append(INTER)
elif each == '...':
csv.append(DIT)
csv.append(INTER)
csv.append(DIT)
csv.append(INTER)
csv.append(DIT)
csv.append(INTER)
elif each == '....':
csv.append(DIT)
csv.append(INTER)
csv.append(DIT)
csv.append(INTER)
csv.append(DIT)
csv.append(INTER)
csv.append(DIT)
csv.append(INTER)
elif each == '-':
csv.append(DAH)
csv.append(INTER)
elif each == '--':
csv.append(DAH)
csv.append(INTER)
csv.append(DAH)
csv.append(INTER)
elif each == '---':
csv.append(DAH)
csv.append(INTER)
csv.append(DAH)
csv.append(INTER)
csv.append(DAH)
csv.append(INTER)
elif each == '----':
csv.append(DAH)
csv.append(INTER)
csv.append(DAH)
csv.append(INTER)
csv.append(DAH)
csv.append(INTER)
csv.append(DAH)
csv.append(INTER)
elif each == ' ':
csv.append(SHORT)
elif each == '_':
csv.append(MED)
return csv
def main():%
if MESSAGE is not None:
msg = MESSAGE
else:
msg = raw_input('MESSAGE: ')
translated = []
for char in msg:
try:
translated.append(CODE[char.upper()])
except:
pass
csv = to_csv(translated)
print str(csv)[1:-1].replace(' ', '')
if __name__ == "__main__":
main()