changed cursor() from recursion to while loop

modified:   api.py
This commit is contained in:
drevicko 2013-11-08 13:03:23 +11:00
parent 638f75b93d
commit 32337fd036

View file

@ -408,38 +408,36 @@ class Twython(EndpointsMixin, object):
if not hasattr(function, 'iter_mode'): if not hasattr(function, 'iter_mode'):
raise TwythonError('Unable to create generator for Twython method "%s"' % function.__name__) raise TwythonError('Unable to create generator for Twython method "%s"' % function.__name__)
content = function(**params) while True:
content = function(**params)
if not content: if not content:
raise StopIteration raise StopIteration
if hasattr(function, 'iter_key'): if hasattr(function, 'iter_key'):
results = content.get(function.iter_key) results = content.get(function.iter_key)
else: else:
results = content results = content
for result in results: for result in results:
yield result yield result
if function.iter_mode == 'cursor' and content['next_cursor_str'] == '0': if function.iter_mode == 'cursor' and content['next_cursor_str'] == '0':
raise StopIteration raise StopIteration
try: try:
if function.iter_mode == 'id': if function.iter_mode == 'id':
if not 'max_id' in params: if not 'max_id' in params:
# Add 1 to the id because since_id and max_id are inclusive # Add 1 to the id because since_id and max_id are inclusive
if hasattr(function, 'iter_metadata'): if hasattr(function, 'iter_metadata'):
since_id = content[function.iter_metadata].get('since_id_str') since_id = content[function.iter_metadata].get('since_id_str')
else: else:
since_id = content[0]['id_str'] since_id = content[0]['id_str']
params['since_id'] = (int(since_id) - 1) params['since_id'] = (int(since_id) - 1)
elif function.iter_mode == 'cursor': elif function.iter_mode == 'cursor':
params['cursor'] = content['next_cursor_str'] params['cursor'] = content['next_cursor_str']
except (TypeError, ValueError): # pragma: no cover except (TypeError, ValueError): # pragma: no cover
raise TwythonError('Unable to generate next page of search results, `page` is not a number.') raise TwythonError('Unable to generate next page of search results, `page` is not a number.')
for result in self.cursor(function, **params):
yield result
@staticmethod @staticmethod
def unicode2utf8(text): def unicode2utf8(text):