Question: edit ONLY the function to make the test case pass #huffman.py from __future__ import annotations from typing import Optional, TextIO class HuffmanNode: A node in
edit ONLY the function to make the test case pass
#huffman.py
from __future__ import annotations
from typing import Optional, TextIO
class HuffmanNode:
"""A node in a Huffman tree.
Attributes:
char: The character as an integer ASCII value
frequency: The frequency of the character in the file
left: The left Huffman sub-tree
right: The right Huffman sub-tree
"""
def __init__(
self, char: int, frequency: int, left: HuffmanTree, right: HuffmanTree
):
self.char = char
self.frequency = frequency
self.left = left
self.right = right
def __eq__(self, other) -> bool:
"""Return True if and only if self and other are equal."""
if isinstance(other, HuffmanNode):
return (
self.char == other.char
and self.frequency == other.frequency
and self.left == other.left
and self.right == other.right
)
return False
def __lt__(self, other) -> bool:
"""Return True if and only if self < other."""
if isinstance(other, HuffmanNode):
return self.frequency < other.frequency
return False
HuffmanTree = Optional[HuffmanNode]
def huffman_encode(in_file: TextIO, out_file: TextIO) -> None:
"""Encode the data in the input file.
This will write its result to the output file and won't return
anything.
"""
freqs = count_frequencies(in_file)
tree = build_huffman_tree(freqs)
codes = create_codes(tree)
header = create_header(freqs)
out_file.write(header + " ")
in_file.seek(0)
bit_buffer = 0
bit_count = 0
for byte in in_file.read():
code = codes[ord(byte)]
for bit in code:
bit_buffer <<= 1
if bit == '1':
bit_buffer |= 1
bit_count += 1
if bit_count == 8:
#out_file.write(chr(bit_buffer))
out_file.write(str(bytes([bit_buffer])))
#out_file.write(bytes([bit_buffer]).decode('ISO-8859-1'))
#out_file.buffer.write(bytes([bit_buffer]))
#out_file.write(str(bytes([bit_buffer]), 'latin-1'))
bit_buffer = 0
bit_count = 0
if bit_count > 0:
bit_buffer <<= (8 - bit_count)
#out_file.write(chr(bit_buffer))
out_file.write(str(bytes([bit_buffer])))
#out_file.write(bytes([bit_buffer]).decode('ISO-8859-1'))
#out_file.buffer.write(bytes([bit_buffer]))
#out_file.write(str(bytes([bit_buffer]), 'latin-1'))
#huffman_tests.py
def test_huffman_encode_01(self) -> None:
# Create fake files to use for testing
in_file = io.StringIO("abcd abc ab a")
out_file = io.StringIO()
huffman_encode(in_file, out_file)
result = out_file.getvalue()
in_file.close()
out_file.close()
correct_out_text = (
"32 3 97 4 98 3 99 2 100 1 11011011000011011010011010011"
)
self.assertEqual(result, correct_out_text)
in test_huffman_encode_01 self.assertEqual(result, correct_out_text) AssertionError: "32 3 97 4 98 3 99 2 100 1 b'\\xdb'b'\ 'b'\\xa6'b'\\x98'" != '32 3 97 4 98 3 99 2 100 1 11011011000011011010011010011' 32 3 97 4 98 3 99 2 100 1 - b'\xdb'b' 'b'\xa6'b'\x98'+ 11011011000011011010011010011
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
