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


Groups > comp.lang.python > #102302

Re: Cannot step through asynchronous iterator manually

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Subject Re: Cannot step through asynchronous iterator manually
Date Sat, 30 Jan 2016 13:04:44 +0000
Lines 41
Message-ID <mailman.128.1454159539.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>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de eVZ5WXcLlQMkJDZjNCWxXwclQIymH2IakFW5vygzzR7w==
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'cc:addr:python-list': 0.09; 'rows': 0.09; 'exception': 0.13; '...)': 0.16; '2))': 0.16; '2016': 0.16; 'cc:name:python list': 0.16; 'consume': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'returned,': 0.16; 'row': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'suggested': 0.20; 'select': 0.23; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; 'raise': 0.29; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'exist': 0.35; 'but': 0.36; 'too': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'january': 0.38; 'received:209': 0.38; 'test': 0.39; 'does': 0.39; 'application': 0.39; 'your': 0.60; 'more': 0.63; 'times': 0.63; 'realise': 0.66; 'statement,': 0.66; 'liked': 0.67; 'frank': 0.72; 'chrisa': 0.84; 'oscar': 0.84; 'do:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=Tg83CKz2WNgjFsGb0hw0X4gpUrBhXMRkUXHOXgXiOSA=; b=sBQPXgrmUJJS8fqxBBSJr2PkleOJJK5dqx4cMLdSbq1/MvCSHci566IwZltl/vaOQX 5aXDPxcCgJcYe7+cInbsUNYDPVc6MROwPc20tIfG/IvefmC/F8JQ33x12E5+xuO8HLgj VCiHu9omQt8a4TRwR4CAXpcbdn+fTZlqbP+Ch9oXtXuiLOqCtWfsbhk/r1+2b3IZQZQa 8TTkCKpx/xZuCNBAN8cyZ2uw5JYIOLK9GueABzQyZtWB7FjidQivPESJ0b9WzH3+mcRA C7c6rIwl1ZBOwOIYUZcZG6fHB8jKg0OA8LpB3juI+EEuVg547bwbET8JmF4U0sWxEmlO /L8Q==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=Tg83CKz2WNgjFsGb0hw0X4gpUrBhXMRkUXHOXgXiOSA=; b=NuwaOevGt2dhih6sAIh6PU5LU8QNlA6IMUngwuZvlmQl1uQ32H1A7xn6jvqypIYUJj gfrxW0Tg18aML6MqIzilxqyW7ty5CQ2Xbo3fuOZs6L0ZF7AdGEZTygTtsKbJ1Rcdm/d6 DtMJwpeiPev+DPmpe0/6rltG7GHh6lQbfbYt6NAfrzhSBkOox0Oi3d16dLeD7t5DSfSH x2hof9ERrPgJ7u/hkrezgBEzdKHagqSH5Fp2tsfjPL0RuGq3+BgwrDJUkOCu4QfRUXLD /MN0ys7+B+DY1iUGTEKsOFjPWNrIIQqs/8y8DPU8pX27nK527lRWxvj+9LPD+pvx5yqd dwzw==
X-Gm-Message-State AG10YOQSyCbvn+A3hojRJTsMz1gLq7RVhsmrRoQFTPI3shECuCA3noRzDHe5xKqa1O2NAI9S/UcmhLEqKOFRpA==
X-Received by 10.112.168.194 with SMTP id zy2mr5286006lbb.120.1454159104491; Sat, 30 Jan 2016 05:05:04 -0800 (PST)
In-Reply-To <n8hrs1$orn$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102302

Show key headers only | View raw


On 30 January 2016 at 08:22, Frank Millman <frank@chagford.com> wrote:
> There are times when I want to execute a SELECT statement, and test for
> three possibilities -
>    - if no rows are returned, the object does not exist
>    - if one row is returned, the object does exist
>    - if more that one row is returned, raise an exception
>
> 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

The simplest thing would just be to call list(cur) but I realise that
you don't want to consume more than 2 rows from the database so just
use islice:

rows = list(islice(cur, 2))  # pull at most 2 rows
if not rows:
    # no rows
elif len(rows) > 1:
    # too many rows
row = rows[0]

Depending on your application if you just want to raise any error when
there's not exactly one row then you could just do:

(row,) = list(islice(cur, 2))

--
Oscar

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


Thread

Re: Cannot step through asynchronous iterator manually Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-01-30 13:04 +0000

csiph-web