Replace print(s) with logging functions

This commit is contained in:
2023-03-22 16:25:37 -04:00
parent 8b9dc89420
commit 6d3a58595c
+13 -12
View File
@@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from os import path from os import path
from multiprocessing import Pool from multiprocessing import Pool
from logging import info, debug, warn, error
import functools import functools
import operator import operator
@@ -297,13 +298,13 @@ class WordTree:
# Check that letter group is shorter than and matches in the word remainder # Check that letter group is shorter than and matches in the word remainder
if len(alpha) < word_len - i and alpha == word[i+1:i+1+len(alpha)]: if len(alpha) < word_len - i and alpha == word[i+1:i+1+len(alpha)]:
if alpha not in curr_node.children: if alpha not in curr_node.children:
self.insert_node(alpha, curr_node) self.insert_node(alpha, curr_node )
curr_node = curr_node.children[alpha] curr_node = curr_node.children[alpha]
else: # node already exist else: # node already exist
curr_node = curr_node.children[alpha] curr_node = curr_node.children[alpha]
skip_cnt = len(alpha) - 1 skip_cnt = len(alpha) - 1
#print(f"LONG GROUP: {alpha}; {word}") debug(f"LONG GROUP: {alpha}; {word}")
else: else:
return False return False
@@ -337,7 +338,7 @@ class WordTree:
''' '''
if word_len > self.max_word_len or word_len >= board.max_word_len: if word_len > self.max_word_len or word_len >= board.max_word_len:
#print(f"MAX DEPTH REACHED! Depth = {depth}") debug(f"MAX WORD LENGTH REACHED! len = {word_len}")
subtree.active_node = subtree.active_node.parent subtree.active_node = subtree.active_node.parent
self.active_node = self.active_node.parent self.active_node = self.active_node.parent
return return
@@ -346,14 +347,14 @@ class WordTree:
if self.active_node.is_word and len(self.active_node.children) == 0: if self.active_node.is_word and len(self.active_node.children) == 0:
word_path = subtree.active_node.path[::-1] word_path = subtree.active_node.path[::-1]
subtree.word_paths.append((subtree.active_node.get_word(board), word_path)) subtree.word_paths.append((subtree.active_node.get_word(board), word_path))
#print("1: WORD FOUND:", "".join([board.board[x].letters for x in word_path]), word_path) debug("1: WORD FOUND:", "".join([board.board[x].letters for x in word_path]), word_path)
self.active_node = self.active_node.parent self.active_node = self.active_node.parent
subtree.active_node = subtree.active_node.parent subtree.active_node = subtree.active_node.parent
return return
elif self.active_node.is_word: elif self.active_node.is_word:
word_path = subtree.active_node.path[::-1] word_path = subtree.active_node.path[::-1]
subtree.word_paths.append((subtree.active_node.get_word(board), word_path)) subtree.word_paths.append((subtree.active_node.get_word(board), word_path))
#print("2: WORD FOUND:", "".join([board.board[x].letters for x in word_path]), word_path) debug("2: WORD FOUND:", "".join([board.board[x].letters for x in word_path]), word_path)
# Branch for each adjacent board cell # Branch for each adjacent board cell
for cell in board_cell.adjacent_cells: for cell in board_cell.adjacent_cells:
@@ -383,7 +384,7 @@ def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str,
board_tree = {} board_tree = {}
index = {} index = {}
print("Reading in wordlists...") info("Reading in wordlists...")
for letters in alphabet: for letters in alphabet:
if letters == "": if letters == "":
# Skip wordlist read for blocks with empty string # Skip wordlist read for blocks with empty string
@@ -397,16 +398,16 @@ def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str,
try: try:
wordlist = read_wordlist(path.join(path.abspath(wordlist_path), filename)) wordlist = read_wordlist(path.join(path.abspath(wordlist_path), filename))
index[letters] = wordlist index[letters] = wordlist
print(f">> {letters}: {filename}") info(f">> {letters}: {filename}")
except FileNotFoundError: except FileNotFoundError:
print(f">> {letters}: -- Skipping -- no wordlist found for {letters}") info(f">> {letters}: -- Skipping -- no wordlist found for {letters}")
index[letters] = {} index[letters] = {}
print("Generating WordTrees...") info("Generating WordTrees...")
params = [ [alphabet, board, cell, index[cell.letters] ] for cell in board.board.values()] params = [ [alphabet, board, cell, index[cell.letters] ] for cell in board.board.values()]
with Pool(processes=len(board.board)) as pool: with Pool(processes=len(board.board)) as pool:
for i, res in enumerate(pool.map(build_boggle_tree, params)): for i, res in enumerate(pool.map(build_boggle_tree, params)):
print(f">> {params[i][2]}") info(f">> {params[i][2]}")
board_tree[params[i][2].pos] = res board_tree[params[i][2].pos] = res
return board_tree return board_tree
@@ -414,7 +415,7 @@ def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str,
def read_wordlist(file): def read_wordlist(file):
'''Return dictionary of words with associated word count (1 by default)''' '''Return dictionary of words with associated word count (1 by default)'''
with open(file, 'r', encoding='utf-8') as file: with open(file, 'r', encoding='utf-8') as file:
return {k:1 for k in file.read().split()} return file.read().split()
def read_boggle_file(file): def read_boggle_file(file):
'''Return list of rows from Boggle board csv file''' '''Return list of rows from Boggle board csv file'''
@@ -426,4 +427,4 @@ def find_paths_by_word(board_letters, dictionary_path, max_len):
boggle_board = BoggleBoard(board_letters, max_len) boggle_board = BoggleBoard(board_letters, max_len)
boggle_tree = build_full_boggle_tree(boggle_board, dictionary_path) boggle_tree = build_full_boggle_tree(boggle_board, dictionary_path)
paths_by_word = functools.reduce(operator.iconcat, [x.word_paths for x in boggle_tree.values()], []) paths_by_word = functools.reduce(operator.iconcat, [x.word_paths for x in boggle_tree.values()], [])
return paths_by_word return paths_by_word