Skip to content

Commit 13c6612

Browse files
committed
Added utilities function
1 parent f265ae8 commit 13c6612

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

dpytools/utilities/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Utility Functions
2+
3+
## str_to_bool
4+
5+
The `str_to_bool` function is a utility function that converts a string representation of a boolean to a boolean value.
6+
7+
### Usage
8+
9+
```python
10+
from utilities import str_to_bool
11+
12+
print(str_to_bool('True')) # Outputs: True
13+
print(str_to_bool('false')) # Outputs: False
14+
15+
```
16+
17+
#### Parameters
18+
`should_be_bool` (str): A string that should represent a boolean value.
19+
20+
#### Returns
21+
`bool`: The boolean value represented by the input string.
22+
23+
#### Raises
24+
`AssertionError`: If the input value is not a string.
25+
`ValueError`: If the input string does not represent a boolean value.
26+
27+
#### Example
28+
29+
```python
30+
try:
31+
print(str_to_bool('not a boolean')) # Raises ValueError
32+
except ValueError as e:
33+
print(e) # Outputs: A str value representing a boolean should be one of 'True', 'true', 'False', 'false'. Got 'not a boolean'
34+
```

dpytools/utilities/utilities.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def str_to_bool(should_be_bool: str) -> bool:
2+
"""
3+
Take a string that should represent a boolean
4+
and convert it to bool.
5+
6+
Raise if we've an unexpected value.
7+
"""
8+
9+
assert isinstance(
10+
should_be_bool, str
11+
), f"Function str_to_bool only accepts strings, got {type(should_be_bool)}"
12+
13+
consistent_should_be_bool = should_be_bool.strip().lower()
14+
15+
if consistent_should_be_bool == "true":
16+
return True
17+
elif consistent_should_be_bool == "false":
18+
return False
19+
else:
20+
raise ValueError(
21+
f"A str value representing a boolean should be one of 'True', 'true', 'False', 'false'. Got '{should_be_bool}'"
22+
)
23+
24+

tests/test_utilities.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
3+
from dpytools.utilities.utilities import str_to_bool
4+
5+
6+
def test_str_to_bool_valid_values():
7+
"""
8+
Test str_to_bool utility can correctly identify the expected
9+
boolean values from strings.
10+
"""
11+
12+
assert str_to_bool("True") is True
13+
assert str_to_bool("true") is True
14+
assert str_to_bool(" True ") is True
15+
assert str_to_bool("False") is False
16+
assert str_to_bool("false") is False
17+
assert str_to_bool("false ") is False
18+
19+
20+
def test_str_to_bool_valid_raises_for_invalid_str_value():
21+
"""
22+
Test str_to_bool utility will raise when given a non
23+
expected string value
24+
"""
25+
26+
with pytest.raises(ValueError) as err:
27+
str_to_bool("foo")
28+
29+
assert (
30+
"A str value representing a boolean should be one of 'True', 'true', 'False', 'false'"
31+
in str(err)
32+
)
33+
34+
35+
def test_str_to_bool_raises_for_not_string_argument():
36+
"""
37+
Test str_to_bool utility will raise when given a non
38+
string argument
39+
"""
40+
41+
for invalid_type in [1, True, 897.23]:
42+
43+
with pytest.raises(AssertionError) as err:
44+
str_to_bool(invalid_type)
45+
46+
assert "Function str_to_bool only accepts strings" in str(err)

0 commit comments

Comments
 (0)