generated from mwc/lab_compression
41 lines
917 B
Python
41 lines
917 B
Python
|
|
import argparse
|
|
import codecs
|
|
import sys
|
|
import os
|
|
|
|
# Make sure Python can see the codec modules
|
|
sys.path.append(os.path.dirname(__file__))
|
|
|
|
# Import codecs so they register
|
|
import alphanumeric
|
|
import ascii7
|
|
import ascii6
|
|
|
|
def evaluate_encoding(encoding, filename):
|
|
print(f"Evaluating encoding {encoding}")
|
|
|
|
with open(filename, "r", encoding="utf8") as f:
|
|
text = f.read()
|
|
|
|
compressed = text.encode(encoding)
|
|
|
|
bits = len(compressed) * 8
|
|
chars = len(text)
|
|
|
|
return (encoding, bits, chars)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("encodings", nargs="+")
|
|
parser.add_argument("--text", default="hello.txt")
|
|
|
|
args = parser.parse_args()
|
|
|
|
results = [evaluate_encoding(e, args.text) for e in args.encodings]
|
|
|
|
print("\nResults:")
|
|
for enc, bits, chars in results:
|
|
print(f"{enc}: {bits} bits for {chars} characters")
|