Handling exceptions in while loop - Python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Here is my code (almost full version for @cdhowie :)):

def getResult(method, argument=None):
  result = None

  while True:
    print( ### loop )
    try:
      print ( ### try hard... )
      if argument:
        result = method(argument)
      else:
        result = method()
      break
    except Exception as e:
      print( ### GithubException )
      if 403 == e.status:
        print( Warning:   + str(e.data))
        print( I will try again after 10 minutes... )
      else:
        raise e

  return result

def getUsernames(locations, gh):
  usernames = set()

  for location in locations:
    print location
    result = getResult(gh.legacy_search_users, location)
    for user in result:
      usernames.add(user.login)
      print user.login,

  return usernames

# "main.py"
gh = Github()
locations = [ Washington ,  Berlin ]
# "main.py", line 12 is bellow
usernames = getUsernames(locations, gh)

The problem is, that exception is raised, but I can t handle it. Here is an output:

### loop
### try hard...
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    usernames = getUsernames(locations, gh)
  File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames
    for user in result:
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__
    newElements = self.__grow()
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 45, in __grow
    newElements = self._fetchNextPage()
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 37, in _fetchNextPage
    return self.get_page(page)
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 48, in get_page
    None
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Requester.py", line 69, in requestAndCheck
    raise GithubException.GithubException(status, output)
github.GithubException.GithubException: 403 {u message : u API Rate Limit Exceeded for 11.11.11.11 }

Why it doesn t print ### handling exception?

Answers

Take a close look at the stack trace in the exception:

Traceback (most recent call last):
  File "main.py", line 12, in <module>
    usernames = getUsernames(locations, gh)
  File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames
    for user in result:
  File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__
    newElements = self.__grow()
  ...

The exception is being thrown from code being called by the line for user in result: after getResult finishes executing. This means that the API you re using is using lazy evaluation, so the actual API request doesn t quite happen when you expect it to.

In order to catch and handle this exception, you ll need to wrap the code inside the getUsernames function with a try/except handler.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/12940717/handling-exceptions-in-while-loop-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils