Skip to content

Commit 4c16ae2

Browse files
committed
removed all docstrings from string mode
1 parent 105bcf7 commit 4c16ae2

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

compression/huffman.py

+26-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import sys
4-
53

64
class Letter:
75
def __init__(self, letter: str, freq: int):
@@ -25,11 +23,11 @@ def parse_file(file_path: str) -> list[Letter]:
2523
Read the file and build a dict of all letters and their
2624
frequencies, then convert the dict into a list of Letters.
2725
28-
# >>> file_path = 'file_to_read.txt'
29-
# >>> print(open(file_path, 'r').read()) # showing content of file
30-
# This is the text contained in the file
31-
# >>> parse_file(file_path)
32-
# [T:1, x:1, c:1, o:1, a:1, d:1, f:1, l:1, s:2, h:3, n:3, i:5, t:5, e:5, :7]
26+
>>> file_path = 'file_to_read.txt'
27+
>>> print(open(file_path, 'r').read()) # showing content of file
28+
This is the text contained in the file
29+
>>> parse_file(file_path)
30+
[T:1, x:1, c:1, o:1, a:1, d:1, f:1, l:1, s:2, h:3, n:3, i:5, t:5, e:5, :7]
3331
3432
"""
3533
chars: dict[str, int] = {}
@@ -47,11 +45,11 @@ def build_tree(letters: list[Letter]) -> Letter | TreeNode:
4745
Run through the list of Letters and build the min heap
4846
for the Huffman Tree.
4947
50-
# >>> result_from_parse_file_func = parse_file('file_to_read.txt')
51-
# >>> result_from_parse_file_func
52-
# [T:1, x:1, c:1, o:1, a:1, d:1, f:1, l:1, s:2, h:3, n:3, i:5, t:5, e:5, :7]
53-
# >>> build_tree(result_from_parse_file_func)
54-
# <__main__.TreeNode object at 0x7fb08adff810>
48+
>>> result_from_parse_file_func = parse_file('file_to_read.txt')
49+
>>> result_from_parse_file_func
50+
[T:1, x:1, c:1, o:1, a:1, d:1, f:1, l:1, s:2, h:3, n:3, i:5, t:5, e:5, :7]
51+
>>> build_tree(result_from_parse_file_func)
52+
<__main__.TreeNode object at 0x7fb08adff810>
5553
5654
"""
5755
response: list[Letter | TreeNode] = letters # type: ignore
@@ -70,12 +68,12 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
7068
Recursively traverse the Huffman Tree to set each
7169
Letter's bitstring dictionary, and return the list of Letters
7270
73-
# >>> result_from_parse_file_func = parse_file('file_to_read.txt')
74-
# >>> result_from_build_tree_func = build_tree(result_from_parse_file_func)
75-
# >>> result_from_build_tree_func
76-
# <huffman.TreeNode object at 0x10c0cf8c0>
77-
# >>> traverse_tree(result_from_build_tree_func, "")
78-
# [n:3, s:2, T:1, x:1, c:1, o:1, a:1, d:1, i:5, t:5, e:5, f:1, l:1, h:3, :7]
71+
>>> result_from_parse_file_func = parse_file('file_to_read.txt')
72+
>>> result_from_build_tree_func = build_tree(result_from_parse_file_func)
73+
>>> result_from_build_tree_func
74+
<huffman.TreeNode object at 0x10c0cf8c0>
75+
>>> traverse_tree(result_from_build_tree_func, "")
76+
[n:3, s:2, T:1, x:1, c:1, o:1, a:1, d:1, i:5, t:5, e:5, f:1, l:1, h:3, :7]
7977
8078
"""
8179
if isinstance(root, Letter):
@@ -94,16 +92,16 @@ def huffman(file_path: str) -> None:
9492
again, using the letters dictionary to find and print out the
9593
bitstring for each letter.
9694
97-
# >>> file_path = 'file_to_read.txt'
98-
# >>> print(open(file_path, 'r').read())
99-
# This is the text contained in the file
95+
>>> file_path = 'file_to_read.txt'
96+
>>> print(open(file_path, 'r').read())
97+
This is the text contained in the file
10098
101-
# >>> huffman(file_path)
102-
# Huffman Coding of file_to_read.txt:
103-
# 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \
104-
# 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \
105-
# 100 1101 101 111 11000 011 11001 101
106-
# None
99+
>>> huffman(file_path)
100+
Huffman Coding of file_to_read.txt:
101+
00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \
102+
100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \
103+
100 1101 101 111 11000 011 11001 101
104+
None
107105
108106
"""
109107
letters_list = parse_file(file_path)
@@ -123,4 +121,4 @@ def huffman(file_path: str) -> None:
123121

124122
if __name__ == "__main__":
125123
# pass the file path to the huffman function
126-
huffman(sys.argv[0])
124+
huffman("file_to_read.txt")

0 commit comments

Comments
 (0)