Skip to content

Commit 8eea4b8

Browse files
committed
Pass doctests by adding function to determine which path should be used
1 parent f8122c5 commit 8eea4b8

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

compression/huffman.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def parse_string(string: str) -> list[Letter]:
5353
def parse_file(file_path: str) -> list[Letter]:
5454
"""
5555
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"
5758
>>> out1 = parse_file(test_file_path_in1)
5859
>>> out1
5960
[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]:
111112
"""
112113
Recursively traverse the Huffman Tree to set each
113114
Letter's bitstring dictionary, and return the list of Letters
115+
>>> PREFIX = _get_test_data_path_prefix()
114116
>>> root_in1 = build_tree(parse_string("goose"))
115117
>>> out1 = traverse_tree(root_in1, "")
116118
>>> out1
@@ -123,7 +125,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str = "") -> list[Letter]:
123125
'10'
124126
>>> out1[3].bitstring['o']
125127
'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"))
127129
>>> out2 = traverse_tree(root_in2)
128130
>>> out2
129131
[.:3, i:2, t:2, T:1, h:1, a:1, e:1, s:3, :3]
@@ -166,12 +168,14 @@ def huffman(
166168
Parse the file, Huffman Code it and print the result
167169
to the given output_file, with each bitstring
168170
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="_")
170173
Huffman Coding of text_data/text_original.txt:
171174
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:
175179
... print(output_file_out1.read())
176180
Huffman Coding of text_data/text_original.txt:
177181
1000100101011011101011011110101110111011110011000000
@@ -189,9 +193,23 @@ def huffman(
189193
print(file=output_file)
190194

191195

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+
192210
if __name__ == "__main__":
193211
if len(sys.argv) < 2:
194-
# if no file path given, test the module
212+
# if no file path given, test module
195213
doctest.testmod()
196214
else:
197215
# pass the file path to the huffman function

0 commit comments

Comments
 (0)