From 1fe0df71ba3dc0fad347af3bde912fb17380a2f5 Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Tue, 27 Nov 2018 16:59:56 -0800 Subject: [PATCH 1/3] [ENH] Improve ``str2bool`` + doctests While addressing other issues I had to write some improvements to this little utility. I thought it was worth a separate, quick PR --- nipype/utils/misc.py | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/nipype/utils/misc.py b/nipype/utils/misc.py index f73443b348..4cb3632471 100644 --- a/nipype/utils/misc.py +++ b/nipype/utils/misc.py @@ -5,7 +5,7 @@ """ from __future__ import (print_function, unicode_literals, division, absolute_import) -from builtins import next, str +from builtins import next, bytes, str import os import sys @@ -192,15 +192,42 @@ def package_check(pkg_name, def str2bool(v): + """ + Convert strings (and bytearrays) to boolean values + + >>> all([str2bool(v) for v in (True, "yes", "true", + ... "t", "Yes", "True", "1", "on", "On")]) + True + >>> all([str2bool(v.encode('utf-8')) + ... for v in ("yes", "true", "t", "1", "Yes", "on", "On")]) + True + >>> any([str2bool(v) for v in (False, "no", "false", "n", "f", + ... "False", "0", "off", "Off")]) + False + >>> any([str2bool(v.encode('utf-8')) + ... for v in ("no", "false", "n", "f", "0", "off", "Off")]) + False + >>> any([str2bool(v) for v in (None, '/some/path', 'Agg', 'INFO', + ... '/some/bytes/path'.encode('utf-8'))]) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: ... + + """ if isinstance(v, bool): return v - lower = v.lower() - if lower in ("yes", "true", "t", "1"): - return True - elif lower in ("no", "false", "n", "f", "0"): - return False - else: - raise ValueError("%s cannot be converted to bool" % v) + + if isinstance(v, bytes): + v = v.decode('utf-8') + + if isinstance(v, str): + lower = v.lower() + if lower in ("yes", "true", "t", "1", "on"): + return True + elif lower in ("no", "false", "n", "f", "0", "off"): + return False + + raise ValueError("%r cannot be converted to bool" % v) def flatten(S): From 4eee6b7834bb11ba18ec0b234234515b14991e71 Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Tue, 27 Nov 2018 18:08:31 -0800 Subject: [PATCH 2/3] split bad doctest (it was evaluating only the first value) --- nipype/utils/misc.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/nipype/utils/misc.py b/nipype/utils/misc.py index 4cb3632471..9d9140b3ac 100644 --- a/nipype/utils/misc.py +++ b/nipype/utils/misc.py @@ -207,8 +207,23 @@ def str2bool(v): >>> any([str2bool(v.encode('utf-8')) ... for v in ("no", "false", "n", "f", "0", "off", "Off")]) False - >>> any([str2bool(v) for v in (None, '/some/path', 'Agg', 'INFO', - ... '/some/bytes/path'.encode('utf-8'))]) # doctest: +ELLIPSIS + >>> str2bool(None) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: ... + >>> str2bool('/some/path') # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: ... + >>> str2bool('Agg') # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: ... + >>> str2bool('INFO') # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: ... + >>> str2bool('/some/bytes/path'.encode('utf-8')) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: ... From 787f4fddc02c481580e4721a3e2de603f7eb6940 Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Tue, 27 Nov 2018 22:06:38 -0800 Subject: [PATCH 3/3] add "y" as possible value --- nipype/utils/misc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nipype/utils/misc.py b/nipype/utils/misc.py index 9d9140b3ac..61730025ff 100644 --- a/nipype/utils/misc.py +++ b/nipype/utils/misc.py @@ -196,10 +196,10 @@ def str2bool(v): Convert strings (and bytearrays) to boolean values >>> all([str2bool(v) for v in (True, "yes", "true", - ... "t", "Yes", "True", "1", "on", "On")]) + ... "y", "t", "Yes", "True", "1", "on", "On")]) True >>> all([str2bool(v.encode('utf-8')) - ... for v in ("yes", "true", "t", "1", "Yes", "on", "On")]) + ... for v in ("yes", "true", "y", "t", "1", "Yes", "on", "On")]) True >>> any([str2bool(v) for v in (False, "no", "false", "n", "f", ... "False", "0", "off", "Off")]) @@ -237,7 +237,7 @@ def str2bool(v): if isinstance(v, str): lower = v.lower() - if lower in ("yes", "true", "t", "1", "on"): + if lower in ("yes", "true", "y", "t", "1", "on"): return True elif lower in ("no", "false", "n", "f", "0", "off"): return False