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
participants (12)
-
Bar Harel
-
Barry Warsaw
-
Ben Finney
-
Bernardo Sulzbach
-
jab@math.brown.edu
-
Marcin Sztolcman
-
Michael Selik
-
Pavol Lisy
-
Stephen J. Turnbull
-
Steven D'Aprano
-
Terry Reedy
-
Łukasz Langa