From 728298853726905a3013c16771c4b067a4aaae19 Mon Sep 17 00:00:00 2001 From: dhondta Date: Mon, 27 Apr 2020 22:15:42 +0200 Subject: [PATCH] Added new codec: radio --- README.md | 1 + codext/languages/__init__.py | 1 + codext/languages/radio.py | 22 ++++++++++++++++++++++ docs/encodings.md | 17 +++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 codext/languages/radio.py diff --git a/README.md b/README.md index 3e17d92..1d27ecd 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ This library extends the native `codecs` library and provides some new encodings `markdown` | markdown --> HTML | unidirectional `morse` | morse <-> text | uses whitespace as a separator `nokia3310` | Nokia 3310 keystrokes <-> text | uses "`-`" as a separator for encoding, "`-`" or "`_`" or whitespace for decoding +`radio` | Radio <-> text | aka NATO or radio phonetic alphabet `rot-N` | ROT(N) <-> text | aka Caesar cipher (N belongs to [1,25]) `url` | URL <-> text | aka URL encoding `xor-N` | XOR(N) <-> text | XOR with a single byte (N belongs to [1,255]) diff --git a/codext/languages/__init__.py b/codext/languages/__init__.py index b320454..dd10f15 100644 --- a/codext/languages/__init__.py +++ b/codext/languages/__init__.py @@ -1,3 +1,4 @@ # -*- coding: UTF-8 -*- from .braille import * from .morse import * +from .radio import * diff --git a/codext/languages/radio.py b/codext/languages/radio.py new file mode 100644 index 0000000..2d1eb14 --- /dev/null +++ b/codext/languages/radio.py @@ -0,0 +1,22 @@ +# -*- coding: UTF-8 -*- +"""Radio Codec - NATO/Military phonetic alphabet 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 * + + +ENCMAP = { + 'A': "Alpha", 'B': "Bravo", 'C': "Charlie", 'D': "Delta", 'E': "Echo", 'F': "Foxtrot", 'G': "Golf", 'H': "Hotel", + 'I': "India", 'J': "Juliett", 'K': "Kilo", 'L': "Lima", 'M': "Mike", 'N': "November", 'O': "Oscar", 'P': "Papa", + 'Q': "Quebec", 'R': "Romeo", 'S': "Sierra", 'T': "Tango", 'U': "Uniform", 'V': "Victor", 'W': "Whiskey", + 'X': "X-ray", 'Y': "Yankee", 'Z': "Zulu", ' ': "/", +} + + +add_map("radio", ENCMAP, sep=" ", ignore_case="both", + pattern=r"^(?:military|nato|radio)(?:[-_]phonetic)?[-_]alphabet?$") diff --git a/docs/encodings.md b/docs/encodings.md index 507f75e..a3e178e 100644 --- a/docs/encodings.md +++ b/docs/encodings.md @@ -172,6 +172,23 @@ At this time, only Nokia 3310 keystrokes is supported. ----- +### Radio Alphabet + +This is also known as the [NATO phonetic alphabet](https://en.wikipedia.org/wiki/NATO_phonetic_alphabet). + +**Codec** | **Conversions** | **Aliases** | **Comment** +:---: | :---: | --- | --- +`radio` | Radio <-> text | `military_alphabet`, `nato-phonetic-alphabet`, `radio-alphabet` | + +```python +>>> codext.encode("foobar", "nato_phonetic_alphabet") +'Foxtrot Oscar Oscar Bravo Alpha Romeo' +>>> codext.decode("Foxtrot Oscar Oscar Bravo Alpha Romeo", "radio-alphabet") +'FOOBAR' +``` + +----- + ### ROT N This is a dynamic encoding, that is, it can be called with an integer to define the ROT offset. Encoding will apply a positive offset, decoding will apply a negative one.