Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7638649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:13:35+00:00 2026-05-31T08:13:35+00:00

In querying an API that has a paginated list of unknown length I found

  • 0

In querying an API that has a paginated list of unknown length I found myself doing essentially

def fetch_one(self, n):
    data = json.load(urlopen(url_template % n))
    if data is None:
        self.finished = True
        return
    for row in data:
        if row_is_weird(row):
            self.finished = True
            return
        yield prepare(row)

def work(self):
    n = 1
    self.finished = False
    while not self.finished:
        consume(self.fetch_one(n))
        n += 1

the split between work and fetch_one makes it very easy to test, but the signalling via instance variables means I can’t have more than one work going on at the same time, which sucks. I came up with what I think is a cleaner solution, but it involves an iterator with two “done” states, and I have no idea what to call it. I’m sure this pattern exists elsewhere, so I’d appreciate pointers (or reasons why this is stupid):

class Thing(object):
    def __init__(self, gen):
        self.gen = gen
        self.finished = False

    def __iter__(self):
        return self

    def __next__(self):
        try:
            v = next(self.gen)
        except StopThisThing:
            self.finished = True
            raise StopIteration
        else:
            return v
    next = __next__

which I’d then use like

@thinged
def fetch_one(self, n):
    data = json.load(urlopen(url_template % n))
    if data is None:
        raise StopThisThing()
    for row in data:
        if row_is_weird(row):
            raise StopThisThing()
        yield prepare(row)

def work(self):
    n = 1
    while True:
        one = self.fetch_one(n)
        consume(one)
        if one.finished:
            break
        n += 1

so what is this Thing I have created?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:13:36+00:00Added an answer on May 31, 2026 at 8:13 am

    I think that you can avoid that by yielding something special.

    I had to build my own runnable example, to show what I mean:

    def fetch_one(n):
        lst = [[1,2,3], [4,5,6], [7,8,9]][n]
        for x in lst:
            if x == 6:
                yield 'StopAll'
                return
            yield x
    
    def work():
        n = 0
        in_progress = True
        while in_progress:
            numbers_iterator = fetch_one(n)
            for x in numbers_iterator:
                if x == 'StopAll':
                    in_progress = False
                    break
                print('x =', x)
            n += 1
    
    work()
    

    Output:

    x = 1
    x = 2
    x = 3
    x = 4
    x = 5
    

    I like this more than self.finished or a decorator like the one you built, but I think that something better could still be found. (Maybe this answer could help you with that).

    Update: A much simplier solution might be to transform fetch_one into a class that carries its own finised flag.

    A decorator approach to this solution might be:

    class stopper(object):
        def __init__(self, func):
            self.func = func
            self.finished = False
    
        def __call__(self, *args, **kwargs):
            for x in self.func(*args, **kwargs):
                if x == 6:
                    self.finished = True
                    raise StopIteration
                yield x
            else:
                self.finished = True
    

    Basically you don’t care anymore how fetch_one works, only if what yields is ok or not.

    Usage example:

    @stopper
    def fetch_one(n):
        lst = [[1,2,3], [4,5,6], [7,8,9]][n]
        #lst = [[1,2,3], [], [4,5,6], [7,8,9]][n]   # uncomment to test for/else
        for x in lst:
            yield x
    
    def work():
        n = 0
        while not fetch_one.finished:
            for x in fetch_one(n):
                print('x =', x)
            n += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with the criteria API, querying against a mapping file that I cannot
I am interested in querying a REST API using Groovy. I found HttpURLClient which
I am querying a sharepoint list via the web service api. Calling the service
When querying facebook graph's api I see that there are several properties missing, for
I'm using GAPI library (in PHP) for querying Google Analytics API. I request 2
I'm making an android application using phonegap. I'm using phonegap's Storage api for querying
I'm querying the database for a single record that matches the id passed in
I'm querying an MFC implemented COM object that is implemented as follows: class CA
I'm writing a framework for querying the Mediawiki API . I have a Page
I am querying Amazon's Product Advertising API for Instant Video (streaming) results. Everything is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.