Skip to content

Commit

Permalink
Added new codec: radio
Browse files Browse the repository at this point in the history
  • Loading branch information
dhondta committed Apr 27, 2020
1 parent e543430 commit 7282988
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
1 change: 1 addition & 0 deletions codext/languages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: UTF-8 -*-
from .braille import *
from .morse import *
from .radio import *
22 changes: 22 additions & 0 deletions codext/languages/radio.py
Original file line number Diff line number Diff line change
@@ -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?$")
17 changes: 17 additions & 0 deletions docs/encodings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7282988

Please sign in to comment.