Revision d63a7c6c-98c7-4014-93db-ae7edf8d0c05 - Code Review Stack Exchange

Instead of using `while` to count a specific number of times and manually handle the number, you should use `for _ in range(num_of_retries)`. It works the same except that you don't need to manually change `num_of_retries`. And this way `num_of_retries` wont be modified from the initial value, in case you need to access it again for any reason.

There is one small change to make this work. You need to use the `for ... else` syntax to `raise` if the loop is not broken. An `else` block after a `for` block will be executed if the `for` loop never reaches a `break` statement. This fits your case perfectly as `break` is called on success, but if there's never success then it will run through all the iterations and reach the else block where you can `raise`:

 for _ in range(num_of_retries):
 try: 
 request_id = str(uuid.uuid4())
 params = [('requestId', request_id)]
 url = 'service.com' 
 url = url + urllib.urlencode(params)
 request = urllib2.Request(url, data=html)
 request.add_header('Accept', 'application/pdf')
 request.add_header('Content-Type', 'text/html')
 handle = urllib2.urlopen(request)
 pdf_blob = handle.read() 
 break
 except Exception:
 typ, val, tb = sys.exc_info()
 logger.error(traceback.format_exception(typ, val, tb))
 time.sleep(time_interval)
 else:
 raise

Without any explicit argument, `raise` will just use the last exception that has been raised and handled. See the SO answer [here](http://stackoverflow.com/a/18001776/4374739) about it. Basically when you `try except` an exception, its details are being retained by Python and are passed to `raise` in the absence of any explicit exception passed to `raise`.

AltStyle によって変換されたページ (->オリジナル) /