n00b question on spacing

Joshua Landau joshua.landau.ws at gmail.com
Sat Jun 22 09:36:43 EDT 2013


On 21 June 2013 23:26, Gary Herron <gherron at digipen.edu> wrote:
> On 06/21/2013 02:17 PM, Yves S. Garret wrote:
>> I have the following line of code:
>> log.msg("Item wrote to MongoDB database %s/%s" %(settings['MONGODB_DB'],
>> settings['MONGODB_COLLECTION']), level=log.DEBUG, spider=spider)
<...>
>> I was thinking of splitting it up like so:
>> log.msg("Item wrote to MongoDB database %s/%s"
>>   %(settings['MONGODB_DB'], settings['MONGODB_COLLECTION']),
>>   level=log.DEBUG, spider=spider)
>
> This is how I'd do it:  (And it's *FAR* clearer -- You win no points for
> clarity by having it all in one statement.)
>
> fmt  = "Item wrote to MongoDB database %s/%s"
> msg = fmt % (settings['MONGODB_DB'],
>                          settings['MONGODB_COLLECTION'])
> log.msg(msg, level=log.DEBUG, spider=spider)

Hear, Hear.

But really, you should be using .format by now :P

My favourite way would be along the lines of:

message = "Item wrote to MongoDB database "
message += "{0[MONGODB_DB]}/{0[MONGODB_COLLECTION]}".format(settings)
log.msg(message, level=log.DEBUG, spider=spider)



More information about the Python-list mailing list