More cleanup, yey!
[python_utils.git] / letter_compress.py
1 #!/usr/bin/env python3
2
3 """A simple compression helper for lowercase ascii text."""
4
5 import bitstring
6
7 from collect.bidict import BiDict
8
9 special_characters = BiDict(
10     {
11         ' ': 27,
12         '.': 28,
13         ',': 29,
14         "-": 30,
15         '"': 31,
16     }
17 )
18
19
20 def compress(uncompressed: str) -> bytes:
21     """Compress a word sequence into a stream of bytes.  The compressed
22     form will be 5/8th the size of the original.  Words can be lower
23     case letters or special_characters (above).
24
25     >>> import binascii
26     >>> binascii.hexlify(compress('this is a test'))
27     b'a2133da67b0ee859d0'
28
29     >>> binascii.hexlify(compress('scot'))
30     b'98df40'
31
32     >>> binascii.hexlify(compress('scott'))  # Note the last byte
33     b'98df4a00'
34
35     """
36     compressed = bitstring.BitArray()
37     for letter in uncompressed:
38         if 'a' <= letter <= 'z':
39             bits = ord(letter) - ord('a') + 1  # 1..26
40         else:
41             if letter not in special_characters:
42                 raise Exception(f'"{uncompressed}" contains uncompressable char="{letter}"')
43             bits = special_characters[letter]
44         compressed.append(f"uint:5={bits}")
45     while len(compressed) % 8 != 0:
46         compressed.append("uint:1=0")
47     return compressed.bytes
48
49
50 def decompress(kompressed: bytes) -> str:
51     """
52     Decompress a previously compressed stream of bytes back into
53     its original form.
54
55     >>> import binascii
56     >>> decompress(binascii.unhexlify(b'a2133da67b0ee859d0'))
57     'this is a test'
58
59     >>> decompress(binascii.unhexlify(b'98df4a00'))
60     'scott'
61
62     """
63     decompressed = ''
64     compressed = bitstring.BitArray(kompressed)
65
66     # There are compressed messages that legitimately end with the
67     # byte 0x00.  The message "scott" is an example; compressed it is
68     # 0x98df4a00.  It's 5 characters long which means there are 5 x 5
69     # bits of compressed info (25 bits, just over 3 bytes).  The last
70     # (25th) bit in the steam happens to be a zero.  The compress code
71     # padded out the compressed message by adding seven more zeros to
72     # complete the partial 4th byte.  In the 4th byte, however, one
73     # bit is information and seven are padding.
74     #
75     # It's likely that this API's client code may treat a zero byte as
76     # a termination character and not regard it as a legitimate part
77     # of the message.  This is a bug in that client code, to be clear.
78     #
79     # However, it's a bug we can work around:
80     #
81     # Here, I'm appending an extra 0x00 byte to the compressed message
82     # passed in.  If the client code dropped the last 0x00 byte (and,
83     # with it, some of the legitimate message bits) by treating it as
84     # a termination mark, this 0x00 will replace it (and the missing
85     # message bits).  If the client code didn't drop the last 0x00 (or
86     # if the compressed message didn't end in 0x00), adding an extra
87     # 0x00 is a no op because the codepoint 0b00000 is a "stop" message
88     # so we'll ignore the extras.
89     compressed.append("uint:8=0")
90
91     for chunk in compressed.cut(5):
92         chunk = chunk.uint
93         if chunk == 0:
94             break
95         elif 1 <= chunk <= 26:
96             letter = chr(chunk - 1 + ord('a'))
97         else:
98             letter = special_characters.inverse[chunk][0]
99         decompressed += letter
100     return decompressed
101
102
103 if __name__ == '__main__':
104     import doctest
105
106     doctest.testmod()