Use pathlib to handle paths instead of os.path

- Replaces all instances of os.path objects with pathlib.Path
This commit is contained in:
2023-05-26 11:50:30 -04:00
parent 02bb7c4262
commit e1981c44de
4 changed files with 10 additions and 22 deletions
+1 -1
View File
@@ -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():
+3 -3
View File
@@ -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 <dice_file>')
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])
+3 -13
View File
@@ -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)
+3 -5
View File
@@ -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):