Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Apr 23, 2024
1 parent 8a9ac63 commit 4f0cfb0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest mypy
- name: Run tests
run: |
pytest tests/
# - name: Check types with mypy
# run: |
# mypy .
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.pyc
3 changes: 3 additions & 0 deletions .pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
pythonpath = .

21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# huffman-py

Teeworlds huffman compression ported to python. Work in progress. Not working yet.
Teeworlds huffman compression ported to python. Work in progress.

This is not a general purpose compression library. It uses the teeworlds frequency table
and is intended to be used for teeworlds networking.

## example usage

```python
from huffman import Huffman

huffman = Huffman()
decompressed = huffman.decompress(bytes([174, 149, 19, 92, 9, 87, 194, 22, 177, 86, 220, 218, 34, 56, 185, 18, 156, 168, 184, 1]))
print(decompressed) # => b'hello world'
```

## similar projects

- teeworlds huffman (ruby) https://github.com/ChillerDragon/huffman-tw
- teeworlds huffman (rust) https://github.com/edg-l/rustyman

10 changes: 2 additions & 8 deletions huffman.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ def construct_tree(self, frequencies: list[int]) -> None:
self.start_node = self.nodes[self.num_nodes-1]
self.setbits_r(self.start_node, 0, 0)

def __init__(self, frequencies: list[int]) -> None:
def __init__(self) -> None:
self.nodes: list[Node] = [Node(0, 0, 0) for _ in range(0, HUFFMAN_MAX_NODES)]
self.decoded_lut: list[Node] = [Node(0, 0, 0) for _ in range(0, HUFFMAN_LUTSIZE)]
self.num_nodes = 0
self.start_node = Node(0, 0, 0)

self.construct_tree(frequencies)
self.construct_tree(FREQUENCY_TABLE)

for i in range(0, HUFFMAN_LUTSIZE):
bits = i
Expand Down Expand Up @@ -162,9 +162,3 @@ def decompress(self, data: bytes) -> bytes:
dst.append(node.symbol)
return bytes(dst)

huffman = Huffman(FREQUENCY_TABLE)
# a = huffman.decompress(bytes([174, 149, 19, 92, 9, 87, 194, 22, 177, 86, 220, 218, 34, 56, 185, 18, 156, 168, 184, 1]))
a = huffman.decompress(bytearray([188, 21, 55, 0]))

print(a)

23 changes: 23 additions & 0 deletions tests/basic_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from huffman import Huffman

def test_huffman():
huffman = Huffman()
compressed = b'\x4a\x42\x88\x4a\x6e\x16\xba\x31\x46\xa2\x84\x9e\xbf\xe2\x06'
decompressed = huffman.decompress(compressed)
expected = b'\x40\x02\x02\x02\x00\x40\x07\x03\x22\x01\x00\x01\x00\x01\x08\x40\x01\x04\x0b'
assert decompressed == expected

def test_huffman_hello_world():
huffman = Huffman()
compressed = bytes([174, 149, 19, 92, 9, 87, 194, 22, 177, 86, 220, 218, 34, 56, 185, 18, 156, 168, 184, 1])
decompressed = huffman.decompress(compressed)
expected = b'hello world'
assert decompressed == expected

def test_huffman_A():
huffman = Huffman()
compressed = bytes([188, 21, 55, 0])
decompressed = huffman.decompress(compressed)
expected = b'A'
assert decompressed == expected

0 comments on commit 4f0cfb0

Please sign in to comment.