Support for numeric paramstyle

Skip to first unread message

Cédric Krier

Jun 1, 2015, 7:20:02 AM6/1/15
to python-sql

I need to have support for numeric paramstyle [1].
It is one of the three format that should be prefered as said the
PEP0249. The two others are named and pyformat but they are really not
logical for a library like python-sql as we will have to guess the name.

But to implement numeric, it is quite tricky because we don't know when
to start counting the parameters because queries can be inside other
queries. So the logical counting should be done outside the string
generation. I see two options:

* Having an explicit counter to reset explicitly before stringify
the main query.
This will look like this:

query =
Flavor.get().reset() # reset the param counter

Pros: explicit, doesn't add new loop
Cons: new API, new method call, wierd error if reset is forgetten
(but could be converted as context manager)

* Or using the 'format' style and having a method that takes the
query tuple and convert the '%s' into ':i'.
This will look like this:

query =

and numberify will do something like:

def numberify(query, params):
return query % [':%s' % i for i, _ in enumerate(params)]

Pros: not intrusif
Cons: not explicit, could lead to formatting error

What do you think? Have you any other ideas?

Cédric Krier - B2CK SPRL
Tel: +32 472 54 46 59

Cédric Krier

Jun 2, 2015, 4:25:03 AM6/2/15
to python-sql
Here is the implementation of the converter:

It is not yet set if this will be the choosen solution but at least we
have an implementation example.
Reply all
Reply to author
0 new messages