projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Add method to get up/down/enter keystrokes.
[python_utils.git]
/
letter_compress.py
diff --git
a/letter_compress.py
b/letter_compress.py
index 9a6f32db43fb09762cca71f79cb268a8862e2452..6cb6b74e87c54928a5aeb7e184114e81ff877b02 100644
(file)
--- a/
letter_compress.py
+++ b/
letter_compress.py
@@
-1,10
+1,14
@@
#!/usr/bin/env python3
#!/usr/bin/env python3
+# © Copyright 2021-2022, Scott Gasch
+
+"""A simple compression helper for lowercase ascii text."""
+
import bitstring
import bitstring
-from collect.bidict import
bid
ict
+from collect.bidict import
BiD
ict
-special_characters =
bid
ict(
+special_characters =
BiD
ict(
{
' ': 27,
'.': 28,
{
' ': 27,
'.': 28,
@@
-32,9
+36,9
@@
def compress(uncompressed: str) -> bytes:
"""
compressed = bitstring.BitArray()
"""
compressed = bitstring.BitArray()
- for
(n, letter) in enumerate(uncompressed)
:
+ for
letter in uncompressed
:
if 'a' <= letter <= 'z':
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}"')
else:
if letter not in special_characters:
raise Exception(f'"{uncompressed}" contains uncompressable char="{letter}"')
@@
-100,4
+104,5
@@
def decompress(kompressed: bytes) -> str:
if __name__ == '__main__':
import doctest
if __name__ == '__main__':
import doctest
+
doctest.testmod()
doctest.testmod()