diff --git a/boggler/__main__.py b/boggler/__main__.py index d370270..3cfe31a 100755 --- a/boggler/__main__.py +++ b/boggler/__main__.py @@ -44,7 +44,7 @@ def main(): print("Invalid MAX_WORD_LENGTH. Please try again with a valid integer.") sys.exit() - boggle_tree = build_full_boggle_tree(boggle_board, args.wordlists) + boggle_tree = build_full_boggle_tree(boggle_board, Path(args.wordlists)) if args.format: match args.format.lower(): diff --git a/boggler/board_randomizer.py b/boggler/board_randomizer.py index b190843..d3e4a8a 100755 --- a/boggler/board_randomizer.py +++ b/boggler/board_randomizer.py @@ -2,11 +2,11 @@ from sys import argv, stderr from random import randint, shuffle from math import sqrt, floor -from os import path +from pathlib import Path def read_dice_file(dice_path: str): '''Return list of die strings from file ignoring all comments (#)''' - with open(path.abspath(dice_path), 'r+', encoding="utf-8") as file: + with open(Path.absolute(dice_path), 'r+', encoding="utf-8") as file: return [line.rstrip().split(',') for line in file.readlines() if line[0] != "#"] def roll_die(die: str): @@ -38,7 +38,7 @@ if __name__ == '__main__': print('Usage: python3 board_randomizer.py ') else: try: - dice = read_dice_file(path.abspath(argv[1])) + dice = read_dice_file(Path(argv[1])) board = get_random_board_csv(dice) for r in range(0, int(floor(sqrt(len(dice))))): print(board[r]) diff --git a/boggler/boggler_utils.py b/boggler/boggler_utils.py index 92464dd..0d38018 100755 --- a/boggler/boggler_utils.py +++ b/boggler/boggler_utils.py @@ -1,7 +1,6 @@ '''Boggler Utils''' from __future__ import annotations -#from pathlib import Path -from os import path +from pathlib import Path from multiprocessing import Pool import logging as log import functools @@ -368,7 +367,7 @@ def build_boggle_tree(args): sub_tree = WordTree(alphabet, WordNode(cell.letters, False, board_pos=cell.pos)) return dict_tree.build_boggle_tree(board, cell, sub_tree) -def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str, WordTree]: +def build_full_boggle_tree(board: BoggleBoard, wordlist_path: Path) -> dict[str, WordTree]: '''Return dictionary of WordTree(s) for every letter on a BoggleBoard''' alphabet = sorted(set([cell.letters for cell in board.board.values()])) board_tree = {} @@ -386,7 +385,7 @@ def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str, filename = "words_" + letters[0] + ".txt" try: - wordlist = read_wordlist(path.join(path.abspath(wordlist_path), filename)) + wordlist = read_wordlist(Path(wordlist_path, filename)) index[letters] = wordlist log.info(">> %s: %s", letters, filename) except FileNotFoundError: @@ -412,15 +411,6 @@ def read_boggle_file(file): with open(file, 'r', encoding='utf-8') as file: return [x.rstrip().split(',') for x in file.readlines()] -def write_wordlist(filename: Path, filetype: str = "txt", sort: bool = False): - match filetype: - case "txt": - pass - case "json": - pass - case _: - pass - def find_paths_by_word(board_letters, dictionary_path, max_len): '''Return list of paths by word''' boggle_board = BoggleBoard(board_letters, max_len) diff --git a/tests/test_solve_4x4_board.py b/tests/test_solve_4x4_board.py index 8be470b..5710665 100755 --- a/tests/test_solve_4x4_board.py +++ b/tests/test_solve_4x4_board.py @@ -1,11 +1,9 @@ -import unittest -from functools import reduce from itertools import chain -from os import path +from pathlib import Path import pytest from boggler.boggler_utils import BoggleBoard, build_full_boggle_tree -WORDLISTS_DIR = path.abspath("./boggler/wordlists/dwyl/") +WORDLISTS_DIR = Path("./boggler/wordlists/dwyl/").absolute() @pytest.fixture def board_4x4(): @@ -122,7 +120,7 @@ def board_4(board_4x4): @pytest.fixture def tree_4(board_4): - return build_full_boggle_tree(board_4, WORDLISTS_DIR) + return build_full_boggle_tree(board_4, Path(WORDLISTS_DIR)) @pytest.fixture def found_words_4(tree_4):