@@ -53,7 +53,8 @@ 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
- >>> test_file_path_in1 = "text_data/text_original.txt"
56
+ >>> PREFIX = _get_test_data_path_prefix()
57
+ >>> test_file_path_in1 = f"{PREFIX}text_data/text_original.txt"
57
58
>>> out1 = parse_file(test_file_path_in1)
58
59
>>> out1
59
60
[T:1, h:1, a:1, e:1, i:2, t:2, s:3, :3, .:3]
@@ -111,6 +112,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str = "") -> list[Letter]:
111
112
"""
112
113
Recursively traverse the Huffman Tree to set each
113
114
Letter's bitstring dictionary, and return the list of Letters
115
+ >>> PREFIX = _get_test_data_path_prefix()
114
116
>>> root_in1 = build_tree(parse_string("goose"))
115
117
>>> out1 = traverse_tree(root_in1, "")
116
118
>>> out1
@@ -123,7 +125,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str = "") -> list[Letter]:
123
125
'10'
124
126
>>> out1[3].bitstring['o']
125
127
'11'
126
- >>> root_in2 = build_tree(parse_file(" text_data/text_original.txt"))
128
+ >>> root_in2 = build_tree(parse_file(f"{PREFIX} text_data/text_original.txt"))
127
129
>>> out2 = traverse_tree(root_in2)
128
130
>>> out2
129
131
[.:3, i:2, t:2, T:1, h:1, a:1, e:1, s:3, :3]
@@ -166,12 +168,14 @@ def huffman(
166
168
Parse the file, Huffman Code it and print the result
167
169
to the given output_file, with each bitstring
168
170
separated by sep parameter
169
- >>> huffman("text_data/text_original.txt", sep="_")
171
+ >>> PREFIX = _get_test_data_path_prefix()
172
+ >>> huffman(f"{PREFIX}text_data/text_original.txt", sep="_")
170
173
Huffman Coding of text_data/text_original.txt:
171
174
1000_1001_010_110_111_010_110_111_1010_111_011_1011_110_011_00_00_00_
172
- >>> with open("text_data/text_huffman.txt", "w") as output_file_in1:
173
- ... huffman("text_data/text_original.txt", sep="", output_file=output_file_in1)
174
- >>> with open("text_data/text_huffman.txt") as output_file_out1:
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:
175
179
... print(output_file_out1.read())
176
180
Huffman Coding of text_data/text_original.txt:
177
181
1000100101011011101011011110101110111011110011000000
@@ -189,9 +193,23 @@ def huffman(
189
193
print (file = output_file )
190
194
191
195
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
+
192
210
if __name__ == "__main__" :
193
211
if len (sys .argv ) < 2 :
194
- # if no file path given, test the module
212
+ # if no file path given, test module
195
213
doctest .testmod ()
196
214
else :
197
215
# pass the file path to the huffman function
0 commit comments