Welcome, guest | Sign In | My Account | Store | Cart

After seeing another recipe recently that helps people with Morse Code, I was inspired to clean up the following code just a bit to make it worthy for the rest of the world. After being executed, it waits for lines of text to be entered, compiles each one in turn into timing codes, and plays them back with a simple execution

Python, 99 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from time import sleep
from winsound import Beep

################################################################################

# CONSTANTS DEFINED BY USER

FQC = 800
DOT = 0.1

################################################################################

# CONSTANTS DEFINED BY STANDARD

DAH = DOT * 3
SEP = DOT * 7

CODE = {' ': ' ',
        "'": '.----.',
        '(': '-.--.-',
        ')': '-.--.-',
        ',': '--..--',
        '-': '-....-',
        '.': '.-.-.-',
        '/': '-..-.',
        '0': '-----',
        '1': '.----',
        '2': '..---',
        '3': '...--',
        '4': '....-',
        '5': '.....',
        '6': '-....',
        '7': '--...',
        '8': '---..',
        '9': '----.',
        ':': '---...',
        ';': '-.-.-.',
        '?': '..--..',
        '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': '--..',
        '_': '..--.-'}

################################################################################

# MAIN PROGRAM FUNCTIONS

def main():
    "Loop while getting, compiling, and executing given text."
    data = input()
    while data:
        _execute(_compile(data))
        data = input()

def _compile(data):
    "Format string as a series of timing codes for execution."
    code = []
    for word in ''.join(key for key in data.upper() if key in CODE).split():
        for character in word:
            for symbol in CODE[character]:
                code.extend(((DAH, DOT)[symbol == '.'], DOT))
            code[-1] = DAH
        code[-1] = SEP
    return tuple(code)

def _execute(code, ops=(lambda t: Beep(FQC, round(t * 1000)), sleep)):
    "Run timing codes with 'Beep' and 'sleep' by their order."
    for i, time in enumerate(code):
        ops[i & 1](time)

################################################################################

# STANDARD CONDITIONAL EXECUTION

if __name__ == '__main__':
    main()

2 comments

Barry Walker 9 years, 10 months ago  # | flag

Works mighty fine except my internal PC speaker is almost inaudible and makes for difficult _reading_...

Nice one...

Bazza, G0LCU...

Barry Walker 9 years, 10 months ago  # | flag

Noticed an error.

Your open parenthesis line 20 is wrong!

It should be "-.--."

Bazza, G0LCU...