Re: Stop writing Python 3.5 incompatible code :-)

2016-01-14 Thread Frank Millman
"Frank Millman" wrote in message news:n77j78$ld0$1...@ger.gmane.org... cur.execute(...) try: row = next(cur) except StopIteration: # 0 rows returned try: next(cur) except StopIteration: pass else: # >1 rows returned For the record, I just tried this and found an error. It is

Re: Stop writing Python 3.5 incompatible code :-)

2016-01-14 Thread Steven D'Aprano
On Thursday 14 January 2016 17:27, Frank Millman wrote: > So my test was - > > except ValueError as e: > if str(e).startswith('need'): > # 0 rows returned > else: > # > 1 rows returned > > This has worked for years, but with 3.5 it stopped working. It took me a > while to

Re: Stop writing Python 3.5 incompatible code :-)

2016-01-14 Thread Chris Angelico
On Thu, Jan 14, 2016 at 6:36 PM, Frank Millman wrote: > I like your first solution. In fact, with sqlite3, 'cur' is already > iterable, so I can just say > > cur.execute(...) > try: >row = next(cur) > except StopIteration: ># 0 rows returned > try: >next(cur) > except StopIteration: pa

Re: Stop writing Python 3.5 incompatible code :-)

2016-01-13 Thread Frank Millman
"Chris Angelico" wrote in message news:captjjmps+vfu33tulae5oivrvn_otfuxrp8yluy68qmu36-...@mail.gmail.com... On Thu, Jan 14, 2016 at 5:27 PM, Frank Millman wrote: > Using LBYL, one would retrieve the row(s) and check the length. I found > a > way to use EAFP, as follows - > > cur.execute('S

Re: Stop writing Python 3.5 incompatible code :-)

2016-01-13 Thread Chris Angelico
On Thu, Jan 14, 2016 at 5:27 PM, Frank Millman wrote: > Using LBYL, one would retrieve the row(s) and check the length. I found a > way to use EAFP, as follows - > > cur.execute('SELECT ...') > (row,) = cur > > This uses tuple unpacking, and only works if exactly one row is returned. If > it fails

Stop writing Python 3.5 incompatible code :-)

2016-01-13 Thread Frank Millman
This is a tongue-in-cheek follow-up to the thread on Python 4 incompatible code, but it did happen to me, and may help someone else. I execute a lot of database SELECT commands. In many cases I expect to get only one row returned, but it could be zero or more than one. Using LBYL, one would r