generated from mwc/lab_compression
Created a new file ascii6.py that follows similar style to ascii7.py encode(text), decode(text) register_codec and uses Bits. the docstrict also includes compression rate with is about .72 or 72%.
This commit is contained in:
29
text_codecs/ascii6.py
Normal file
29
text_codecs/ascii6.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from easybits import Bits
|
||||
from register import register_codec
|
||||
|
||||
CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!"
|
||||
|
||||
def encode(text):
|
||||
result = Bits()
|
||||
text = text.upper()
|
||||
for char in text:
|
||||
if char in CHARACTERS:
|
||||
index = CHARACTERS.index(char)
|
||||
b = Bits(index, length=6)
|
||||
result = result.concat(b)
|
||||
return result.bytes
|
||||
|
||||
def decode(data):
|
||||
bits = Bits(bytes(data))
|
||||
text = ""
|
||||
for i in range(0, len(bits), 6):
|
||||
b = bits[i:i+6]
|
||||
if len(b)<6:
|
||||
break
|
||||
|
||||
index = b.int
|
||||
if index < len(CHARACTERS):
|
||||
text+= CHARACTERS[index]
|
||||
return text
|
||||
|
||||
register_codec(encode, decode, "ascii6")
|
||||
Reference in New Issue
Block a user