Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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 here 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.

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 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.

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 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.

Renamed variable as in Rev 5 of the question
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

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(reqrequest)
 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 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.

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(req)
 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 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.

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 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.

added 362 characters in body
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

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(req)
 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 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.

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(req)
 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

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(req)
 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 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.

Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62
Loading
lang-py

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