def main(): import argparse import sys choices = [c.PLAIN, c.HUMAN_READABLE, c.BINARY] parser = argparse.ArgumentParser( description='Convert stdin between plain text and Morse code. ' 'Formats are {}'.format(choices)) parser.add_argument(dest='from_format', metavar='from_format', action='store', default=None, type=str, choices=choices, help='the format for input data') parser.add_argument(dest='to_format', metavar='to_format', action='store', default=None, type=str, choices=choices, help='the format for output data') args = parser.parse_args() input_str = sys.stdin.read() morse = (Morse.from_plain_text(input_str) if args.from_format == c.PLAIN else Morse.from_human_readable(input_str) if args.from_format == c.HUMAN_READABLE else Morse.from_binary(input_str)) print(morse.plain_text if args.to_format == c.PLAIN else morse.human_readable if args.to_format == c.HUMAN_READABLE else morse.binary)
def test_sos_from_all_formats_with_newline(): """Typical usage will terminate input with a newline. Be ready!""" assert Morse.from_plain_text("SOS\n").plain_text == "SOS" assert Morse.from_human_readable("... ___ ...\n").plain_text == "SOS" assert Morse.from_binary( "101010001110111011100010101\n").plain_text == "SOS"
def test_there_and_back_plain_text(value): print(value) morse = Morse.from_plain_text(value) morse_from_human_readable = Morse.from_human_readable(morse.human_readable) assert morse_from_human_readable.plain_text == value.strip().upper() morse_from_binary = Morse.from_binary(morse.binary) assert morse_from_binary.plain_text == value.strip().upper()
def test_there_and_back_binary(data): binary_chars = data.draw( lists(elements=sampled_from(list(c.BINARY_CHARS)))) separators = data.draw( lists( elements=sampled_from((c.BINARY_CHAR_GAP, c.BINARY_WORD_GAP)), min_size=max(len(binary_chars) - 1, 0), max_size=max(len(binary_chars) - 1, 0), )) value = ''.join(char for char in itertools.chain.from_iterable( itertools.zip_longest(binary_chars, separators, fillvalue=None)) if char is not None) print(value) morse = Morse.from_binary(value) morse_from_plain_text = Morse.from_plain_text(morse.plain_text) assert morse_from_plain_text.binary == value morse_from_human_readable = Morse.from_human_readable(morse.human_readable) assert morse_from_human_readable.binary == value
def test_sos_from_all_formats(): assert Morse.from_plain_text("SOS").plain_text == "SOS" assert Morse.from_human_readable("... ___ ...").plain_text == "SOS" assert Morse.from_binary("101010001110111011100010101").plain_text == "SOS"
if value == '0': gpio_out.off() else: gpio_out.on() time.sleep(cycle_time) # Interpret all the 0s and 1s we've collected binary_incoming = [] current_edge = edges[0] current_time = edges[0][0] + dt.timedelta(seconds=(cycle_time / 2)) while edges: if edges[0][0] < current_time: current_edge = edges.pop(0) # This is far from a realtime system, we'll just correct any # timing drift sync every time we see an edge. current_time = current_edge[0] + dt.timedelta(seconds=(cycle_time / 2)) binary_incoming.append(current_edge[1]) current_time += dt.timedelta(seconds=cycle_time) binary_received = ''.join(binary_incoming[:-len(end_of_file)]) print("Received binary: " + binary_received) message_received = Morse.from_binary(binary_received) print("Received message: " + message_received.plain_text) finally: gpio_in.close() gpio_out.close()