[Python-ideas] bool.from_config_str()

Marcin Sztolcman marcin at urzenia.net
Mon Jun 13 03:27:21 EDT 2016


On Mon, Jun 13, 2016 at 1:52 AM,  <jab at math.brown.edu> wrote:
> (New here... hope this idea contributes something!)
>
> Several times I've had to implement a silly function that converts the
> odd environment variable (or some other value from the outside world
> that, perforce, comes in as a string) to the boolean it actually
> represents.
>
> I say "silly" because, for other commonly-needed types of primitive
> values, we have concise, idiomatic ways to convert them from strings,
> leaving no need for wheel reinvention:
>
> int(os.getenv('NUM_FJORDS'))
> float(os.getenv('PRICE_LIMBURGER'))
> etc.

My proposal I use everyday:

foo = os.environ.get('FOO', DEFAULT_FOO).lower() in ('1', 'true', 'yes')

It's easy, concise and powerful, allow to specify what do you think is
true in that case.

-- 
Marcin Sztolcman :: http://urzenia.net/ :: http://sztolcman.eu/


More information about the Python-ideas mailing list