Skip to content

Commit

Permalink
Added new codec: ordinals
Browse files Browse the repository at this point in the history
  • Loading branch information
dhondta committed Apr 28, 2020
1 parent 0ff9851 commit 57827dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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
`ordinals` | Ordinals <-> text | dummy character ordinals conversion
`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
Expand Down
16 changes: 16 additions & 0 deletions codext/others/ordinals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: UTF-8 -*-
"""Ordinals Codec - ordinals 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 = {chr(i): str(i) for i in range(256)}


add_map("ordinals", ENCMAP, sep=" ", pattern=r"^ordinals?$")
17 changes: 17 additions & 0 deletions docs/encodings.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ At this time, only Nokia 3310 keystrokes is supported.

-----

### Ordinals

This simple codec converts characters into their ordinals.

**Codec** | **Conversions** | **Aliases** | **Comment**
:---: | :---: | --- | ---
`ordinals` | Ordinals <-> text | `ordinal` |

```python
>>> codext.encode("this is a test", "ordinal")
'116 104 105 115 32 105 115 32 97 32 116 101 115 116'
>>> codext.decode("116 104 105 115 32 105 115 32 97 32 116 101 115 116", "ordinals")
'this is a test'
```

-----

### Radio Alphabet

This is also known as the [NATO phonetic alphabet](https://en.wikipedia.org/wiki/NATO_phonetic_alphabet).
Expand Down

0 comments on commit 57827dc

Please sign in to comment.