From bdf8b2a3982758e9e3956721240f4229ef98b927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Y=C4=B1lmaz=20=C3=87ABUK?= Date: Sun, 13 Aug 2023 20:16:17 +0300 Subject: [PATCH] refactor: improve WordList class and methods - Refactor the WordList class to include informative docstrings and attributes. - Modify the open_file method to provide a clearer description in its docstring. - Enhance the generate_wordlist method to improve code readability. - Create the fetch_words_from_files method to efficiently gather words from source files. - Implement the read_source_file method to yield valid words from source files. - Add the write_words_to_file method to sort and write valid words to the destination file. This commit improves the WordList class by enhancing its methods and documentation, resulting in cleaner and more maintainable code. --- src/modules/wordlist.py | 89 ++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 27 deletions(-) diff --git a/src/modules/wordlist.py b/src/modules/wordlist.py index d9b08a8..ca6f7c9 100644 --- a/src/modules/wordlist.py +++ b/src/modules/wordlist.py @@ -1,49 +1,84 @@ import os -from io import TextIOWrapper -from typing import Generator, Set -from src.utils.utils import valid_word +from typing import Generator, Set, TextIO from src.config import SOURCES, WORDLIST +from src.utils.utils import valid_word class WordList: - """Represents a class that can generate and load a wordlist file for Spellsolver""" + """ + Represents a class that can generate and load a wordlist file for Spellsolver. + + Attributes: + source_path (str): The path to the folder containing source files. + dest_path (str): The path to the destination wordlist file. + """ - def __init__(self) -> None: - self.source_path: str = SOURCES - self.dest_path: str = WORDLIST + def __init__(self): + """ + Initialize a WordList object with source and destination paths. + """ + self.source_path = SOURCES + self.dest_path = WORDLIST - def open_file(self) -> TextIOWrapper: - """Load a wordlist file, if it doesn't exist it is generated""" + def open_file(self) -> TextIO: + """ + Load a wordlist file, generate it if it doesn't exist. + + Returns: + TextIO: A file object representing the opened wordlist file. + """ if not os.path.isfile(self.dest_path): self.generate_wordlist() print("Wordlist file successfully generated from sources") - return open(self.dest_path) def generate_wordlist(self) -> None: - """Generate a wordlist files from multiples files in a source folder""" - files = ( - os.path.join(self.source_path, file) - for file in os.listdir(self.source_path) - ) + """ + Generate a wordlist file from multiple files in a source folder. + """ + words = self.fetch_words_from_files() + self.write_words_to_file(words, path=self.dest_path) + + def fetch_words_from_files(self) -> Set[str]: + """ + Fetch valid words from a list of source files. + + Returns: + Set[str]: A set containing valid words from source files. + """ words = set() + for file in os.listdir(self.source_path): + words.update( + self.read_source_file(path=os.path.join(self.source_path, file)) + ) + return words - for file in files: - words.update(self.read_source_file(path=file)) + @staticmethod + def read_source_file(path: str) -> Generator[str, None, None]: + """ + Read valid words from a source file. - self.write_dest_file(words=words, path=self.dest_path) + Args: + path (str): The path to the source file. - def read_source_file(self, path: str) -> Generator[str, None, None]: - """Read all valid words from a source file""" + Yields: + Generator[str, None, None]: A generator yielding valid words from the source file. + """ with open(path) as file: - return ( - word - for word in (line[:-1].lower() for line in file.readlines()) - if valid_word(word) - ) + for line in file.readlines(): + word = line.strip().lower() + if valid_word(word): + yield word + + @staticmethod + def write_words_to_file(words: Set[str], path: str) -> None: + """ + Sort valid words and write them to a destination file. - def write_dest_file(self, words: Set[str], path: str) -> None: - """Sorts valid words and writes them to a destination file""" + Args: + words (Set[str]): A set of valid words to be written to the file. + path (str): The path to the destination file. + """ words = sorted(words) with open(path, "w") as f: for word in words: