More concise way to do something a maximum of X times, raising an exception if it takes X times to run the command

user2380153

Is there a more concise way to perform this in Python:

tries = 10
for x in range(tries):
    try:
        foo()
        break
    except Exception as e:
        if x == tries-1:
            raise e

The point of it is that sometimes the operation fails (for whatever reason), but we want to allow for a number of retries before raising an exception.

9000

A usual way to make something more concise is to factor it out.

def retrying(max_attempts, func, *args, **kwargs):
  attempts_left = max_attempts
  while attempts_left:  # could be 'while True', but an extra check won't hurt
    try:
      return func(*args, **kwargs)
    except SomeException:
      attempts_left -= 1
      if not attempts_left:
        raise

The above code only catches SomeException and not Exception, else it would keep retrying when you have an undefined identifier in your code. Usually catching all exceptions is a bad idea, unless you re-raise them immediately. The lone raise preserves the func's stack trace. You could pass the list of exceptions to catch as a parameter.

The code above does most sense if you have several places where you need to retry actions. Then you just write retrying(3, foo, 1, b=2) instead of foo(1, b=2).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Allow infinitescroll.js to run X times, then load more posts

From Dev

Allow infinitescroll.js to run X times, then load more posts

From Dev

What is the recommended way to run a script X times in a row (one at a time)

From Dev

Ruby: Print something x amount of times

From Dev

how to get rows that repeated X times or more?

From Dev

Duplicate file x times in command shell

From Dev

How to execute 1 command x times in java

From Dev

Repeating the same command for x number of times

From Dev

What is the fastest way to run an exe command many times in C#?

From Dev

Return row counts for field that appears more than x times

From Dev

How to sort,uniq and display line that appear more than X times

From Dev

How to find the files that changed more than X times in a git repo?

From Dev

Delete a row that contains 0 more than 'x' amount of times

From Dev

Select columns where a value appears more than x times

From Dev

Loop X number of times

From Dev

getLine x times haskell

From Dev

Ansible to loop x times

From Dev

How to write a function that takes a function f and a float x and applies f to x k times using Swift?

From Dev

How to write a function that takes a function f and a float x and applies f to x k times using Swift?

From Dev

Run a command many times in Javascript

From Dev

Run a function x amount of times then sleep for a period before going again

From Dev

Run a function x amount of times then sleep for a period before going again

From Dev

Webdriver in Ruby, how to run a test case x times

From Dev

Get a while loop to run "X" number of times with two conditions in PHP

From Dev

Python Tkinter Run in a loop entries x times defined in a variable

From Dev

Idiomatic way to detect sequences of x times same object in an Array in Smalltalk?

From Dev

Better way to test if method is called x times with MiniTest?

From Dev

Is there a faster way of repeating a chunk of code x times and taking an average?

From Dev

Java Regex X{n,m} X, at least n but not more than m times

Related Related

  1. 1

    Allow infinitescroll.js to run X times, then load more posts

  2. 2

    Allow infinitescroll.js to run X times, then load more posts

  3. 3

    What is the recommended way to run a script X times in a row (one at a time)

  4. 4

    Ruby: Print something x amount of times

  5. 5

    how to get rows that repeated X times or more?

  6. 6

    Duplicate file x times in command shell

  7. 7

    How to execute 1 command x times in java

  8. 8

    Repeating the same command for x number of times

  9. 9

    What is the fastest way to run an exe command many times in C#?

  10. 10

    Return row counts for field that appears more than x times

  11. 11

    How to sort,uniq and display line that appear more than X times

  12. 12

    How to find the files that changed more than X times in a git repo?

  13. 13

    Delete a row that contains 0 more than 'x' amount of times

  14. 14

    Select columns where a value appears more than x times

  15. 15

    Loop X number of times

  16. 16

    getLine x times haskell

  17. 17

    Ansible to loop x times

  18. 18

    How to write a function that takes a function f and a float x and applies f to x k times using Swift?

  19. 19

    How to write a function that takes a function f and a float x and applies f to x k times using Swift?

  20. 20

    Run a command many times in Javascript

  21. 21

    Run a function x amount of times then sleep for a period before going again

  22. 22

    Run a function x amount of times then sleep for a period before going again

  23. 23

    Webdriver in Ruby, how to run a test case x times

  24. 24

    Get a while loop to run "X" number of times with two conditions in PHP

  25. 25

    Python Tkinter Run in a loop entries x times defined in a variable

  26. 26

    Idiomatic way to detect sequences of x times same object in an Array in Smalltalk?

  27. 27

    Better way to test if method is called x times with MiniTest?

  28. 28

    Is there a faster way of repeating a chunk of code x times and taking an average?

  29. 29

    Java Regex X{n,m} X, at least n but not more than m times

HotTag

Archive