"""If the duration of a dot is taken to be one unit then that of a dash is three units. The space between the components of one character is one unit, between characters is three units and between words seven units. To indicate that a mistake has been made and for the receiver to delete the last word send ........ (eight dots). FOR WPM: The word PARIS was chosen as the standard length for CW code speed. Each dit counts for one count, each dah counts for three counts, intra-character spacing is one count, inter-character spacing is three counts and inter-word spacing is seven counts, so the word PARIS is exactly 50 counts. """ import winsound, os, time, sys, cmd class MorseError(Exception): """General exception class""" pass char_morse={'A':'. -', '\304':'. - . -', '\301':'. - - . -', '\305':'. - - . -', 'B':'- . . .', 'C':'- . - .', '\307':'- - - -', 'D':'- . .', 'E':'.', '\311':'. . - . .', 'F':'. . - .', 'G':'- - .', 'H':'. . . .', 'I':'. .', 'J':'. - - -', 'K':'- . -', 'L':'. - . .', 'M':'- -', 'N':'- .', '\321':'- - . - -', 'O':'- - -', '\326':'- - - .', 'P':'. - - .', 'Q':'- - . -', 'R':'. - .', 'S':'. . .', 'T':'-', 'U':'. . -', '\334':'. . - -', 'V':'. . . -', 'W':'. - -', 'X':'- . . -', 'Y':'- . - -', 'Z':'- - . .', '1':'. - - - -', '2':'. . . - -', '3':'. . . - -', '4':'. . . . -', '5':'. . . . .', '6':'- . . . .', '7':'- - . . .', '8':'- - - . .', '9':'- - - - .', '0':'- - - - -', ',':'- - . . - -', '.':'. - . - . -', '?':'. . - - . .', ';':'- . - . -', ':':'- - - . . .', '/':'- . . - .', '-':'- . . . . -', '\'':'. - - - - .', '(':'- . - - . -',')':'- . - - . -', '[':'- . - - . -',']':'- . - - . -', '{':'- . - - . -','}':'- . - - . -', '_':'. . - - . -'} def reverseDict(dict): reversed_dict={} for current_key in dict.keys(): current_value=dict[current_key] reversed_dict[current_value]=current_key return reversed_dict temp_morse_char=reverseDict(char_morse) def removeSpaces(dict): new_dict={} for current_key in dict.keys(): new_dict[current_key.replace(' ','')]=dict[current_key] return new_dict morse_char=removeSpaces(temp_morse_char) def textToMorse(text): text=text.upper() morse='' for char in text: if char==' ': morse+=' ' continue else: try: morse+=char_morse[char] except KeyError: raise MorseError, 'Unknown symbol -- %s' % char morse+=' ' continue morse=morse.strip() return morse def morseToText(code): space_cnt=0 code_cache='' text='' for char in code: if char==' ': space_cnt+=1 if space_cnt==3: try: text+=morse_char[code_cache] except KeyError: raise MorseError, 'Unknown symbol -- %s' % char code_cache='' elif char=='.' or char=='-': code_cache+=char if space_cnt==7: text+=' ' space_cnt=0 else: raise MorseError, 'Unkown symbol -- %s' % char if code_cache: text+=morse_char[code_cache] return text def calcWPM(wpm): cpm=wpm*50.0 sec_unit=60/cpm ms_unit=sec_unit*1000 return ms_unit def playMorse(code,ms_unit=250,frequency=1000): if os.name!='nt': raise MorseError, 'Not running under Windows' else: sec_unit=ms_unit/1000.0 for char in code: if char==' ': time.sleep(sec_unit) continue elif char=='.': winsound.Beep(frequency,ms_unit) continue elif char=='-': winsound.Beep(frequency,ms_unit*3) continue else: raise MorseError, 'Unrecognized symbol -- %s' % char class TextMorse(cmd.Cmd): def __init__(self): self.prompt='>>>' if os.name=='nt': self.intro='Welcome to the language of the West -- Morse code\n\n\ This program will give you a chance to change words into Morse code or \nMorse code into words. It will also give you the associated sounds as the code comes up on the screen.\n\ Here is all you have to do:\n\ \tI. Words into Code:\n\ \t\t1) When you see ">>>", type in "convert " and the words you want\ \t\tconverted into Morse code.\n\ \t\t2) Press the "Enter" key.\n\n\ \tII. Code into Words:\n\ \t\t1) When you see ">>>", type in convert " and the Morse code you\ \t\t\twant converted into words. For a ".", use the period key. For\ \t\t\ta "-", use the dash key (it isbetween the "0" key and the "=" \ \t\t\tkey).\n\ \t\t2) Press the "Enter" key.\n\n\n\ When you want to exit, type "exit".\n\n\n' else: self.intro='Welcome to morsecode.py\n\nTo see a list of commands, just type "?" at the prompt.\n\n\n' self.cmdloop() def do_convert(self, stuff): if stuff[0]=='.' or stuff[0]=='-': returned=morseToText(stuff) print print returned print else: returned=textToMorse(stuff) print for char in returned: sys.stdout.write(char) playMorse(char) print print def help_convert(self): print print 'Convert converts its arguments into the opposite form that it was passed in as: Morse code to text or text to Morse code.\ If it is passed in text, the Morse code will be returned while the Morse code is played through the PC speakers (requires Windows).' print def do_straight_convert(self, stuff): print if stuff[0]=='.' or stuff[0]=='-': returned=morseToText(stuff) print returned else: print textToMorse(stuff) print def help_straight_convert(self,stuff): print print 'Straight_convert acts just like convert, but it does not automatically play the Morse code through the PC speakers.' print def do_play(self,stuff): playMorse(stuff) def help_play(self): print print 'Play will play any Morse code passed in to it through your PC speakers (Windows required).' print def do_WPM(self, stuff): print returned=calcWPM(int(stuff)) print '%s ms per unit' % returned print def help_WPM(self): print print' WPM takes in the words-per-minute speed you would want Morse code to be played back at and returns the speed (in milliseconds) each unit would need to be at to be going at that WPM speed.' print def do_exit(self,stuff): sys.exit() def help_exit(self): print print 'Exit quits the program.' print def emptyline(self): print response=raw_input('Do you really want to exit? (yes/no):') if response.lower() in ('yes','y'): sys.exit() if __name__=='__main__': TextMorse()