Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting an Object Oriented morse code translator
    primarykey
    data
    text
    <p>below is my solution to reddit.com/r/dailyprogrammer #93 (easy). It is a python script that translates between english and morse code.</p> <p>Is <code>String_To_Translate</code> a good example of a class? Am I violating convention? How can I improve this code and make it more readable and pythonic?</p> <p>Any advice is much appreciated!</p> <pre><code>import re morse_code_dict = { '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':'-----', ',':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-', ' ':' ', } class String_To_Translate: def __init__(self, string): self.string = string self.Translated = '' self.lang = '' def determine_lang(self): exp = r'[a-zA-Z\,\?\/\(\)]'#regexp for non morse code char if re.search(exp, self.string) == None: self.lang = 'm' else: self.lang = 'eng' def prep(self): if self.lang == 'eng': #get rid of whitespace &amp; warn user (self.string, numWhitespaceReplacements) = re.subn(r'\s', ' ', self.string) if numWhitespaceReplacements &gt; 0: print 'Some whitespace characters have been replaced with spaces.' #get rid of characters not in dict &amp; warn user (self.string, numReplacements) = re.subn(r'[^a-zA-Z\,\?\/\(\)\.\-\s]', ' ', self.string) if numReplacements &gt; 0: print 'Some ({0}) characters were unrecognized and could not be translated, they have been replaced by spaces.'.format(numReplacements) #convert to lowercase self.string = self.string.lower() def translate_from_morse(self): eng_dict = dict([(v, k) for (k, v) in morse_code_dict.iteritems()]) def split_morse(string): d = ' ' charList = [[char for char in word.split() + [' ']] for word in string.split(d) if word != ''] charList = [item for sublist in charList for item in sublist] print charList return charList charList = split_morse(self.string) for char in charList: self.Translated += eng_dict[char] def translate_from_eng(self): charList=list(self.string) for char in charList: self.Translated += morse_code_dict[char] + ' ' def translate(self): self.determine_lang() if self.lang == 'm': self.translate_from_morse() elif self.lang == 'eng': self.prep() self.translate_from_eng() else: print 'shit' if __name__=="__main__": translateme = String_To_Translate(raw_input('enter a string: ')) translateme.translate() print translateme.Translated print 'translating back' test = String_To_Translate(translateme.Translated) test.translate() print test.Translated </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload