code: def loop_until_timeout_or_valid(timeout_s, function, validation_fn, sleep_s=1):  # pylint: disable=invalid-name
    
    if timeout_s is None or not hasattr(timeout_s, 'has_expired'):
      timeout_s = PolledTimeout(timeout_s)
  
    while True:
      # Calls the function at least once
      result = function()
      if validation_fn(result) or timeout_s.has_expired():
        return result
      time.sleep(sleep_s)
docs: Loops until the specified function returns valid or a timeout is reached.
  
    Note: The function may return anything which, when passed to validation_fn,
    evaluates to implicit True.  This function will loop calling the function as
    long as the result of validation_fn(function_result) returns something which
    evaluates to False. We ensure function is called at least once regardless
    of timeout.
  
    Args:
      timeout_s: The number of seconds to wait until a timeout condition is
          reached. As a convenience, this accepts None to mean never timeout.  Can
          also be passed a PolledTimeout object instead of an integer.
      function: The function to call each iteration.
      validation_fn: The validation function called on the function result to
          determine whether to keep looping.
      sleep_s: The number of seconds to wait after calling the function.
  
    Returns:
      Whatever the function returned last.