Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

word_generator() changes #54

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions bing_rewards/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,58 @@
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:
OSError: If there are issues accessing or reading the file.
"""
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:
while True:
with (
resources.as_file(word_data) as p,
p.open(mode='r', encoding='utf-8') as fh,
):
# Get the file size 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 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 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]:
Expand Down