@@ -53,11 +53,6 @@ def parse_string(string: str) -> list[Letter]:
53
53
def parse_file (file_path : str ) -> list [Letter ]:
54
54
"""
55
55
Read file and return a list of Letter objects storing frequency
56
- >>> PREFIX = _get_test_data_path_prefix()
57
- >>> test_file_path_in1 = f"{PREFIX}text_data/text_original.txt"
58
- >>> out1 = parse_file(test_file_path_in1)
59
- >>> out1
60
- [T:1, h:1, a:1, e:1, i:2, t:2, s:3, :3, .:3]
61
56
"""
62
57
chars : dict [str , Letter ] = {}
63
58
with open (file_path ) as input_file :
@@ -112,7 +107,6 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str = "") -> list[Letter]:
112
107
"""
113
108
Recursively traverse the Huffman Tree to set each
114
109
Letter's bitstring dictionary, and return the list of Letters
115
- >>> PREFIX = _get_test_data_path_prefix()
116
110
>>> root_in1 = build_tree(parse_string("goose"))
117
111
>>> out1 = traverse_tree(root_in1, "")
118
112
>>> out1
@@ -125,7 +119,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str = "") -> list[Letter]:
125
119
'10'
126
120
>>> out1[3].bitstring['o']
127
121
'11'
128
- >>> root_in2 = build_tree(parse_file(f"{PREFIX}text_data/text_original.txt "))
122
+ >>> root_in2 = build_tree(parse_string("This is a test... "))
129
123
>>> out2 = traverse_tree(root_in2)
130
124
>>> out2
131
125
[.:3, i:2, t:2, T:1, h:1, a:1, e:1, s:3, :3]
@@ -168,18 +162,6 @@ def huffman(
168
162
Parse the file, Huffman Code it and print the result
169
163
to the given output_file, with each bitstring
170
164
separated by sep parameter
171
- >>> PREFIX = _get_test_data_path_prefix()
172
- >>> huffman(f"{PREFIX}text_data/text_original.txt", sep="_")
173
- Huffman Coding of text_data/text_original.txt:
174
- 1000_1001_010_110_111_010_110_111_1010_111_011_1011_110_011_00_00_00_
175
- >>> with open(f"{PREFIX}text_data/text_huffman.txt", "w") as output_file_in1:
176
- ... huffman(f"{PREFIX}text_data/text_original.txt", sep="",
177
- ... output_file=output_file_in1)
178
- >>> with open(f"{PREFIX}text_data/text_huffman.txt") as output_file_out1:
179
- ... print(output_file_out1.read())
180
- Huffman Coding of text_data/text_original.txt:
181
- 1000100101011011101011011110101110111011110011000000
182
- <BLANKLINE>
183
165
"""
184
166
letters_list = parse_file (file_path )
185
167
root = build_tree (letters_list )
@@ -193,20 +175,6 @@ def huffman(
193
175
print (file = output_file )
194
176
195
177
196
- def _get_test_data_path_prefix ():
197
- try :
198
- with open ("compression/text_data/text_original.txt" ):
199
- pass
200
- return "copmression/"
201
- except FileNotFoundError :
202
- try :
203
- with open ("text_data/text_original.txt" ):
204
- pass
205
- return ""
206
- except FileNotFoundError :
207
- raise FileNotFoundError ("Could not locate testing data" )
208
-
209
-
210
178
if __name__ == "__main__" :
211
179
if len (sys .argv ) < 2 :
212
180
# if no file path given, test module
0 commit comments