Skip to content

Commit 5296952

Browse files
committed
fix: Restore validation helper functions accidentally deleted in upstream main
1 parent 0a4f9fb commit 5296952

1 file changed

Lines changed: 166 additions & 67 deletions

File tree

utils/validation.py

Lines changed: 166 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,166 @@
1-
from typing import List, Optional
2-
3-
def get_int(
4-
prompt: str,
5-
min_value: Optional[int] = None,
6-
max_value: Optional[int] = None,
7-
default: Optional[int] = None,
8-
error_empty: str = "❌ Error: Input cannot be empty.",
9-
error_invalid: str = "❌ Invalid input. Please enter a valid integer.",
10-
) -> int:
11-
while True:
12-
try:
13-
val_str = input(prompt).strip()
14-
if not val_str:
15-
if default is not None:
16-
if min_value is not None and default < min_value:
17-
print(f"❌ Default {default} is below minimum {min_value}.")
18-
continue
19-
if max_value is not None and default > max_value:
20-
print(f"❌ Default {default} is above maximum {max_value}.")
21-
continue
22-
return default
23-
print(error_empty)
24-
continue
25-
val = int(val_str)
26-
if min_value is not None and val < min_value:
27-
print(f"❌ Please enter a number greater than or equal to {min_value}.")
28-
continue
29-
if max_value is not None and val > max_value:
30-
print(f"❌ Please enter a number less than or equal to {max_value}.")
31-
continue
32-
return val
33-
except ValueError:
34-
print(error_invalid)
35-
36-
def get_float(
37-
prompt: str,
38-
min_value: Optional[float] = None,
39-
max_value: Optional[float] = None,
40-
default: Optional[float] = None,
41-
error_empty: str = "❌ Error: Input cannot be empty.",
42-
error_invalid: str = "❌ Invalid input. Please enter a valid number.",
43-
) -> float:
44-
while True:
45-
try:
46-
val_str = input(prompt).strip()
47-
if not val_str:
48-
if default is not None:
49-
if min_value is not None and default < min_value:
50-
print(f"❌ Default {default} is below minimum {min_value}.")
51-
continue
52-
if max_value is not None and default > max_value:
53-
print(f"❌ Default {default} is above maximum {max_value}.")
54-
continue
55-
return default
56-
print(error_empty)
57-
continue
58-
val = float(val_str)
59-
if min_value is not None and val < min_value:
60-
print(f"❌ Please enter a number greater than or equal to {min_value}.")
61-
continue
62-
if max_value is not None and val > max_value:
63-
print(f"❌ Please enter a number less than or equal to {max_value}.")
64-
continue
65-
return val
66-
except ValueError:
67-
print(error_invalid)
1+
from typing import List, Optional
2+
3+
def get_int(
4+
prompt: str,
5+
min_value: Optional[int] = None,
6+
max_value: Optional[int] = None,
7+
default: Optional[int] = None,
8+
error_empty: str = "❌ Error: Input cannot be empty.",
9+
error_invalid: str = "❌ Invalid input. Please enter a valid integer.",
10+
) -> int:
11+
while True:
12+
try:
13+
val_str = input(prompt).strip()
14+
if not val_str:
15+
if default is not None:
16+
if min_value is not None and default < min_value:
17+
print(f"❌ Default {default} is below minimum {min_value}.")
18+
continue
19+
if max_value is not None and default > max_value:
20+
print(f"❌ Default {default} is above maximum {max_value}.")
21+
continue
22+
return default
23+
print(error_empty)
24+
continue
25+
val = int(val_str)
26+
if min_value is not None and val < min_value:
27+
print(f"❌ Please enter a number greater than or equal to {min_value}.")
28+
continue
29+
if max_value is not None and val > max_value:
30+
print(f"❌ Please enter a number less than or equal to {max_value}.")
31+
continue
32+
return val
33+
except ValueError:
34+
print(error_invalid)
35+
36+
def get_float(
37+
prompt: str,
38+
min_value: Optional[float] = None,
39+
max_value: Optional[float] = None,
40+
default: Optional[float] = None,
41+
error_empty: str = "❌ Error: Input cannot be empty.",
42+
error_invalid: str = "❌ Invalid input. Please enter a valid number.",
43+
) -> float:
44+
while True:
45+
try:
46+
val_str = input(prompt).strip()
47+
if not val_str:
48+
if default is not None:
49+
if min_value is not None and default < min_value:
50+
print(f"❌ Default {default} is below minimum {min_value}.")
51+
continue
52+
if max_value is not None and default > max_value:
53+
print(f"❌ Default {default} is above maximum {max_value}.")
54+
continue
55+
return default
56+
print(error_empty)
57+
continue
58+
val = float(val_str)
59+
if min_value is not None and val < min_value:
60+
print(f"❌ Please enter a number greater than or equal to {min_value}.")
61+
continue
62+
if max_value is not None and val > max_value:
63+
print(f"❌ Please enter a number less than or equal to {max_value}.")
64+
continue
65+
return val
66+
except ValueError:
67+
print(error_invalid)
68+
69+
def get_non_empty_string(
70+
prompt: str,
71+
default: Optional[str] = None,
72+
error_empty: str = "❌ Error: Input cannot be empty.",
73+
) -> str:
74+
while True:
75+
val_str = input(prompt).strip()
76+
if not val_str:
77+
if default is not None:
78+
return default
79+
print(error_empty)
80+
continue
81+
return val_str
82+
83+
def get_choice(
84+
prompt: str,
85+
choices: List[str],
86+
default: Optional[str] = None,
87+
error_empty: str = "❌ Error: Input cannot be empty.",
88+
error_invalid: Optional[str] = None,
89+
) -> str:
90+
choices_lower = [c.lower() for c in choices]
91+
while True:
92+
val_str = input(prompt).strip()
93+
if not val_str:
94+
if default is not None:
95+
return default
96+
print(error_empty)
97+
continue
98+
if val_str.lower() in choices_lower:
99+
idx = choices_lower.index(val_str.lower())
100+
return choices[idx]
101+
if error_invalid is not None:
102+
print(error_invalid)
103+
else:
104+
print(f"❌ Invalid selection. Please choose from: {', '.join(choices)}")
105+
106+
def get_yes_no(prompt: str, default: Optional[str] = None) -> bool:
107+
while True:
108+
val_str = input(prompt).strip().lower()
109+
if not val_str:
110+
if default is not None:
111+
return default.lower() in ['y', 'yes']
112+
print("❌ Error: Input cannot be empty. Please enter 'y' or 'n'.")
113+
continue
114+
if val_str in ['y', 'yes']:
115+
return True
116+
if val_str in ['n', 'no']:
117+
return False
118+
print("❌ Invalid choice. Please enter 'y' or 'n'.")
119+
120+
def get_int_list(
121+
prompt: str,
122+
min_len: Optional[int] = None,
123+
max_len: Optional[int] = None,
124+
error_empty: str = "❌ Error: Input cannot be empty.",
125+
error_invalid: str = "❌ Error: Please enter valid integers only.",
126+
) -> List[int]:
127+
while True:
128+
val_str = input(prompt).strip()
129+
if not val_str:
130+
print(error_empty)
131+
continue
132+
try:
133+
val_list = [int(x) for x in val_str.split()]
134+
if min_len is not None and len(val_list) < min_len:
135+
print(f"❌ Error: Please enter at least {min_len} numbers.")
136+
continue
137+
if max_len is not None and len(val_list) > max_len:
138+
print(f"❌ Error: Please enter at most {max_len} numbers.")
139+
continue
140+
return val_list
141+
except ValueError:
142+
print(error_invalid)
143+
144+
def get_float_list(
145+
prompt: str,
146+
min_len: Optional[int] = None,
147+
max_len: Optional[int] = None,
148+
error_empty: str = "❌ Error: Input cannot be empty.",
149+
error_invalid: str = "❌ Error: Please enter valid numbers only.",
150+
) -> List[float]:
151+
while True:
152+
val_str = input(prompt).strip()
153+
if not val_str:
154+
print(error_empty)
155+
continue
156+
try:
157+
val_list = [float(x) for x in val_str.split()]
158+
if min_len is not None and len(val_list) < min_len:
159+
print(f"❌ Error: Please enter at least {min_len} numbers.")
160+
continue
161+
if max_len is not None and len(val_list) > max_len:
162+
print(f"❌ Error: Please enter at most {max_len} numbers.")
163+
continue
164+
return val_list
165+
except ValueError:
166+
print(error_invalid)

0 commit comments

Comments
 (0)