Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
wipac_dev_tools.strtobool()
[minor] (#40)
Co-authored-by: github-actions <github-actions@github.com>
- Loading branch information
Showing
6 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Test strtobool.py.""" | ||
|
||
|
||
from random import choice | ||
|
||
import pytest | ||
from wipac_dev_tools import strtobool | ||
|
||
|
||
def random_case(string: str) -> str: | ||
"""Randomly capitalize string.""" | ||
return "".join(choice((str.upper, str.lower))(c) for c in string) | ||
|
||
|
||
def test_true() -> None: | ||
"""Test True cases""" | ||
strings = ["y", "yes", "t", "true", "on", "1"] | ||
strings.extend([s.upper() for s in strings]) | ||
strings.extend([random_case(s) for s in strings]) # yes, this doubles everything | ||
|
||
for string in strings: | ||
val = strtobool(string) | ||
assert isinstance(val, bool) | ||
assert val | ||
|
||
|
||
def test_false() -> None: | ||
"""Test False cases.""" | ||
strings = ["n", "no", "f", "false", "off", "0"] | ||
strings.extend([s.upper() for s in strings]) | ||
strings.extend([random_case(s) for s in strings]) # yes, this doubles everything | ||
|
||
for string in strings: | ||
val = strtobool(string) | ||
assert isinstance(val, bool) | ||
assert not val | ||
|
||
|
||
def test_value_error() -> None: | ||
"""Test error cases""" | ||
strings = ["nah", "yup", "", "hmm", "maybe", "offf", "123"] | ||
strings.extend([s.upper() for s in strings]) | ||
strings.extend([random_case(s) for s in strings]) # yes, this doubles everything | ||
|
||
for string in strings: | ||
with pytest.raises(ValueError, match=rf"Invalid truth value: {string}"): | ||
strtobool(string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""The useful `strtobool()`, originally from the deprecated `distutils.util`.""" | ||
|
||
|
||
def strtobool(string: str) -> bool: | ||
"""Smart-cast a string to a bool using common-sense interpretations. | ||
Unlike the since deprecated `distutils.util.strtobool`, this | ||
returns an actual bool. | ||
True: 'y', 'yes', 't', 'true', 'on', '1' | ||
False: 'n', 'no', 'f', 'false', 'off', '0' | ||
Raises: | ||
ValueError: if the string does not match any of the about | ||
""" | ||
if string.lower() in ("y", "yes", "t", "true", "on", "1"): | ||
return True | ||
elif string.lower() in ("n", "no", "f", "false", "off", "0"): | ||
return False | ||
else: | ||
raise ValueError(f"Invalid truth value: {string}") |