[Python-3000] Changing the % format return type Re: features i'd like [Python 3000?] ... #4: interpolated strings ala perl

Josiah Carlson jcarlson at uci.edu
Wed Dec 6 17:33:27 CET 2006


Jan Grant <jan.grant at bristol.ac.uk> wrote:
> On Wed, 6 Dec 2006, Jan Grant wrote:
> 
> > def foo(d: sqlDriver):
> >   s = "select template goes here" % (param1, param2)
> >   r = d.query(s)	# do the Right Thing
> 
> Suppose for a moment that % used like this returns, not a string, but an 
> object that encapsulates the format and the parameter set supplied.
> 
> Then the sqlDriver implementation could take advantage of Phillip's GF 
> mechanisms thus:
> 
> class driver 	# which implements sqlDriver
>   def query(self, s: str):
>     ... code here to perform a query based on a stock string ...
> 
>   def query(self, pf: parameterised_format):
>     ... code here to extract the format string and quote the
>     ... parameters appropriately within it.

At least in the case of the few SQL drivers I've used in the past couple
years, usually there is something like the following, which
automatically handles escaping as necessary...

    d.query("UPDATE table SET value = ? WHERE rowid = ?", val, row)

If your SQL driver of choice in any language you use does not have a
mechanism similar to the above, then it needs a feature addition.  As
such, I'm not sure that this particular problem (or its solution) is
necessary for Python.  Do like the SQL people have been doing for years,
pass *both* parts to your query/format function.


 - Josiah



More information about the Python-3000 mailing list