[issue8666] Allow ConfigParser.get*() to take a default value

Tim Chase report at bugs.python.org
Mon May 10 20:16:38 CEST 2010


Tim Chase <python.list at tim.thechases.com> added the comment:

The "raise_on_bad" (I'm ambivalent on the name, but if you come up with a better name, I'd be fine with changing it) allows you to differentiate between an option that isn't provided, and one that is provided, but can't be converted to the specified int/float/boolean type.  E.g.

  [MySection]
  myFloat = hello

if you issue

  f = cp.getfloat("MySection", "myFloat", default=3.14, raise_on_bad=True)

it will raise a ValueError because "hello" can't be converted to a float.  However there are other times you want (well, other times *I've* wanted...most cases, in fact) to be able to specify that if there's ANY problem, just return the default:

  f = cp.getfloat("MySection", "myFloat", default=3.14, raise_on_bad=False)

returns f=3.14 (the default).  The only crazy side-exception I saw in the code is if you make a "dumb programmer" mistake of 

  f = cp.getfloat("MySection", "myFloat", default="foo", raise_on_bad=False)

it may still give you an error or unexpected non-float results because the default isn't what you asked for.

The ability to get a valid result back (regardless of section or option presence; or data errors) is most helpful when all you want is the answer to the question "did the user specify a valid value I can use? otherwise, just use the darn default".

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8666>
_______________________________________


More information about the Python-bugs-list mailing list