<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def cursor(self, function, return_pages=False, **params):
 """Returns a generator for results that match a specified query. :param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search) :param \*\*params: Extra parameters to send with your request (usually parameters accepted by the Twitter API endpoint) :rtype: generator Usage:: OAUTH_TOKEN_SECRET) """

        if not callable(function):
            raise TypeError('.cursor() takes a Twython function as its first \
                            argument. Did you provide the result of a \
                            function call?')

        if not hasattr(function, 'iter_mode'):
            raise TwythonError('Unable to create generator for Twython \
                               method "%s"' % function.__name__)

        while True:
            content = function(**params)

            if not content:
                raise StopIteration

            if hasattr(function, 'iter_key'):
                results = content.get(function.iter_key)
            else:
                results = content

            if return_pages:
                yield results
            else:
                for result in results:
                    yield result

            if function.iter_mode == 'cursor' and \
               content['next_cursor_str'] == '0':
                raise StopIteration

            try:
                if function.iter_mode == 'id':
                    # Set max_id in params to one less than lowest tweet id
                    if hasattr(function, 'iter_metadata'):
                        # Get supplied next max_id
                        metadata = content.get(function.iter_metadata)
                        if 'next_results' in metadata:
                            next_results = urlsplit(metadata['next_results'])
                            params = dict(parse_qsl(next_results.query))
                        else:
                            # No more results
                            raise StopIteration
                    else:
                        # Twitter gives tweets in reverse chronological order:
                        params['max_id'] = str(int(content[-1]['id_str']) - 1)
                elif function.iter_mode == 'cursor':
                    params['cursor'] = content['next_cursor_str']
            except (TypeError, ValueError):  # pragma: no cover
                raise TwythonError('Unable to generate next page of search \
                                   results, `page` is not a number.')
            except (KeyError, AttributeError):  #pragma no cover
                raise TwythonError('Unable to generate next page of search \
                                   results, content has unexpected structure.')