Browse Source

Merge pull request 'first version of morseencoder' (#1) from morseencoder into master

Reviewed-on: #1
master
rick 2 months ago
parent
commit
6774dd33da
  1. 1
      .gitignore
  2. 135
      src/morseencoder.py

1
.gitignore vendored

@ -0,0 +1 @@
text.txt

135
src/morseencoder.py

@ -0,0 +1,135 @@
#!/bin/python
#This small code takes latin words/letters and translate them to morse code
import sys, getopt
__morsed = {
'a': '.-',
'b': '-...',
'c': '-.-.',
'd': '-..',
'e': '.',
'f': '..-.',
'g': '--.',
'h': '....',
'i': '..',
'j': '.---',
'k': '-.-',
'l': '.-..',
'm': '--',
'n': '-.',
'o': '---',
'p': '.--.',
'q': '--.-',
'r': '.-.',
's': '...',
't': '-',
'u': '..-',
'v': '...-',
'w': '.--',
'y': '-.--',
'z': '--..',
'1': '.----',
'2': '..---',
'3': '...--.',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
'0': '-----',
' ': ' ',
'.': '.-.-.-',
',': '--..--',
':': '---..--',
';': '-.-.-',
'?': '..--..',
'!': '-.-.--',
'-': '-....-',
'_': '..--.-',
'(': '-.--.',
')': '-.--.-',
'"': '.-..-.',
'=': '-...-',
'+': '.-.-.',
'/': '-..-.',
'\n': '...---...',
}
#getops
def main(argv):
led_output = False
translated = ''
try:
opts, args = getopt.getopt(argv, 'hif:l')
except getopt.GetoptError:
help()
sys.exit(2)
#check if we got an cli option
if not opts:
help()
sys.exit(2)
#loop over all options
for opt, arg in opts:
if opt == '-h':
help()
sys.exit()
elif opt == '-i':
translated = translate(interact())
elif opt == '-f':
translated = (parsefile(arg))
elif opt == '-l':
led_output = True
if led_output != True:
print(translated)
else:
print('LED PARTY starts here!')
led_party(translated)
def help():
print(""" usage:
-i interactive (type text interactivly)
-f file (parse text from a file)
""")
def interact():
return input('Sentence to translate: ')
def translate(input):
output = []
for letters in list(input.lower()):
output.append(__morsed[letters] + ' ')
return(''.join(output))
def parsefile(filepath):
try:
with open(filepath, 'r') as fileinput:
return(translate(''.join(fileinput.readlines())))
except IOError:
print("Can't open file, please check path and access permissions")
sys.exit(2)
def led_party(translated):
charakters = (list(translated))
for charakter in charakters:
if charakter == ' ':
print('sleep 1')
elif charakter == '-':
print('led_on')
print('sleep 3')
print('led_off')
elif charakter == '.':
print('led_on')
print('sleep 1')
print('led_off')
#Start of Programm
main(sys.argv[1:])
Loading…
Cancel
Save