draftply Download

Morse Code Converter

← All samples

A .dplybundle packs a document together with its libraries in one file. The libraries are collapsed below — click to expand.

morse_converter.dplyEditor
1---
2version: 0.1.0
3precision: 2
4form_title: Morse Code Converter
5form_description: Convert between text and Morse code. Pick a direction, type your input, read the result.
6form_inputs: Direction|direction[Text to Morse:1,Morse to Text:0], Input|input
7form_outputs: Result|result
8---
9# draftply Pro — Morse Code Converter
10# The library below is bundled with this document and imported here.
11# Tap the form icon for an input mask.
12import "morse_code.dplylib"
13
14# ── Your input ─────────────────────────────────
15input = "Hello World!"
16direction = 1 # 1 = text to morse, 0 = morse to text
17
18# ── Conversion ─────────────────────────────────
19# The chip options of a form field carry numbers, so the direction is one.
20result = to_morse(input) if direction == 1 else from_morse(input)
21
22"Direction (1 = to morse)": direction
23"Input": input
24"Result": result
25
26# ── Round trip: encode, then decode again ──────
27"Encoded": to_morse("SOS")
28"Decoded again": from_morse(to_morse("SOS"))
29
30# ── The library's parts, usable on their own ───
31# Both tables must stay the same length — the n-th character has the n-th symbol.
32"Table size": len(morse_chars())
33"Symbols": len(morse_codes())
34"Split": split_spaces("..- .- ...")
35"Where is Q?": index_of(morse_chars(), "Q")
morse_code.dplyliblibrary
morse_code.dplylibEditor
1---
2version: 1.1.0
3min_app_version: 0.1.0
4---
5# morse_code.dplylib — Morse encoding and decoding for radio amateurs.
6# Import with: import "morse_code.dplylib" (Pro: functions + loops)
7#
8# to_morse("SOS") -> "... --- ..."
9# from_morse("... --- ...") -> "SOS"
10#
11# Letters are separated by one space, words by " / ".
12#
13# How this works: two parallel tables. The n-th character of morse_chars()
14# has the n-th symbol of morse_codes(), so encoding and decoding are the
15# same lookup read in opposite directions — find the index in one table,
16# take that index from the other.
17#
18# Everything here rests on three string features: `s[i]` reads a character,
19# `==` compares text, and `for c in text:` walks a string. A library exports
20# functions and not variables, so the two tables are functions as well.
21
22# ── A split, since draftply has none ────────────────────
23# "..- .-" -> ["..-", ".-"]. Collect characters until a space ends the part;
24# the space appended to the input flushes the last one.
25def split_spaces(s):
26 out = list()
27 part = ""
28 for c in s + " ":
29 if c == " ":
30 if len(part) > 0:
31 out = append(out, part)
32 part = ""
33 else:
34 part = part + c
35 return out
36
37# Where does x sit in items? -1 if it is not in there at all. Works on a list
38# and on a string, because both answer to len() and [i].
39def index_of(items, x):
40 for i in 0..len(items)-1:
41 if items[i] == x: return i
42 return -1
43
44# draftply has no upper(), but ord()/char() bridge text and numbers:
45# a lower-case letter sits 32 code points above its upper-case twin.
46def upper(c):
47 k = ord(c)
48 return char(k - 32) if k >= 97 and k <= 122 else c
49
50# ── The alphabet ────────────────────────────────────────
51# 53 characters, and below them their 53 symbols in the same order.
52def morse_chars():
53 s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # 26 letters
54 s = s + "0123456789" # 10 digits
55 s = s + ".,?'!()&:;=+-_$@" + "\"" # 17 punctuation marks
56 return s
57
58def morse_codes():
59 s = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. --" # A-M
60 s = s + " -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.." # N-Z
61 s = s + " ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----." # 0-9
62 s = s + " .-.-.- --..-- ..--.. .----. -.-.-- -.--. -.--.- .-..." # . , ? ' ! ( ) &
63 s = s + " ---... -.-.-. -...- .-.-. -....- ..--.- ...-..- .--.-. .-..-." # : ; = + - _ $ @ "
64 return split_spaces(s)
65
66# ── Text → morse ────────────────────────────────────────
67def to_morse(text):
68 chars = morse_chars()
69 codes = morse_codes()
70 out = ""
71 for c in text:
72 i = index_of(chars, upper(c))
73 if c == " ":
74 sym = "/" # word gap; the separator below spaces it out
75 elif i >= 0:
76 sym = codes[i]
77 else:
78 sym = "" # unknown character: skipped
79 if len(sym) > 0:
80 out = out + (" " if len(out) > 0 else "") + sym
81 return out
82
83# ── Morse → text ────────────────────────────────────────
84# Collect dots and dashes until something else ends the symbol; the space
85# appended to the input flushes the last one.
86def from_morse(code):
87 chars = morse_chars()
88 codes = morse_codes()
89 out = ""
90 sym = ""
91 for c in code + " ":
92 if c == "." or c == "-":
93 sym = sym + c
94 else:
95 if len(sym) > 0:
96 i = index_of(codes, sym)
97 out = out + (chars[i] if i >= 0 else "")
98 sym = ""
99 if c == "/":
100 out = out + " "
101 return out