Simple Morse code interpreter
Overall, this code was very simple and easy to follow. I just have a few nit-picks to point out.



The function syntax

def func_name(param_name: type) -&gt; return_type:


is new in Python 3. However, it is not a very common thing to be used in Python, and I don't think it looks very pythonic.

Your code will work just as well without making functions like this, so I recommend that you stick to using the usual syntax:

def func_name(param_name):




This clunky statement:

if msg[0][0] in ['.', '-', '/']:
    return True
else:
    return False


can be reduced to a more pythonic and clean version:

return msg[0][0] in ['.', '-', '/']




Comments like this:

# type: list


are really unnecessary and just add noise to your code. Just from reading your code, someone reviewing it should be able to determine what type each variable is.



You need to do some more error catching.

For example, say you are translating a string from morse code to english. What if you encounter a character that is not a valid morse character? Your program will raise an error here:

for char in msg:
    c = eng[char] # &lt;======
    out += "{} ".format(c)


Therefore, this is what you should do:


Encase that line in a try/except.
In the except, throw a custom InvalidMorseLetter exception.
Exit


Here is what I mean:

try:
    c = eng[char] # type: str
except KeyError:
    raise(InvalidMorseLetter)


where InvalidMorseLetter is:

class InvalidMorseLetter(Exception):
    pass


Read here and here.

In the code, I wrote to catch a KeyError because that is the error that would be raised if an invalid dictionary key is used.

You should use a custom exception because it specifically describes what went wrong with your specific code, rather than just a general KeyError.

You should do something very similar to this for when you are translating english to morse.



Thanks to Zenohm for this.

In your translation functions, you should not be constantly modifying a single string each iteration:

c = morse[char] # type: str
out += c # &lt;======================


The reason: python strings are immutable. Therefore, by constantly changing the string, you are actually creating a new string each and every time. This is very inefficient.

As Zenohm posted in the comments, you should be doing something along the lines of this:

return ''.join("{} ".format(eng[char]) for char in msg))



  While the modifying of a single string does not a bad habit make and
  isn't really a problem here, I think it would be good if he started
  avoiding the modifying of an immutable object right off the bat.
  return ''.join("{} ".format(eng[char]) for char in msg) Could be used
  in place of his original string modification and would help to
  encourage better practice in future.