[DB-SIG] format vs pyformat

Chris Cogdon chris@cogdon.org
Mon, 19 Aug 2002 14:15:24 -0700

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=20
dictionary already. Yes, this can be reasonably easy to set up. Viz:

cur.execute ( "select * from people where name ilike %(name)s and=20
=09{ '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",
=09criteria.name, criteria.age )

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

cur.execute ( "select * from people where name ilike %(name)s and=20
=09criteria.__dict__ )

But this assumes that the values to be passed are already pre-calculated,=
you'll need to find 'temporary' space for them so you can throw them into=
dictionary. pyformat also allows reuse of parameters, but I've seen this =
too seldom to make it a big plus.

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

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

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

Thanks in advance.

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