Documentation, assignment in expression.

T

Thomas Rachel

Am 25.03.2012 15:03 schrieb Tim Chase:
Perhaps a DB example
works better. With assignment allowed in an evaluation, you'd be able to
write

while data = conn.fetchmany():
for row in data:
process(row)

whereas you have to write

while True:
data = conn.fetchmany()
if not data: break
for row in data:
process(row)

Or simpler

for data in iter(conn.fetchmany, []):
for row in data:
process(row)

provided that a block of rows is returned as a list - which might be
different among DB engines.


Thomas
 
T

Terry Reedy

(I seem to recall a language that used a single = for both assignment and
equality testing, guessing which one you meant from context. BASIC
perhaps?

Right. In some Basics, such as MS GW-Basic (I still have their book), a
= b = c meant a = (b = c), or in Python, a = b==c.
 
D

Dennis Lee Bieber

Am 26.03.2012 00:59 schrieb Dennis Lee Bieber:


I use this form regularly with MySQLdb and am now surprised to see that
this is optional according to http://www.python.org/dev/peps/pep-0249/.

So a database cursor is not required to be iterable, alas.

Sounds like it may be time to do a survey...

MySQLdb iterable cursor
Cursor, DictCursor, SSCursor, SSDictCursor (the
first two suck the entire result set to the client and then feed it to
the application, the latter two keep the results on the server and fetch
them as needed; the Dict versions obviously return dictionaries, the
others return tuples)

SQLite3 iterable cursor
Dictionary-like cursor using a row-factory (in
Python 2.5 documentation: 13.13.6.2 Accessing columns by name instead of
by index )

Psycopg iterable cursor
dictionary-like cursor
http://initd.org/psycopg/docs/extras.html#connection-and-cursor-subclasses

Pygresql iterable cursor
dictresult() ?

py-postgresql unknown -- the only documentation I found appears to
be for the NON-DB-API interface
Row object supports index or key access

pypgsql NOT iterable (based on readme and source)
Result object supports index or key access

Can anyone add to this? mxODBC, pyodbc, M$ SQL server?

One thing I note in the above is that ALL of those adapters have
built-in means of accessing return data as a dictionary, so the OP's
generator function to create dictionary values is likely superfluous,
and may even be a bottleneck in processing.
 
T

Tim Chase

Am 25.03.2012 15:03 schrieb Tim Chase:
while True:
data = conn.fetchmany()
if not data: break
for row in data:
process(row)

Or simpler

for data in iter(conn.fetchmany, []):
for row in data:
process(row)

Nice! That's the first time I've seen the 2-parameter version of
iter() improve things :) (I've seen it once or twice before in
the 2-param form, and wondered why it wasn't written in a clearer
way). Also, I was surprised to learn that the sentinel was
available as far back as 2.2 (I knew about the 1-param version as
far back as 2.3 when I became more serious about Python).

-tkc
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top