-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalue_manipulation_functions.py
More file actions
50 lines (36 loc) · 1.21 KB
/
value_manipulation_functions.py
File metadata and controls
50 lines (36 loc) · 1.21 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
"""
Functions for working with and manipulating values
"""
import logging
logger = logging.getLogger()
def clamp_value(value, min_value, max_value):
if min_value > max_value:
raise ValueError("min_value must be <= max_value")
original_value = value
# Inclusive clamp
value = min(max(value, min_value), max_value)
# Log only if clamped
if value != original_value:
logger.info(f"Clamped value from {original_value} to {value} (range: [{min_value}, {max_value}])")
return value
def to_bool(value):
"""
Convert a value to a boolean.
Acceptable truthy strings: "true", "yes", "1"
Acceptable falsy strings: "false", "no", "0"
Accepts int, float, bool directly.
Raises ValueError if the value cannot be interpreted as boolean.
"""
if isinstance(value, bool):
return value
if isinstance(value, (int, float)):
result = value != 0
return result
if isinstance(value, str):
val = value.strip().lower()
if val in {"true", "yes", "1"}:
return True
if val in {"false", "no", "0"}:
return False
# Could not interpret value
raise ValueError(f"Cannot convert {value!r} to bool")