pg_proboscis 0.9 release

James William Pye pgsql at jwp.name
Tue Jan 8 19:15:33 UTC 2008


On Tue, Jan 08, 2008 at 11:42:29AM -0700, James William Pye wrote:
> + first() method (~ shorthand)

Figure I should explain this.

I find myself frequently needing to run a query where I'm only interested in the
first result. So, having an quick way to access this without all the extra
characters and objects is needed.

# Or Query(...).first()
>>> ~GTX.Query("SELECT version()")
u'PostgreSQL 8.2.5 on i386-apple-darwin8.10.1, compiled by GCC i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)'
>>> GTX.Query("SELECT version()")().next()[0]
u'PostgreSQL 8.2.5 on i386-apple-darwin8.10.1, compiled by GCC i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)'

Additionally, this saves you from having to handle StopIteration exceptions on
empty result sets.

This idiom does create some ambiguity; empty result sets return None and SELECT
NULL returns None. However, the convenience is well worth it, IMO.


More information about the Python-general mailing list