mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-26 03:29:26 -04:00
Replace print(s) with logging functions
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
@@ -303,7 +304,7 @@ class WordTree:
|
|||||||
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'''
|
||||||
|
|||||||
Reference in New Issue
Block a user