Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102292

Re: Cannot step through asynchronous iterator manually

From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Re: Cannot step through asynchronous iterator manually
Date 2016-01-30 12:51 +0200
Message-ID <mailman.120.1454151091.2338.python-list@python.org> (permalink)
References <n8hjmt$b8n$1@ger.gmane.org> <CALwzid=sSDSm8hdAN+ORJ54A_jEu9Wc8103iqGKAah8mrj-TXw@mail.gmail.com> <n8hrs1$orn$1@ger.gmane.org> <CAPTjJmoAmVNTCKq7QYaDRNQ67Gcg9TxSXYXCrY==S9Djjna_rA@mail.gmail.com>

Show all headers | View raw


"Chris Angelico"  wrote in message 
news:CAPTjJmoAmVNTCKq7QYaDRNQ67Gcg9TxSXYXCrY==S9Djjna_rA@mail.gmail.com...
>
> On Sat, Jan 30, 2016 at 7:22 PM, Frank Millman <frank@chagford.com> wrote:
> > We had a recent discussion about the best way to do this, and ChrisA
> > suggested the following, which I liked -
> >
> >    cur.execute('SELECT ...)
> >    try:
> >        row = next(cur)
> >    except StopIteration:
> >        # row does not exist
> >    else:
> >        try:
> >            next_row = next(cur)
> >        except StopIteration:
> >            # row does exist
> >        else:
> >            # raise exception
> >
> > Now that I have gone async, I want to do the same with an asynchronous
> > iterator.
>
> Here's a crazy option. (Assuming that a row can't be None. If not, use
> a unique sentinel object.)
>
> cur.execute(whatever)
> have_row = None
> async for row in cur:
>     if have_row is not None:
>         raise TooManyRows
>     have_row = row
> if have_row is None:
>     raise NoRowFound
>

Not so crazy :-) If Ian doesn’t come up with a better idea I will run with 
it.

Here is a slight variation - just variable name changes really, but I think 
it is slightly easier to read -

found = False
async for row in cur:
    if found:
        raise TooManyRows
    found = True
if found:
    process row
else:
    no rows found

Frank

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Cannot step through asynchronous iterator manually "Frank Millman" <frank@chagford.com> - 2016-01-30 12:51 +0200

csiph-web