Exemple #1
0
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)
Exemple #2
0
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"
Exemple #3
0
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()
Exemple #4
0
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
Exemple #5
0
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"
Exemple #6
0
from hypothesis import given, example
from hypothesis.strategies import (
    text,
    lists,
    data,
    sampled_from,
)

import sweetmorse.constants as c
from sweetmorse.morse import Morse


@pytest.mark.parametrize("one,other,are_equal", (
    (
        Morse.from_plain_text("same text"),
        Morse.from_plain_text("same text"),
        True,
    ),
    (
        Morse.from_plain_text("same text"),
        Morse.from_human_readable("... ._ __ .   _ . _.._ _"),
        True,
    ),
    (
        Morse.from_plain_text("one text"),
        Morse.from_plain_text("two text"),
        False,
    ),
    (
        Morse.from_plain_text("one text"),
Exemple #7
0
speed = 100  # bits per second
cycle_time = 1 / speed  # seconds per bit
end_of_file = "1010101010101010101"

print("Obtaining pin 21 for output...")
gpio_out = DigitalOutputDevice(21)  # voltage generated here

print("Obtaining pin 16 for input...")
print()
gpio_in = DigitalInputDevice(16)  # voltage read here
edges = []  # rising or falling voltage transitions
gpio_in.when_activated = lambda: edges.append((dt.datetime.now(), "1"))
gpio_in.when_deactivated = lambda: edges.append((dt.datetime.now(), "0"))

try:
    message = Morse.from_plain_text("sos")
    binary_outgoing = message.binary

    print("Sending message: " + message.plain_text)
    print("Converted to binary: " + binary_outgoing)
    print()
    print("Sending message at {} bits per second...".format(speed))
    print()

    for value in binary_outgoing + end_of_file:
        if value == '0':
            gpio_out.off()
        else:
            gpio_out.on()
        time.sleep(cycle_time)