Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Documentation, assignment in expression.

Reply
Thread Tools

Documentation, assignment in expression.

 
 
Thomas Rachel
Guest
Posts: n/a
 
      03-26-2012
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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      03-26-2012
On 3/26/2012 1:36 AM, Steven D'Aprano wrote:
>
> (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.

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      03-26-2012
On Mon, 26 Mar 2012 15:54:52 +0200, Thomas Rachel
<nutznetz-0c1b6768-bfa9-48d5-a470->
declaimed the following in gmane.comp.python.general:

> Am 26.03.2012 00:59 schrieb Dennis Lee Bieber:
>
> > If you use the longer form
> >
> > con = db.connect()
> > cur = con.cursor()
> >
> > the cursor object, in all that I've worked with, does function for
> > iteration

>
> 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...sor-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.
--
Wulfraed Dennis Lee Bieber AF6VN
HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      03-26-2012
On 03/26/12 08:59, Thomas Rachel wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
Question about interference and Wireless Channel Assignment HOESan Wireless Networking 4 09-04-2004 08:36 PM
Comparison of Bit Vectors in a Conditional Signal Assignment Statement Anand P Paralkar VHDL 2 08-04-2003 08:40 PM
Help: conditional attribute assignment itsme VHDL 1 07-23-2003 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57