-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
52 lines (45 loc) · 1.64 KB
/
main.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
import json
from utils.util import input_taker, check_in, check_out
class Morse:
f = open("morse-code.json")
morse_dict = json.load(f)
f.close()
def __init__(self, type_morse, val):
self.val = val
self.type_morse = type_morse
def converter(self):
if self.type_morse == "morse":
return self.convert(self.val, type_morse="morse")
elif self.type_morse == "text":
return self.convert(self.val, type_morse="text")
else:
raise Exception("Please give input as 'morse' or 'text' for the needed conversion.")
@staticmethod
def convert(val, type_morse):
if type_morse == "text":
val = list(val)
for i in range(len(val)):
if val[i] in Morse.morse_dict.keys():
val[i] = Morse.morse_dict[val[i]]
else:
val[i] = "*"
return '\\'.join(val)
else:
val = val.strip(' ').split('\\')
key_list = list(Morse.morse_dict.keys())
val_list = list(Morse.morse_dict.values())
for i in range(len(val)):
if val[i] in val_list:
pos = val_list.index(val[i])
val[i] = key_list[pos]
else:
val[i] = "*"
val = ''.join(val)
return ' '.join(val.split())
def init():
type_morse, val, out_type, in_type, in_file = input_taker()
val = val.lower()
lines = check_in(in_file, val)
morse_made = Morse(type_morse, lines)
converted = morse_made.converter()
check_out(out_type, converted, type_morse)