Re: variable assignment in "while" loop

Discussion in 'Python' started by Andy Todd, Jul 29, 2003.

  1. Andy Todd

    Andy Todd Guest

    wrote:

    >>From: Andy Todd [mailto:]
    >>Sent: Martes, 29 de Julio de 2003 10:23 a.m.
    >>
    >>Spot on, with one (minor) correction. With a for loop you have to
    >>iterate over the results of a call to a method on the cursor, e.g.;
    >>
    >>for info in mydbcursor.fetchall():
    >> print "Information:", info
    >>
    >>You could replace fetchall() with fetchmany() or, if you are feeling
    >>contrary, fetchone() ;-)
    >>
    >>Regards,
    >>Andy
    >>

    >
    >
    > Are you sure about this? Because, if you have to iterate
    > through .fetchall() (for example), then the cursor isn't
    > actually an iterable object, the result of fetchall() is
    > (it's a list, which is iterable), same goes for fetchmany();
    > BUT, if you iterate over the results of fetchone() then
    > you're gonna do a columnwise iteration over a single row.
    >
    > :)
    >
    > So... anyone... do cursors have an __iter__() method?
    > I don't have a DB module at hand to check it out...
    >
    > -gustavo
    >
    >


    Technically, cursor objects are not iterators, and the DB-API
    specification has this to say about the various methods that return data;

    """
    ..fetchone()

    Fetch the next row of a query result set, returning a
    single sequence, or None when no more data is
    available.

    ....

    ..fetchmany([size=cursor.arraysize])

    Fetch the next set of rows of a query result, returning a
    sequence of sequences (e.g. a list of tuples). An empty
    sequence is returned when no more rows are available.

    ....

    ..fetchall()

    Fetch all (remaining) rows of a query result, returning
    them as a sequence of sequences (e.g. a list of tuples).
    """

    Which means that the cursor object itself is not an iterator, and the
    methods need only return a sequence (or sequence of sequences).

    But, thats not to say that the db modules can't implement a generator to
    produce the results from the 'fetch' methods if they want to. I don't
    know if any of the database modules do, but then thats up to the module
    authors and I tend not to worry about it too much, I just trust the module.

    Regards,
    Andy
    --
    --------------------------------------------------------------------------------
    From the desk of Andrew J Todd esq - http://www.halfcooked.com/
     
    Andy Todd, Jul 29, 2003
    #1
    1. Advertising

  2. > >>Spot on, with one (minor) correction. With a for loop you have to
    > >>iterate over the results of a call to a method on the cursor, e.g.;
    > >>
    > >>for info in mydbcursor.fetchall():
    > >> print "Information:", info
    > >>
    > >>You could replace fetchall() with fetchmany() or, if you are feeling
    > >>contrary, fetchone() ;-)

    > >
    > >
    > > Are you sure about this? Because, if you have to iterate
    > > through .fetchall() (for example), then the cursor isn't
    > > actually an iterable object, the result of fetchall() is
    > > (it's a list, which is iterable), same goes for fetchmany();
    > > BUT, if you iterate over the results of fetchone() then
    > > you're gonna do a columnwise iteration over a single row.
    > >
    > > So... anyone... do cursors have an __iter__() method?
    > > I don't have a DB module at hand to check it out...

    >
    > Technically, cursor objects are not iterators, and the DB-API
    > specification has this to say about the various methods that return data;
    >
    > """
    > .fetchone()
    >
    > Fetch the next row of a query result set, returning a
    > single sequence, or None when no more data is
    > available.


    Remember, the second form of the iter() function makes
    it easy to build a real iterator here and a fast one too:

    for row in iter(mydbcursor.fetchone, None):
    print row



    > .fetchall()
    >
    > Fetch all (remaining) rows of a query result, returning
    > them as a sequence of sequences (e.g. a list of tuples).
    > """
    >
    > Which means that the cursor object itself is not an iterator, and the
    > methods need only return a sequence (or sequence of sequences).



    Right, so if you want iterator performance (just-in-time production,
    minimal memory consumption, better cache utilization, etc), then use
    fetchone wrapped in iter() as shown above.


    Raymond Hettinger
     
    Raymond Hettinger, Jul 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,289
    Tim Slattery
    Mar 30, 2005
  2. Sybren Stuvel

    variable assignment in "while" loop

    Sybren Stuvel, Jul 29, 2003, in forum: Python
    Replies:
    6
    Views:
    444
    Bengt Richter
    Jul 29, 2003
  3. Andy Todd
    Replies:
    0
    Views:
    820
    Andy Todd
    Jul 29, 2003
  4. Uday Bidkar
    Replies:
    4
    Views:
    522
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    452
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page