[Python-ideas] bool.from_config_str()

jab at math.brown.edu jab at math.brown.edu
Sun Jun 12 19:52:28 EDT 2016


Dear Python-Ideas,

(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.

So I thought it might be nice if we could do something like this for
booleans, too:

Python 3.6.0a1+ (default:0b18f7d262cc+, Jun 12 2016, 18:21:54)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> bool.from_config_str('false')
False
>>> bool.from_config_str('False')
False
>>> bool.from_config_str('True')
True
>>> bool.from_config_str('true')
True
>>> bool.from_config_str('0')
False
>>> bool.from_config_str('')
False
>>> bool.from_config_str('1')
True


I seized the opportunity to work up my first (tiny) patch for CPython
which implements this, along with tests (please see attached patch,
which applies cleanly against current tip).

Is there any interest in this? If so, I'd be happy to make it work for
bytes too, or to make any other changes that would help get it landed.

Thanks for your consideration, and looking forward to your feedback.

Josh
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bool_from_config_str.patch
Type: application/octet-stream
Size: 3402 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160612/c68d50a8/attachment.obj>


More information about the Python-ideas mailing list