Skip to content

pykob.morse

Les Kerr edited this page May 27, 2020 · 1 revision

pykob.morse — text-to-Morse and Morse-to-text conversion

Source code: pykob/morse.py

This module defines classes whose objects convert text to Morse code and vice versa. Both American Morse and International Code are supported. The Sender class optionally supports Farnsworth spacing, either between each character or just between words.

Sender class

  • The Sender class recognizes the following punctuation characters: ampersand (&), period (.), comma (,), question mark (?), exclamation point (!), and break character (=).
  • The numeral 0 is sent as the letter O.
  • Unrecognized characters are treated as spaces, except apostrophe (') and hyphen (-) which are treated as half-spaces.
  • A tilde (~) causes the circuit to open (at the beginning of a transmission, for example), and an underscore (_) causes the circuit to close.

class pykob.morse.Sender(wpm, cwpm=0, codeType=AMERICAN, spacing=CHARSPACING)

  • wpm: the overall speed of the code to be sent
  • cwpm: the code speed of individual characters; defaults to wpm if cwpm not specified or is less than wpm
  • codeType: AMERICAN or INTERNATIONAL
  • spacing: CHARSPACING or WORDSPACING

Sender method

Sender.encode(char)

  • char: the character to be converted to Morse
  • returns the code sequence corresponding to the character char; the code sequence may be empty if char is a space or an unrecognized character

Reader class

  • The Reader class recognizes the same punctuation as the Sender class, except the ampersand is always decoded as the letter pair ES.
  • The long dash is always decoded as the letter L, never the numeral 0.
  • A circuit opening is decoded as a tilde (~), and a circuit closing as an underscore (_).
  • The callback function is called each time a character is decoded. The number of spaces preceding that character is provided by the spacing parameter, which may be a fractional number of spaces and even slightly negative if the characters are closely spaced.
  • Unrecognized characters are returned as a string consisting of dots and dashes within square brackets (e.g. [ . - - . - . ].

class pykob.morse.Reader(wpm=20, codeType=AMERICAN, callback=None)

  • wpm: the nominal code speed of the incoming Morse; the reader adapts automatically to the actual speed of subsequent code sequences
  • codeType: AMERICAN or INTERNATIONAL
  • callback: function called whenever a character is decoded; the callback parameter is required

Reader methods

Reader.decode(code)

Reader.setWPM(wpm)

  • wpm: reset the nominal code speed to the given value; the reader continues to adapt to the actual speed of subsequent code sequences

Reader.flush()

  • forces any remaining code sequences in the decoder's buffer to be decoded

Callback function

callback(char, spacing)

  • char: the character decoded
  • spacing: the fractional number of spaces to add before the character (can be negative)