n00b question on spacing

Joshua Landau joshua.landau.ws at gmail.com
Sat Jun 22 14:20:22 EDT 2013


On 22 June 2013 18:28, Alister <alister.ware at ntlworld.com> wrote:
> On Sat, 22 Jun 2013 17:11:00 +0100, Joshua Landau wrote:
>
>> On 22 June 2013 16:55, Rick Johnson <rantingrickjohnson at gmail.com>
>> wrote:
>>> On Saturday, June 22, 2013 10:40:24 AM UTC-5, Joshua Landau wrote:
>>>> > Plus, your use of the format syntax is incorrect.
>>>> Wut?
>>>
>>> Well what i mean exactly is not that it's illegal, i just find the use
>>> of the "getattr sugar", from WITHIN the format string, to be
>>> excessively noisy.
>>
>> Sure...
>> So in other words you realised you were wrong but you're too scared to
>> admit it. Eh? That's it, isn't it! You just don't want to loosen your
>> proud persona :P.
>
> In this argument I tend to find myself siding with Rick.
> Considering his general reputation in this forum am I missing something
> or do I need help? ;-)

I wasn't mocking the preference against it, but rather that that was
completely not what he said originally. One cannot let slip when the
mighty Rick misses his mark.

That said, yes, it is quite a noisy construct. I still prefer it to:

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

Because this feels undescriptive - the first and "second" lines feel
detached. Instead of Rick's hilarious unpacking, you could always do:

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

Which is probably as good, if longer. You'll see the real problem is
that "MONDODB_DB" and "MONGODB_COLLECTION" are awful names; would you
really have so detested

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

or even, now,

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

?

I think those options are quite nice, á mon avis. I could even write a
wrapper (oh noes!):

# Shortened for EMail
class SettingsWrapper:
    def uglify_key(self, key): return key if key.startswith("MONGODB")
else "MONGODB_" + key.upper()
    def __init__(self, settingsdict): self.wrapped = settingsdict
    def __len__(self): return self.wrapped.__len__()
    def __iter__(self): return self.wrapped.__iter__()
    def __getitem__(self, key): return self.wrapped[self.uglify_key(key)]
    def __contains__(self, key): return self.uglify_key(key) in self.wrapped
    def __setitem__(self, key, value):
self.wrapped[self.uglify_key(key)] = value
    def __delitem__(self, key, value): del self.wrapped[self.uglify_key(key)]

settings = SettingsWrapper(settings)

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



More information about the Python-list mailing list