From: Kushal Kumaran on
On Thu, 2010-05-27 at 17:56 +0100, MRAB wrote:
> Kushal Kumaran wrote:
> [snip]
> > Since I'm in a good mood today, here's a little present:
> >
> > def insert(cursor, table, columns, values):
> > """Insert a row into a table. columns must be a list of column
> > names. values must be a list of values for the new row. The
> > columns and values must correspond."""
> > assert len(columns) == len(values)
> >
> > stmt = """
> > insert into %s (%s) values (%s)
> > """ % (table,
> > ', '.join(columns),
> > ', '.join('%s' * len(values)))
>
> That should be:
>
> ', '.join(['%s'] * len(values)))

My bad. Tested with sqlite using '?' as the placeholder, then simply
replaced with '%s'.

--
regards,
kushal


From: Tim Chase on
On 05/27/2010 11:56 AM, MRAB wrote:
> Kushal Kumaran wrote:
>> ', '.join('%s' * len(values)))
>
> That should be:
>
> ', '.join(['%s'] * len(values)))

Or as I've done in the past:

', '.join('%s' for _ in values)

-tkc



From: Tim Chase on
On 05/27/2010 03:32 PM, Victor Subervi wrote:
> On Thu, May 27, 2010 at 1:15 PM, Tim Chase wrote:
>>> That should be:
>>>
>>> ', '.join(['%s'] * len(values)))
>>
>> Or as I've done in the past:
>>
>> ', '.join('%s' for _ in values)
>
> Huh? Can you describe that underscore to me? Fascinating!

The underscore is a valid variable-name, idiomatically used for
"I don't care about this", often seen in places like tuple
assignment:

a,_,_,d = some_4_element_tuple

So in my above case, it could also have been written as

', '.join('%s' for value in values)
', '.join('%s' for dont_care_about_the_value in values)

but the "_" idiom means "I'm iterating over 'values' but I'm not
actually using the values I get from it"

-tkc




From: Malcolm Greene on
Tim,

> The underscore is a valid variable-name, idiomatically used for "I don't care about this", often seen in places like tuple assignment:

The underscore is also used as an alias for gettext.gettext or
gettext.ugettext so you may want to use another variable-name.

Malcolm