X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=letter_compress.py;h=8d7c8d7259d62be5c30d19432e402eedd06bcffa;hb=a9bdfd8fc9f84b7b2c09a57cd12ba32259e84d1c;hp=9b4cf194c3d97a83c68c47bd0199a65c3a6e1698;hpb=36fea7f15ed17150691b5b3ead75450e575229ef;p=python_utils.git diff --git a/letter_compress.py b/letter_compress.py index 9b4cf19..8d7c8d7 100644 --- a/letter_compress.py +++ b/letter_compress.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 +# © Copyright 2021-2022, Scott Gasch + +"""A simple toy compression helper for lowercase ascii text.""" + import bitstring from collect.bidict import BiDict @@ -32,14 +36,12 @@ def compress(uncompressed: str) -> bytes: """ compressed = bitstring.BitArray() - for (n, letter) in enumerate(uncompressed): + for letter in uncompressed: if 'a' <= letter <= 'z': bits = ord(letter) - ord('a') + 1 # 1..26 else: if letter not in special_characters: - raise Exception( - f'"{uncompressed}" contains uncompressable char="{letter}"' - ) + raise Exception(f'"{uncompressed}" contains uncompressable char="{letter}"') bits = special_characters[letter] compressed.append(f"uint:5={bits}") while len(compressed) % 8 != 0: