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


Groups > comp.lang.python > #35631

Re: Creating an iterator in a class

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.unit0.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04; 'argument': 0.04; 'processed': 0.05; 'result,': 0.05; 'params': 0.07; 'reason,': 0.07; 'run,': 0.07; '__init__': 0.09; 'cursor:': 0.09; 'instantiated': 0.09; 'def': 0.10; 'dec': 0.15; 'instantiate': 0.16; 'iterates': 0.16; 'iterator.': 0.16; 'subject:class': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'yield': 0.17; 'examples': 0.18; 'code,': 0.18; 'import': 0.21; 'defined': 0.22; 'tables': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'probably': 0.29; 'class': 0.29; 'query': 0.30; 'function': 0.30; 'code': 0.31; 'gets': 0.32; 'could': 0.32; 'safely': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'returning': 0.35; 'table': 0.35; 'continue': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'but': 0.36; 'client': 0.36; 'does': 0.37; 'two': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'share': 0.61; 'back': 0.62; 'provide': 0.62; 'results': 0.65; 'hoping': 0.72; 'saw': 0.75; 'connection.': 0.75; 'partially': 0.84; 'to:name:python': 0.84
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 :content-type; bh=jCOxYPe3ApQ4J+EDX+P8dKzVuIpOYHcAJKTw9lpskRw=; b=W2Io1VC3W7p7UEbTd7xYreJt4sU3UVPVTLdipxDJk5vJ5Xl4TBz6LZWDYrI9VCDv3z 75BWD4VG6AjOIQUn2Ar6Z+WcroG1n2pRz9737LkbAt04F7FFwCRqSErjphogOrEZ/0kv F+nA/skOSS0BjeNJjkDBCZ3HoFjDMxPU7ayOavutmjjtevfs2gk9tY6xOSz8IsbEOWUG pK9Sb4NOJGOksrE2W0n8Rcot2bSEcglHJHrXXk963+X7eoMmYk5gHe0mDzg8r3PH1lr/ MFQn/pDx18lDg7GOK7gHrLbwSkXQbu6TrM8OhCpXMB/TfAPf0ENufSR0pY6EfKHP1L0v rv1g==
MIME-Version 1.0
In-Reply-To <assp.0708f2479f.92097A6A775D5147B1078E3F15430B9234A0CBE8@prato.activenetwerx.local>
References <assp.0708f2479f.92097A6A775D5147B1078E3F15430B9234A0CBE8@prato.activenetwerx.local>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 27 Dec 2012 11:35:07 -0700
Subject Re: Creating an iterator in a class
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1352.1356633340.29569.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356633340 news.xs4all.nl 6949 [2001:888:2000:d::a6]:35471
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35631

Show key headers only | View raw


On Thu, Dec 27, 2012 at 7:44 AM, Joseph L. Casale
<jcasale@activenetwerx.com> wrote:
> I am writing a class to provide a db backed configuration for an application.
>
> In my programs code, I import the class and pass the ODBC params to the
> class for its __init__ to instantiate a connection.
>
> I would like to create a function to generically access a table and provide an
> iterator. How does one create a function in a class that takes an argument and
> returns an iterator? I saw some examples where the class gets instantiated
> with the table defined but I was hoping not to do this so I could continue to
> access various tables through one connection/cursor.

It's probably best if you use separate cursors anyway.  Say you have
two methods with a shared cursor:

    def iter_table_a(self):
        self.cursor.execute("SELECT * FROM TABLE_A")
        yield from self.cursor

    def iter_table_b(self):
        self.cursor.execute("SELECT * FROM TABLE_B")
        yield from self.cursor

If the client code calls iter_table_a and partially iterates over the
result, and then needs to use iter_table_b for some reason, then when
it gets back to the table A results the data will be gone.  You can
only safely share cursors when you can guarantee that each query will
be completely processed before the next query is run, and you can't
guarantee that if you're just returning iterators.

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


Thread

Re: Creating an iterator in a class Ian Kelly <ian.g.kelly@gmail.com> - 2012-12-27 11:35 -0700

csiph-web