-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmorse.py
More file actions
40 lines (32 loc) · 1.66 KB
/
morse.py
File metadata and controls
40 lines (32 loc) · 1.66 KB
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
# -*- coding: UTF-8 -*-
"""Morse Codec - morse content encoding.
This codec:
- en/decodes strings from str to str
- en/decodes strings from bytes to bytes
- decodes file content to str (read)
- encodes file content from str to bytes (write)
"""
from ..__common__ import *
__examples__ = {
'enc(morse|morse/-.)': {'this is a test': "- .... .. ... / .. ... / .- / - . ... -"},
'enc(morse-/AB)': {'this is a test': "A BBBB BB BBB / BB BBB / BA / A B BBB A"},
'enc(morse-01)': {'this is a test': "0 1111 11 111 - 11 111 - 10 - 0 1 111 0"},
}
__guess__ = ["morse", "morse/_.", "morse-/01", "morse-01", "morse-/ab", "morse-ab", "morse-/AB", "morse-AB"]
ENCMAP = {
# letters
'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': "--..",
# digits
'1': ".----", '2': "..---", '3': "...--", '4': "....-", '5': ".....", '6': "-....", '7': "--...", '8': "---..",
'9': "----.", '0': "-----",
# punctuation
',': "--..--", '.': ".-.-.-", ':' : "---...", '?': "..--..", '/': "-..-.", '-': "-....-", '=' : "-...-",
'(': "-.--.", ')': "-.--.-", '@' : ".--.-.", '\'': ".----.", '_': "..--.-", '!': "-.-.--", '&': ".-...",
'"': ".-..-.", ';': "-.-.-.", '$': "...-..-",
# word separator
' ' : "/",
}
add_map("morse", ENCMAP, "#", " ", ignore_case="encode", pattern=r"^morse([-_]?.{3})?$", printables_rate=1.,
expansion_factor=(2.8, .6))