Skip to content

Commit 4ff58be

Browse files
committed
Added new codec: excess3
1 parent 628c139 commit 4ff58be

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This library extends the native `codecs` library and provides some new encodings
2222
`baudot` | text <-> Baudot code bits | supports CCITT-1, CCITT-2, EU/FR, ITA1, ITA2, MTK-2 (Python3 only), UK, ...
2323
`braille` | text <-> braille symbols | Python 3 only
2424
`dna` | text <-> DNA-N sequence | implements the 8 rules of DNA sequences (N belongs to [1,8])
25+
`excess3` | text <-> XS3 encoded text | uses Excess-3 (aka Stibitz code) binary encoding to convert characters from their ordinals
2526
`gray` | text <-> gray encoded text | aka reflected binary code
2627
`html` | text <-> HTML entities | implements entities according to [this reference](https://dev.w3.org/html5/html-author/charref)
2728
`leetspeak` | text <-> leetspeak encoded text | based on minimalistic elite speaking rules

codext/binary/excess3.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
from ..__common__ import *
1111

1212

13-
__examples__ = {'enc(excess3|xs-3|stibitz)': {'This is a test': ";t7C\x84H6T8D\x83e<£eD\x944D\x84I"}}
13+
__examples__ = {
14+
'enc(excess3|xs-3|stibitz)': {
15+
'This is a test!': ";t7C\x84H6T8D\x83e<\xa3eD\x944D\x84I6`",
16+
'This is another test ': ";t7C\x84H6T8D\x83e<\xa4CDDICt4DseD\x944D\x84I6P",
17+
},
18+
}
1419

1520

1621
CODE = {
@@ -28,25 +33,25 @@ def excess3_encode(text, errors="strict"):
2833
r += chr(int(b, 2))
2934
b = ""
3035
if len(b) > 0:
31-
b += "0000"
32-
r += chr(int(b, 2))
36+
r += chr(int(b + "0000", 2))
3337
return r, len(text)
3438

3539

3640
def excess3_decode(text, errors="strict"):
37-
code = {v: k for k, v in CODE}
41+
code = {v: k for k, v in CODE.items()}
3842
r, d = "", ""
3943
for c in text:
40-
b = bin(ord(c))[2:].zfill(8)
44+
bin_c = bin(ord(c))[2:].zfill(8)
4145
for i in range(0, 8, 4):
42-
d += code[b[i:i+4]]
46+
try:
47+
d += code[bin_c[i:i+4]]
48+
except KeyError: # (normal case) occurs when 0000 was used for padding
49+
break
4350
if len(d) == 3:
4451
r += chr(int(d))
4552
d = ""
46-
if len(d) > 0:
47-
r += chr(int(d))
48-
return r, len(text)
53+
return r, len(b(text))
4954

5055

51-
add("excess3", excess3_encode, excess3_decode, pattern=r"^(?:excess\-3|xs\-?3|stibitz)$")
56+
add("excess3", excess3_encode, excess3_decode, pattern=r"^(?:excess\-?3|xs\-?3|stibitz)$", text=False)
5257

docs/encodings.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ CACTCGGTCGGCCATATGTTCGGCCATATGTTCGTCTGTTCACTCGCCCATACACT
129129

130130
-----
131131

132+
### Excess-3 Code
133+
134+
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.
135+
136+
**Codec** | **Conversions** | **Aliases** | **Comment**
137+
:---: | :---: | --- | ---
138+
`excess3` | text <-> XS3 encoded text | `excess-3`, `xs3`, `stibitz` |
139+
140+
```python
141+
>>> codext.encode("This is a test!", "excess-3")
142+
';t7C\x84H6T8D\x83e<£eD\x944D\x84I6`'
143+
>>> codext.decode(";t7C\x84H6T8D\x83e<£eD\x944D\x84I6`", "stibitz")
144+
'This is a test!'
145+
```
146+
147+
-----
148+
132149
### Gray Code
133150

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

0 commit comments

Comments
 (0)