Issue 25243: decouple string-to-boolean logic from ConfigParser.getboolean and offer as separate function (original) (raw)

Created on 2015-09-26 22:40 by jab, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (7)

msg251664 - (view)

Author: Joshua Bronson (jab) *

Date: 2015-09-26 22:40

ConfigParser.getboolean[1] has logic to convert strings like '0' and 'False' to False. This logic is generally useful in other contexts and need not be coupled to ConfigParser. Would you consider accepting a patch that factored this string-to-boolean logic out of ConfigParser into a standalone function, and changed ConfigParser.getboolean internally to call that?

Thanks for your consideration.

[1] https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.getboolean

msg251666 - (view)

Author: Joshua Bronson (jab) *

Date: 2015-09-26 22:47

One way this could be offered is as a new static method on bool (something like bool.parse_str?), but I of course defer to the better judgment of the Python core developers. I'd be happy to take a crack at a patch adding it wherever you like, if you like.

msg253886 - (view)

Author: Joshua Bronson (jab) *

Date: 2015-11-01 23:50

My friend @novalis_dt and I worked up a patch for this including tests (attached). First time working with the CPython codebase but hope it's a reasonable start.

Here's a preview:

~> ./python.exe Python 3.6.0a0 (default:9f8b5053e30d+, Nov 1 2015, 18:38:37) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin Type "help", "copyright", "credits" or "license" for more information.

trues = ['true', 'yes', 'on', '1'] [bool.from_config_str(i) for i in trues] [True, True, True, True] falses = ['false', 'no', 'off', '0', ''] [bool.from_config_str(i) for i in falses] [False, False, False, False, False]

I would be happy to try to make any additional changes necessary (including changing ConfigParser.getboolean to use the new method) if there is interest.

Thanks!

msg263639 - (view)

Author: Raymond Hettinger (rhettinger) * (Python committer)

Date: 2016-04-18 04:27

I'm not sure I see the point in moving this out to a separate method (i.e. I wouldn't want to see people importing configparser just to use this particular group of boolean word aliases). In general, users would be better served by a simple dictionary that gives them the ability to say exactly which variants they want to allow ('y', 'n', 'enabled', 'disabled', etc).

Also, the preposition "from" in a method name usually indicates a classmethod that creates instances of the class where it is defined.

msg264387 - (view)

Author: Joshua Bronson (jab) *

Date: 2016-04-27 18:07

Hi Raymond, I'm a bit confused by your comment. The patch I attached to my previous message adds the static method bool.parse_config_str. So there's no need to import configparser to use this, and "from" is no longer included in the proposed method name. Could you please take a look at my previous message, if you missed it, and reopen this if appropriate?

msg264389 - (view)

Author: Joshua Bronson (jab) *

Date: 2016-04-27 18:10

Actually, looks like the version of the patch I attached did use the name bool.from_config_str, sorry about that -- I'll attach a new patch renaming this to bool.parse_config_str if there is interest in further consideration.

msg264390 - (view)

Author: Joshua Bronson (jab) *

Date: 2016-04-27 18:13

Though come to think of it, the issue you raised with using "from" in the method name wouldn't apply here, since the static method is on the bool class, and the method does return bool.

History

Date

User

Action

Args

2022-04-11 14:58:21

admin

set

github: 69430

2016-04-27 18:13:15

jab

set

messages: +

2016-04-27 18:10:54

jab

set

messages: +

2016-04-27 18:07:34

jab

set

messages: +

2016-04-26 06:14:15

rhettinger

set

status: open -> closed
resolution: rejected

2016-04-18 04:27:39

rhettinger

set

assignee: rhettinger

messages: +
nosy: + rhettinger

2015-11-01 23:50:21

jab

set

files: + bool_from_config_str.patch

nosy: + novalis_dt
messages: +

keywords: + patch

2015-09-27 07:24:44

berker.peksag

set

nosy: + lukasz.langa

2015-09-26 22:47:15

jab

set

messages: +

2015-09-26 22:40:20

jab

create