Skip to content

Commit 1744eed

Browse files
committed
Added new codec: manchester
1 parent b13c842 commit 1744eed

4 files changed

Lines changed: 76 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This library extends the native `codecs` library and provides some new encodings
2626
`gray` | text <-> gray encoded text | aka reflected binary code
2727
`html` | text <-> HTML entities | implements entities according to [this reference](https://dev.w3.org/html5/html-author/charref)
2828
`leetspeak` | text <-> leetspeak encoded text | based on minimalistic elite speaking rules
29+
`manchester` | text <-> manchester encoded text | XORes each bit of the input with `01`
2930
`markdown` | markdown --> HTML | unidirectional
3031
`morse` | text <-> morse encoded text | uses whitespace as a separator
3132
`navajo` | text <-> Navajo | only handles letters (not full words from the Navajo dictionary)
@@ -49,6 +50,7 @@ A few variants are also implemented.
4950
:---: | :---: | ---
5051
`baudot-spaced` | text <-> Baudot code groups of bits | groups of 5 bits are whitespace-separated
5152
`baudot-tape` | text <-> Baudot code tape | outputs a string that looks like a perforated tape
53+
`manchester-inverted` | text <-> manchester encoded text | XORes each bit of the input with `10`
5254
`octal-spaced` | text <-> octal digits (whitespace-separated) | dummy octal conversion
5355
`ordinal-spaced` | text <-> ordinal digits (whitespace-separated) | dummy character ordinals conversion
5456
`southpark-icase` | text <-> Kenny's language | same as `southpark` but case insensitive

codext/binary/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
from .baudot import *
33
from .excess3 import *
44
from .gray import *
5+
from .manchester import *
56

codext/binary/manchester.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Manchester Codec - Manchester content encoding.
3+
4+
This codec:
5+
- en/decodes strings from str to str
6+
- en/decodes strings from bytes to bytes
7+
- decodes file content to str (read)
8+
- encodes file content from str to bytes (write)
9+
"""
10+
from ..__common__ import *
11+
12+
13+
__examples1__ = {'enc(manchester)': {'This is a test!': "fei\x95i\x96jZYUi\x96jZYUiVYUjeifjZjeYV"}}
14+
__examples2__ = {
15+
'enc(manchester-inverted|ethernet|ieee802.4)': {
16+
'This is a test!': "\x99\x9a\x96j\x96i\x95\xa5\xa6\xaa\x96i\x95\xa5\xa6\xaa\x96\xa9\xa6\xaa\x95\x9a\x96\x99"
17+
"\x95\xa5\x95\x9a\xa6\xa9",
18+
},
19+
}
20+
21+
22+
def manchester_encode(clock):
23+
def encode(text, errors="strict"):
24+
r = ""
25+
for c in text:
26+
bin_c = bin(ord(c))[2:].zfill(8)
27+
for i in range(0, 8, 4):
28+
r += chr(int("".join(2*bit for bit in bin_c[i:i+4]), 2) ^ clock)
29+
return r, len(b(text))
30+
return encode
31+
32+
33+
def manchester_decode(clock):
34+
def decode(text, errors="strict"):
35+
r, bits = "", ""
36+
for c in text:
37+
bin_c = bin(ord(c) ^ clock)[2:].zfill(8)
38+
bits += "".join(bin_c[i] for i in range(0, len(bin_c), 2))
39+
if len(bits) == 8:
40+
r += chr(int(bits, 2))
41+
bits = ""
42+
return r, len(b(text))
43+
return decode
44+
45+
46+
add("manchester", manchester_encode(0x55), manchester_decode(0x55), examples=__examples1__)
47+
add("manchester-inverted", manchester_encode(0xaa), manchester_decode(0xaa), examples=__examples2__,
48+
pattern=r"^(?:manchester-inverted|ethernet|ieee802\.4)$")
49+

docs/binary.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ It supports various formats such as CCITT-1 and CCITT-2, ITA1 and ITA2, and some
6060

6161
-----
6262

63-
### Excess-3 Code
63+
### Excess-3
6464

6565
Also called *Stibitz code*, it converts letters to ordinals, left-pads with zeros and then applies Excess-3 (Stibitz) code to get groups of 4 bits that are finally reassembled into bytes.
6666

@@ -77,7 +77,7 @@ Also called *Stibitz code*, it converts letters to ordinals, left-pads with zero
7777

7878
-----
7979

80-
### Gray Code
80+
### Gray
8181

8282
Also called *reflected binary code*, it implements the Gray code applied to characters while converted to bytes.
8383

@@ -96,3 +96,25 @@ Also called *reflected binary code*, it implements the Gray code applied to char
9696
'THIS IS A TEST'
9797
```
9898

99+
-----
100+
101+
### Manchester
102+
103+
This codec XORes each group of 4 bits of the input text with a 1-byte clock signal, e.g. `0x55` giving in binary `01010101`.
104+
105+
**Codec** | **Conversions** | **Aliases** | **Comment**
106+
:---: | :---: | --- | ---
107+
`manchester` | text <-> manchester encoded text | | clock signal is `0x55` (`01010101`)
108+
`manchester-inverted` | text <-> manchester encoded text | `ethernet`, `ieee802.4` | clock signal is `0xaa` (`10101010`)
109+
110+
```python
111+
>>> codext.encode("This is a test!", "manchester")
112+
'fei\x95i\x96jZYUi\x96jZYUiVYUjeifjZjeYV'
113+
>>> codext.decode("fei\x95i\x96jZYUi\x96jZYUiVYUjeifjZjeYV", "manchester")
114+
'This is a test!'
115+
>>> codext.encode("This is a test!", "manchester-inverted")
116+
'\x99\x9a\x96j\x96i\x95¥¦ª\x96i\x95¥¦ª\x96©¦ª\x95\x9a\x96\x99\x95¥\x95\x9a¦©'
117+
>>> codext.decode("\x99\x9a\x96j\x96i\x95¥¦ª\x96i\x95¥¦ª\x96©¦ª\x95\x9a\x96\x99\x95¥\x95\x9a¦©", "ethernet")
118+
'This is a test!'
119+
```
120+

0 commit comments

Comments
 (0)