[DB-SIG] format vs pyformat

Keating, Tim TKeating@origin.ea.com
Mon, 19 Aug 2002 16:33:39 -0500


For that matter (is it vain to reply to yourself?), I'm pretty sure you
could just implement __getattr__() for the criteria class type in the
example below, then pass the critera class instance as the formatting
argument. That solves your issue with "dictionary entries" being
pre-calculated.

-----Original Message-----
From: Keating, Tim [mailto:TKeating@origin.ea.com] 
Sent: Monday, August 19, 2002 4:24 PM
To: 'Chris Cogdon'; db-sig@python.org
Subject: RE: [DB-SIG] format vs pyformat


Am I missing something? Aren't you constructing a string and passing it in
either way? Why would the database adapter care how it was formatted?

In any case, dictionary-based formatting isn't nearly as ugly if you already
have the data in a dictionary and don't have to construct an anonymous one
to pass to the format operator. This might seem like a pain, but I suggest
you look at the vars() built-in function and consider the possibilities . .
. 

-----Original Message-----
From: Chris Cogdon [mailto:chris@cogdon.org] 
Sent: Monday, August 19, 2002 4:15 PM
To: db-sig@python.org
Subject: [DB-SIG] format vs pyformat


In all my database coding to date, I've been using the 'format' parameter 
passing protocol to get my stuff into the database query. However, I keep 
reading and hearing that 'pyformat' is by far superior. However, whenever 
I've 'given it a go', it's always seemed far clunkier to me.

For example, for pyformat to work, all your variables have to be in a 
dictionary already. Yes, this can be reasonably easy to set up. Viz:

cur.execute ( "select * from people where name ilike %(name)s and 
age>%(age)s",
	{ 'name':criteria.name, 'age':criteria.age } )

But I still find it much easier to use the positional parameters. Viz:

cur.execute ( "select * from people where name ilike %s and age > %s",
	criteria.name, criteria.age )

The above is a simple example... the disparity between the two protocols 
becomes greater with greater complexity. I understand it's possible to pass
a 
dictionary from a pre-existing namespace. Viz:

cur.execute ( "select * from people where name ilike %(name)s and 
age>%(age)s",
	criteria.__dict__ )

But this assumes that the values to be passed are already pre-calculated, or

you'll need to find 'temporary' space for them so you can throw them into
the 
dictionary. pyformat also allows reuse of parameters, but I've seen this
used 
too seldom to make it a big plus.

To me, 'format' parameter passing still seems a lot easier to use, but
there's 
quite a few DB connectors that support 'pyformat' over 'format': For
example, 
PoPy only supports pyformat, while pyPgSQL supports both format and
pyformat.

Am I completely missing some 'neat trick' here that would make pyformat 
magically easy? 

What are other people's views on the ease of using the two formats ?

Thanks in advance.

-- 
   ("`-/")_.-'"``-._        Chris Cogdon <chris@cogdon.org>
    . . `; -._    )-;-,_`)
   (v_,)'  _  )`-.\  ``-'
  _.- _..-_/ / ((.'
((,.-'   ((,/   fL

_______________________________________________
DB-SIG maillist  -  DB-SIG@python.org
http://mail.python.org/mailman/listinfo/db-sig

_______________________________________________
DB-SIG maillist  -  DB-SIG@python.org
http://mail.python.org/mailman/listinfo/db-sig