-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbools.py
More file actions
152 lines (122 loc) · 5.75 KB
/
bools.py
File metadata and controls
152 lines (122 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# ============================================================================ #
# #
# Title : Bools #
# Purpose : Manipulate and enhance booleans. #
# #
# ============================================================================ #
# ---------------------------------------------------------------------------- #
# #
# Overview ####
# #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Description ####
# ---------------------------------------------------------------------------- #
"""
!!! note "Summary"
The `bools` module is used how to manipulate and enhance Python booleans.
!!! abstract "Details"
Primarily, this module is used to store the `strtobool()` function, which used to be found in the `distutils.util` module, until it was deprecated. As mentioned in [PEP632](https://peps.python.org/pep-0632/#migration-advice), we should re-implement this function in our own code. And that's what we've done here.
"""
# ---------------------------------------------------------------------------- #
# #
# Setup ####
# #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Exports ####
# ---------------------------------------------------------------------------- #
__all__: list[str] = ["strtobool", "STR_TO_BOOL_MAP"]
# ---------------------------------------------------------------------------- #
# Constants ####
# ---------------------------------------------------------------------------- #
STR_TO_BOOL_MAP: dict[str, bool] = {
"y": True,
"yes": True,
"t": True,
"true": True,
"on": True,
"1": True,
"n": False,
"no": False,
"f": False,
"false": False,
"off": False,
"0": False,
}
"""
Summary:
Map of string values to their corresponding boolean values.
"""
# ---------------------------------------------------------------------------- #
# #
# Functions ####
# #
# ---------------------------------------------------------------------------- #
def strtobool(value: str) -> bool:
"""
!!! note "Summary"
Convert a `#!py str` value in to a `#!py bool` value.
???+ abstract "Details"
This process is necessary because the `distutils` module was completely deprecated in Python 3.12.
Params:
value (str):
The string value to convert. Valid input options are defined in [`STR_TO_BOOL_MAP`][toolbox_python.bools.STR_TO_BOOL_MAP]
Raises:
(ValueError):
If the value parse'ed in to `value` is not a valid value to be able to convert to a `#!py bool` value.
Returns:
(bool):
A `#!py True` or `#!py False` value, having successfully converted `value`.
???+ example "Examples"
```pycon {.py .python linenums="1" title="Set up"}
from toolbox_python.bools import strtobool
```
```pycon {.py .python linenums="1" title="Example 1: `true` conversions"}
>>> print(strtobool("true"))
>>> print(strtobool("t"))
>>> print(strtobool("1"))
```
<div class="result" markdown>
```{.sh .shell title="Terminal"}
True
True
True
```
!!! success "Conclusion: Successful conversion."
</div>
```pycon {.py .python linenums="1" title="Example 2: `false` conversions"}
>>> print(strtobool("false"))
>>> print(strtobool("f"))
>>> print(strtobool("0"))
```
<div class="result" markdown>
```{.sh .shell title="Terminal"}
False
False
False
```
!!! success "Conclusion: Successful conversion."
</div>
```pycon {.py .python linenums="1" title="Example 3: invalid value"}
>>> print(strtobool(5))
```
<div class="result" markdown>
```{.sh .shell title="Terminal"}
ValueError: Invalid bool value: '5'.
For `True`, must be one of: ['y', 'yes', 't', 'true', 'on', '1']
For `False`, must be one of: ['n', 'no', 'f', 'false', 'off', '0']
```
!!! failure "Conclusion: Invalid type."
</div>
??? question "References"
- [PEP632](https://peps.python.org/pep-0632/#migration-advice)
"""
try:
return STR_TO_BOOL_MAP[str(value).lower()]
except KeyError as exc:
raise ValueError(
f"Invalid bool value: '{value}'.\n"
f"For `True`, must be one of: {[key for key, val in STR_TO_BOOL_MAP.items() if val]}\n"
f"For `False`, must be one of: {[key for key, val in STR_TO_BOOL_MAP.items() if not val]}"
) from exc