X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=letter_compress.py;h=9b4cf194c3d97a83c68c47bd0199a65c3a6e1698;hb=36fea7f15ed17150691b5b3ead75450e575229ef;hp=d5a4d60ef06483ee07bd2761e10a5ee9bba7385e;hpb=d82c8377ce394cad812dc0d53829f7465b3f3f4e;p=python_utils.git diff --git a/letter_compress.py b/letter_compress.py index d5a4d60..9b4cf19 100644 --- a/letter_compress.py +++ b/letter_compress.py @@ -2,9 +2,9 @@ import bitstring -from collect.bidict import bidict +from collect.bidict import BiDict -special_characters = bidict( +special_characters = BiDict( { ' ': 27, '.': 28, @@ -34,10 +34,12 @@ def compress(uncompressed: str) -> bytes: compressed = bitstring.BitArray() for (n, letter) in enumerate(uncompressed): if 'a' <= letter <= 'z': - bits = ord(letter) - ord('a') + 1 # 1..26 + 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: @@ -70,12 +72,12 @@ def decompress(kompressed: bytes) -> str: # complete the partial 4th byte. In the 4th byte, however, one # bit is information and seven are padding. # - # It's likely that this APIs client code will treat a zero byte as - # a termination character and not regard it as part of the - # message. This is a bug in the client code. + # It's likely that this API's client code may treat a zero byte as + # a termination character and not regard it as a legitimate part + # of the message. This is a bug in that client code, to be clear. # # However, it's a bug we can work around: - + # # Here, I'm appending an extra 0x00 byte to the compressed message # passed in. If the client code dropped the last 0x00 byte (and, # with it, some of the legitimate message bits) by treating it as @@ -100,4 +102,5 @@ def decompress(kompressed: bytes) -> str: if __name__ == '__main__': import doctest + doctest.testmod()