Executes a function at regular intervals while the condition doesn't raise an exception and the amount of attempts < maxAttempts. :param condition: a function :param delay: the delay in second :param max_attempts: the maximum number of attempts. So the timeout
(condition, delay, max_attempts)
| 37 | |
| 38 | |
| 39 | def wait_until_not_raised(condition, delay, max_attempts): |
| 40 | """ |
| 41 | Executes a function at regular intervals while the condition |
| 42 | doesn't raise an exception and the amount of attempts < maxAttempts. |
| 43 | :param condition: a function |
| 44 | :param delay: the delay in second |
| 45 | :param max_attempts: the maximum number of attempts. So the timeout |
| 46 | of this function will be delay*max_attempts |
| 47 | """ |
| 48 | def wrapped_condition(): |
| 49 | try: |
| 50 | result = condition() |
| 51 | except: |
| 52 | return False, None |
| 53 | |
| 54 | return True, result |
| 55 | |
| 56 | attempt = 0 |
| 57 | while attempt < (max_attempts-1): |
| 58 | attempt += 1 |
| 59 | success, result = wrapped_condition() |
| 60 | if success: |
| 61 | return result |
| 62 | |
| 63 | time.sleep(delay) |
| 64 | |
| 65 | # last attempt, let the exception raise |
| 66 | return condition() |
| 67 | |
| 68 | |
| 69 | def late(seconds=1): |