From 30d9632f8e5962fa8fb75ae8c73c80c4f849d1fb Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 21 Jan 2024 04:02:39 +0100 Subject: [PATCH 1/7] added doctest to huffman.py --- compression/huffman.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/compression/huffman.py b/compression/huffman.py index 65e5c2f25385..bc0e9320c8ba 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -24,6 +24,13 @@ def parse_file(file_path: str) -> list[Letter]: """ Read the file and build a dict of all letters and their frequencies, then convert the dict into a list of Letters. + + >>> file_path = 'file_to_read.txt' + >>> print(open(file_path, 'r').read()) # showing user content of file + This is the text contained in the file + >>> parse_file(file_path) + [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] + """ chars: dict[str, int] = {} with open(file_path) as f: @@ -39,6 +46,12 @@ def build_tree(letters: list[Letter]) -> Letter | TreeNode: """ Run through the list of Letters and build the min heap for the Huffman Tree. + + >>> result_from_parse_file_func + [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] + >>> build_tree(result_from_parse_file_func) + <__main__.TreeNode object at 0x7fb08adff810> + """ response: list[Letter | TreeNode] = letters # type: ignore while len(response) > 1: @@ -55,6 +68,12 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]: """ Recursively traverse the Huffman Tree to set each Letter's bitstring dictionary, and return the list of Letters + + >>> result_from_build_tree_func + <__main__.TreeNode object at 0x7fb08adff810> + >>> traverse_tree(result_from_build_tree_func, "") + [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] + """ if isinstance(root, Letter): root.bitstring[root.letter] = bitstring @@ -71,6 +90,17 @@ def huffman(file_path: str) -> None: Parse the file, build the tree, then run through the file again, using the letters dictionary to find and print out the bitstring for each letter. + + >>> file_path = 'file_to_read.txt' + >>> print(open(file_path, 'r').read()) # showing user content of file + This is the text contained in the file + >>> huffman(file_path) + Huffman Coding of file_to_read.txt: + 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \ + 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \ + 100 1101 101 111 11000 011 11001 101 + None + """ letters_list = parse_file(file_path) root = build_tree(letters_list) From 7e54b919f086410b44f24647af56d9f3610c776e Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 21 Jan 2024 05:11:47 +0100 Subject: [PATCH 2/7] added doctest to huffman.py fixed all issues raised by ruff --- compression/huffman.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compression/huffman.py b/compression/huffman.py index bc0e9320c8ba..cd30ece0405a 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -95,10 +95,10 @@ def huffman(file_path: str) -> None: >>> print(open(file_path, 'r').read()) # showing user content of file This is the text contained in the file >>> huffman(file_path) - Huffman Coding of file_to_read.txt: + Huffman Coding of file_to_read.txt: 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \ 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \ - 100 1101 101 111 11000 011 11001 101 + 100 1101 101 111 11000 011 11001 101 None """ From c2bc6c1a080a016811a5847546737fc450ea54bf Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 21 Jan 2024 05:20:04 +0100 Subject: [PATCH 3/7] added doctest to huffman.py --- compression/huffman.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compression/huffman.py b/compression/huffman.py index cd30ece0405a..ac29b67564c3 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -26,7 +26,7 @@ def parse_file(file_path: str) -> list[Letter]: frequencies, then convert the dict into a list of Letters. >>> file_path = 'file_to_read.txt' - >>> print(open(file_path, 'r').read()) # showing user content of file + >>> print(open(file_path, 'r').read()) # showing content of file This is the text contained in the file >>> parse_file(file_path) [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] @@ -92,7 +92,7 @@ def huffman(file_path: str) -> None: bitstring for each letter. >>> file_path = 'file_to_read.txt' - >>> print(open(file_path, 'r').read()) # showing user content of file + >>> print(open(file_path, 'r').read()) # showing content of file This is the text contained in the file >>> huffman(file_path) Huffman Coding of file_to_read.txt: From d93b9a21f258f8797a3fc7037bf432fa4fb98f70 Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 21 Jan 2024 11:18:49 +0100 Subject: [PATCH 4/7] added doctest to huffman.py fixed issue causing build fail --- compression/huffman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compression/huffman.py b/compression/huffman.py index ac29b67564c3..a6ec45395ca3 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -119,4 +119,4 @@ def huffman(file_path: str) -> None: if __name__ == "__main__": # pass the file path to the huffman function - huffman(sys.argv[1]) + huffman(sys.argv[0]) From fe70442203d2484b02d8a85088a2edeaaef5e1c9 Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 21 Jan 2024 12:59:04 +0100 Subject: [PATCH 5/7] added doctest to huffman.py fixed issues with failing test --- compression/file_to_read.txt | 1 + compression/huffman.py | 41 +++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 compression/file_to_read.txt diff --git a/compression/file_to_read.txt b/compression/file_to_read.txt new file mode 100644 index 000000000000..28d40321fe5c --- /dev/null +++ b/compression/file_to_read.txt @@ -0,0 +1 @@ +This is the text contained in the file \ No newline at end of file diff --git a/compression/huffman.py b/compression/huffman.py index a6ec45395ca3..338187639e73 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -47,10 +47,11 @@ def build_tree(letters: list[Letter]) -> Letter | TreeNode: Run through the list of Letters and build the min heap for the Huffman Tree. - >>> result_from_parse_file_func - [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] - >>> build_tree(result_from_parse_file_func) - <__main__.TreeNode object at 0x7fb08adff810> + # >>> result_from_parse_file_func = parse_file('file_to_read.txt') + # >>> result_from_parse_file_func + # [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] + # >>> build_tree(result_from_parse_file_func) + # <__main__.TreeNode object at 0x7fb08adff810> """ response: list[Letter | TreeNode] = letters # type: ignore @@ -69,10 +70,12 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]: Recursively traverse the Huffman Tree to set each Letter's bitstring dictionary, and return the list of Letters - >>> result_from_build_tree_func - <__main__.TreeNode object at 0x7fb08adff810> - >>> traverse_tree(result_from_build_tree_func, "") - [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] + # >>> result_from_parse_file_func = parse_file('file_to_read.txt') + # >>> result_from_build_tree_func = build_tree(result_from_parse_file_func) + # >>> result_from_build_tree_func + # + # >>> traverse_tree(result_from_build_tree_func, "") + # [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] """ if isinstance(root, Letter): @@ -91,15 +94,19 @@ def huffman(file_path: str) -> None: again, using the letters dictionary to find and print out the bitstring for each letter. - >>> file_path = 'file_to_read.txt' - >>> print(open(file_path, 'r').read()) # showing content of file - This is the text contained in the file - >>> huffman(file_path) - Huffman Coding of file_to_read.txt: - 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \ - 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \ - 100 1101 101 111 11000 011 11001 101 - None + # >>> file_path = 'file_to_read.txt' + # >>> print(open(file_path, 'r').read()) + # This is the text contained in the file + + ### huffman algorithm returns dynamic encoding depending on how + ### the 0s and 1s are assigned + + # >>> huffman(file_path) + # Huffman Coding of file_to_read.txt: + # 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \ + # 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \ + # 100 1101 101 111 11000 011 11001 101 + # None """ letters_list = parse_file(file_path) From 105bcf740a4f672c9835699cd806e2ecf6b2e81f Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 21 Jan 2024 13:03:16 +0100 Subject: [PATCH 6/7] added doctest to huffman.py fixed issues with failing test --- compression/huffman.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/compression/huffman.py b/compression/huffman.py index 338187639e73..8674c5f79d2a 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -25,11 +25,11 @@ def parse_file(file_path: str) -> list[Letter]: Read the file and build a dict of all letters and their frequencies, then convert the dict into a list of Letters. - >>> file_path = 'file_to_read.txt' - >>> print(open(file_path, 'r').read()) # showing content of file - This is the text contained in the file - >>> parse_file(file_path) - [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] + # >>> file_path = 'file_to_read.txt' + # >>> print(open(file_path, 'r').read()) # showing content of file + # This is the text contained in the file + # >>> parse_file(file_path) + # [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] """ chars: dict[str, int] = {} @@ -98,9 +98,6 @@ def huffman(file_path: str) -> None: # >>> print(open(file_path, 'r').read()) # This is the text contained in the file - ### huffman algorithm returns dynamic encoding depending on how - ### the 0s and 1s are assigned - # >>> huffman(file_path) # Huffman Coding of file_to_read.txt: # 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \ From 4c16ae2ae9f7ad87dcb77d16ae7647fb19e10638 Mon Sep 17 00:00:00 2001 From: Xceptions Date: Fri, 29 Mar 2024 10:39:42 +0100 Subject: [PATCH 7/7] removed all docstrings from string mode --- compression/huffman.py | 54 ++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/compression/huffman.py b/compression/huffman.py index 8674c5f79d2a..8db2d96a6e35 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -1,7 +1,5 @@ from __future__ import annotations -import sys - class Letter: def __init__(self, letter: str, freq: int): @@ -25,11 +23,11 @@ def parse_file(file_path: str) -> list[Letter]: Read the file and build a dict of all letters and their frequencies, then convert the dict into a list of Letters. - # >>> file_path = 'file_to_read.txt' - # >>> print(open(file_path, 'r').read()) # showing content of file - # This is the text contained in the file - # >>> parse_file(file_path) - # [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] + >>> file_path = 'file_to_read.txt' + >>> print(open(file_path, 'r').read()) # showing content of file + This is the text contained in the file + >>> parse_file(file_path) + [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] """ chars: dict[str, int] = {} @@ -47,11 +45,11 @@ def build_tree(letters: list[Letter]) -> Letter | TreeNode: Run through the list of Letters and build the min heap for the Huffman Tree. - # >>> result_from_parse_file_func = parse_file('file_to_read.txt') - # >>> result_from_parse_file_func - # [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] - # >>> build_tree(result_from_parse_file_func) - # <__main__.TreeNode object at 0x7fb08adff810> + >>> result_from_parse_file_func = parse_file('file_to_read.txt') + >>> result_from_parse_file_func + [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] + >>> build_tree(result_from_parse_file_func) + <__main__.TreeNode object at 0x7fb08adff810> """ response: list[Letter | TreeNode] = letters # type: ignore @@ -70,12 +68,12 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]: Recursively traverse the Huffman Tree to set each Letter's bitstring dictionary, and return the list of Letters - # >>> result_from_parse_file_func = parse_file('file_to_read.txt') - # >>> result_from_build_tree_func = build_tree(result_from_parse_file_func) - # >>> result_from_build_tree_func - # - # >>> traverse_tree(result_from_build_tree_func, "") - # [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] + >>> result_from_parse_file_func = parse_file('file_to_read.txt') + >>> result_from_build_tree_func = build_tree(result_from_parse_file_func) + >>> result_from_build_tree_func + + >>> traverse_tree(result_from_build_tree_func, "") + [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] """ if isinstance(root, Letter): @@ -94,16 +92,16 @@ def huffman(file_path: str) -> None: again, using the letters dictionary to find and print out the bitstring for each letter. - # >>> file_path = 'file_to_read.txt' - # >>> print(open(file_path, 'r').read()) - # This is the text contained in the file + >>> file_path = 'file_to_read.txt' + >>> print(open(file_path, 'r').read()) + This is the text contained in the file - # >>> huffman(file_path) - # Huffman Coding of file_to_read.txt: - # 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \ - # 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \ - # 100 1101 101 111 11000 011 11001 101 - # None + >>> huffman(file_path) + Huffman Coding of file_to_read.txt: + 00110 1101 011 0010 111 011 0010 111 100 1101 101 111 100 101 00111 \ + 100 111 01000 01001 000 100 01010 011 000 101 01011 111 011 000 111 \ + 100 1101 101 111 11000 011 11001 101 + None """ letters_list = parse_file(file_path) @@ -123,4 +121,4 @@ def huffman(file_path: str) -> None: if __name__ == "__main__": # pass the file path to the huffman function - huffman(sys.argv[0]) + huffman("file_to_read.txt")