[Python-ideas] bool.from_config_str()

Barry Warsaw barry at python.org
Mon Jun 13 05:32:37 EDT 2016


On Jun 12, 2016, at 07:52 PM, jab at math.brown.edu wrote:

>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

FWIW, in some projects I use lazr.config, which has (among other type
conversions) an as_boolean() method which does:

    value = value.lower()
    if value in ('true', 'yes', '1', 'on', 'enabled', 'enable'):
        return True
    if value in ('false', 'no', '0', 'off', 'disabled', 'disable'):
        return False
    raise ValueError('Invalid boolean value: %s' % value)

I understand the problem with adding this in a generic method on the bool
type, so I'm just mentioning this in case lazr.config.as_boolean() is useful
to you.

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160613/7d8d9e74/attachment-0001.sig>


More information about the Python-ideas mailing list