joshbduncan / word-search-generator

Make awesome Word Search puzzles!
MIT License
72 stars 23 forks source link

alter generate.retry to take the maximum number of tries as a parameter #29

Closed duck57 closed 1 year ago

duck57 commented 1 year ago

I want to use Word Search as the basis for a crossword generator (perhaps in December, more likely in February or March). The algorithm I'd use is:

  1. Read in a tab-separated file of words and their hints
  2. Find initial puzzle attempt size as max(sqrt(sum(len(word) for word in words)), len(longest_word))
  3. Use the @retry(3) wrapper to try generating a puzzle with those dimensions 3 times.
  4. If not all words were fit within 3 tries, try again at the next
  5. Bunch of formatting and display logic to think through at a later date

Changes to the code

  1. Add the parameter to the @retry function
  2. Change the @retry decorator on try_to_fit_word to be @retry(max_fit_tries)
duck57 commented 1 year ago

Additional complication I fully expect you to close as out-of-scope (unless you plan to separate out @retry into its own package):

Add optional params to the retry function to specify which errors to retry and which to always raise. Use would be to save time on exceptions that make no sense to retry (which there aren't any in this word search, hence I expect this to be out-of-scope).

joshbduncan commented 1 year ago

@duck57, the update to the @retry decorator is pretty straightforward. The typing for wrapped decorators is ridiculous so I'm not going to try and please mypy at this time. I just wanted to respond back so you can see the implementation. Also, retries defaults to max_fit_tries so you don't have to supply an argument to the decorator unless you want it to be different.

def retry(retries: int = max_fit_tries):

    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            attempt = 0
            while attempt < retries:
                try:
                    return func(*args, **kwargs)
                except Exception:
                    attempt += 1
            return

        return wrapper

    return decorator

...

@retry(3)
def try_to_fit_word():
    ...
joshbduncan commented 1 year ago

@duck57, I went ahead and added this and merged it into main. I'm slowly making progress on the masking tests but there is a whole lot to test. Not sure when I'll get it done.