def RetryUntilValidOrLimitReached(method, limit, validation_fn, sleep_s=1,
                                  catch_exceptions=()):
  assert limit > 0, 'Limit must be greater than 0'

  def _execute_method(helper):
    try:
      return method()
    except catch_exceptions:
      if not helper.remaining:
        raise
      return None

  helper = RetryHelper(limit - 1)
  result = _execute_method(helper)
  while not validation_fn(result) and helper.RetryIfPossible():
    time.sleep(sleep_s)
    result = _execute_method(helper)
  return result

# pylint: disable=invalid-name
Executes a method until the retry limit or validation_fn returns True.
dataset/ETHPy150Open google/openhtf/openhtf/util/timeouts.py/RetryUntilValidOrLimitReached