From 628fbab3c014d3cc051810defcd02b0f26b11383 Mon Sep 17 00:00:00 2001 From: Meinard Francisco <44516102+znarfm@users.noreply.github.com> Date: Wed, 20 Nov 2024 01:58:05 +0800 Subject: [PATCH 1/4] use whole file for generator, loop back to start if EOF, error handling --- bing_rewards/__init__.py | 47 ++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/bing_rewards/__init__.py b/bing_rewards/__init__.py index 23f961e..c7ca699 100644 --- a/bing_rewards/__init__.py +++ b/bing_rewards/__init__.py @@ -52,17 +52,42 @@ def word_generator() -> Generator[str]: """ word_data = resources.files('bing_rewards').joinpath('data', 'keywords.txt') while True: - with ( - resources.as_file(word_data) as p, - p.open(mode='r', encoding='utf-8') as fh, - ): - fh.seek(0, SEEK_END) - size = fh.tell() # Get the filesize of the Keywords file - # Start at a random position in the stream - fh.seek(random.randint(0, (size * 3 // 4)), SEEK_SET) - for line in fh: - # Use the built in file handler generator - yield line.strip() + try: + with ( + resources.as_file(word_data) as p, + p.open(mode='r', encoding='utf-8') as fh, + ): + # Get the filesize of the Keywords file + fh.seek(0, SEEK_END) + size = fh.tell() + + if size == 0: + raise ValueError("Keywords file is empty") + + # Start at a random position in the stream + fh.seek(random.randint(0, size - 1), SEEK_SET) + + # Read and discard partial line to ensure we start at a clean line boundary + fh.readline() + + # Read lines until EOF + for line in fh: + line = line.strip() + if line: # Skip empty lines + yield line + + # If we hit EOF, seek back to start and continue until we've yielded enough words + fh.seek(0) + for line in fh: + line = line.strip() + if line: + yield line + except (FileNotFoundError, IOError) as e: + print(f"Error accessing keywords file: {e}") + raise + except Exception as e: + print(f"Unexpected error in word generation: {e}") + raise def browser_cmd(exe: Path, agent: str, profile: str = '') -> list[str]: From b520cbbfbae77140f87a72dae5e232d2ad13d1b2 Mon Sep 17 00:00:00 2001 From: Meinard Francisco <44516102+znarfm@users.noreply.github.com> Date: Wed, 20 Nov 2024 01:59:41 +0800 Subject: [PATCH 2/4] added type hints, docstring changes --- bing_rewards/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bing_rewards/__init__.py b/bing_rewards/__init__.py index c7ca699..e376d0a 100644 --- a/bing_rewards/__init__.py +++ b/bing_rewards/__init__.py @@ -44,11 +44,19 @@ import bing_rewards.options as app_options -def word_generator() -> Generator[str]: +def word_generator() -> Generator[str, None, None]: """Infinitely generate terms from the word file. Starts reading from a random position in the file. If end of file is reached, close and restart. + Handles file operations safely and ensures uniform random distribution. + + Yields: + str: A random keyword from the file, stripped of whitespace. + + Raises: + FileNotFoundError: If the keywords file cannot be found. + IOError: If there are issues reading the file. """ word_data = resources.files('bing_rewards').joinpath('data', 'keywords.txt') while True: From 5cd85b157307533197b378428498324b2b5b9f0e Mon Sep 17 00:00:00 2001 From: Meinard Francisco <44516102+znarfm@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:15:08 +0800 Subject: [PATCH 3/4] fix ruff linting errors --- bing_rewards/__init__.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/bing_rewards/__init__.py b/bing_rewards/__init__.py index e376d0a..84e70d7 100644 --- a/bing_rewards/__init__.py +++ b/bing_rewards/__init__.py @@ -55,17 +55,17 @@ def word_generator() -> Generator[str, None, None]: str: A random keyword from the file, stripped of whitespace. Raises: - FileNotFoundError: If the keywords file cannot be found. - IOError: If there are issues reading the file. + OSError: If there are issues accessing or reading the file. """ word_data = resources.files('bing_rewards').joinpath('data', 'keywords.txt') - while True: - try: + + try: + while True: with ( resources.as_file(word_data) as p, p.open(mode='r', encoding='utf-8') as fh, ): - # Get the filesize of the Keywords file + # Get the file size of the Keywords file fh.seek(0, SEEK_END) size = fh.tell() @@ -79,23 +79,23 @@ def word_generator() -> Generator[str, None, None]: fh.readline() # Read lines until EOF - for line in fh: - line = line.strip() - if line: # Skip empty lines - yield line + for raw_line in fh: + stripped_line = raw_line.strip() + if stripped_line: # Skip empty lines + yield stripped_line # If we hit EOF, seek back to start and continue until we've yielded enough words fh.seek(0) - for line in fh: - line = line.strip() - if line: - yield line - except (FileNotFoundError, IOError) as e: - print(f"Error accessing keywords file: {e}") - raise - except Exception as e: - print(f"Unexpected error in word generation: {e}") - raise + for raw_line in fh: + stripped_line = raw_line.strip() + if stripped_line: + yield stripped_line + except OSError as e: + print(f"Error accessing keywords file: {e}") + raise + except Exception as e: + print(f"Unexpected error in word generation: {e}") + raise def browser_cmd(exe: Path, agent: str, profile: str = '') -> list[str]: From 438687fb8df2f0fc3d4efa540acc21931cab2867 Mon Sep 17 00:00:00 2001 From: Meinard Francisco <44516102+znarfm@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:02:50 +0800 Subject: [PATCH 4/4] fix ruff formatting --- bing_rewards/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bing_rewards/__init__.py b/bing_rewards/__init__.py index 84e70d7..de2b0bd 100644 --- a/bing_rewards/__init__.py +++ b/bing_rewards/__init__.py @@ -70,7 +70,7 @@ def word_generator() -> Generator[str, None, None]: size = fh.tell() if size == 0: - raise ValueError("Keywords file is empty") + raise ValueError('Keywords file is empty') # Start at a random position in the stream fh.seek(random.randint(0, size - 1), SEEK_SET) @@ -91,10 +91,10 @@ def word_generator() -> Generator[str, None, None]: if stripped_line: yield stripped_line except OSError as e: - print(f"Error accessing keywords file: {e}") + print(f'Error accessing keywords file: {e}') raise except Exception as e: - print(f"Unexpected error in word generation: {e}") + print(f'Unexpected error in word generation: {e}') raise