[PATCH] Re: ConfigParser.get() defaults?

Tim Chase python.list at tim.thechases.com
Sat May 8 09:52:27 EDT 2010


> On 05/07/2010 07:56 PM, Steven D'Aprano wrote:
>> On Fri, 07 May 2010 15:05:53 -0500, Tim Chase wrote:
>>> With a normal dictionary, I can specify a default fallback value in the
>>> event the requested key isn't present:
>> [...]
>>> However, with the ConfigParser object, there doesn't seem to be any way
>>> to do a similar
>>
>> Sounds like a nice feature to have. When you submit a patch (*grin*),
>
> I'm game to create and provide a patch,

Attached is my initial patch against 2.6's ConfigParser.py to 
work in some defaults (though it should also apply fairly cleanly 
against 2.5).

It doesn't differentiate between missing sections and missing 
options, as both cases are the same in my use:  the user didn't 
provide a setting, so I want to specify a default without lots of 
try/except wrapping.

For the type-specific get[int/float/boolean]() functions, it also 
provides an optional parameter to raise the ValueError or just 
return the default if it's provided-but-bogus (it may also yell 
at you if your default creates a ValueError).

My distributed ConfigParser.py didn't seem to have any tests, so 
I didn't include any; but would be glad to write a few if they'd 
be integrated into the standard core testing.

Is this the sort of things that should also be sent to 
python-dev, or are the powers-that-be lurking sufficiently on 
this list to see this patch fly?

-tkc



-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ConfigParser.diff
URL: <http://mail.python.org/pipermail/python-list/attachments/20100508/1465030a/attachment.ksh>


More information about the Python-list mailing list