generated from mwc/lab_compression
but with differnt characters included. This reads/encodes with 5 bits instead of the original 7.
47 lines
1.4 KiB
Python
Executable File
47 lines
1.4 KiB
Python
Executable File
from easybits import Bits
|
|
|
|
import codecs
|
|
|
|
def register_codec(encode, decode, name):
|
|
def encode_wrapper(text):
|
|
return encode(text), len(text)
|
|
|
|
def decode_wrapper(data):
|
|
return decode(data), len(data)
|
|
|
|
def search_for_codec(query):
|
|
if query == name:
|
|
return codecs.CodecInfo(encode_wrapper, decode_wrapper, name=name)
|
|
|
|
codecs.register(search_for_codec)
|
|
|
|
characters=['A','B','C','D','E','F','G','H','I','J','K','L','M', 'N', 'O','P','Q','R','S','T','U','V','W','X','Y','Z',' ','.',',','?','!']
|
|
|
|
|
|
def encode(text):
|
|
"""This encoder consists of the 26 uppercase letters and select punctuation.
|
|
Characters that are not defined are removed, which makes the encoding less accurate than ascii7.
|
|
There are 5 bits for each byte, so the function ascii5 compresses text into 5/8 of its original size."""
|
|
result= Bits()
|
|
text = text.upper()
|
|
for character in text:
|
|
if character in characters:
|
|
index = characters.index(character)
|
|
b=Bits(bin(index)[2:].zfill(5))
|
|
result = result.concat(b)
|
|
return result.bytes
|
|
|
|
def decode(data):
|
|
"""This function reads 5 bits and returns the text to match."""
|
|
bits=Bits(bytes(data))
|
|
text = ''
|
|
for i in range(0,len(bits),5):
|
|
s=bits[i:i+5]
|
|
index=int(s.bin,2)
|
|
text+= characters[index]
|
|
return text
|
|
|
|
|
|
register_codec(encode, decode, "ascii5")
|
|
|