Repository navigation
Expand file tree
/
Copy pathvalidation.py
More file actions
154 lines (147 loc) · 5.3 KB
/
Copy pathvalidation.py
File metadata and controls
154 lines (147 loc) · 5.3 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
153
154
from typing import List, Optional
def get_int(
prompt: str,
min_value: Optional[int] = None,
max_value: Optional[int] = None,
default: Optional[int] = None,
error_empty: str = "❌ Error: Input cannot be empty.",
error_invalid: str = "❌ Invalid input. Please enter a valid integer.",
) -> int:
while True:
try:
val_str = input(prompt).strip()
if not val_str:
if default is not None:
return default
print(error_empty)
continue
val = int(val_str)
if min_value is not None and val < min_value:
print(f"❌ Please enter a number greater than or equal to {min_value}.")
continue
if max_value is not None and val > max_value:
print(f"❌ Please enter a number less than or equal to {max_value}.")
continue
return val
except ValueError:
print(error_invalid)
def get_float(
prompt: str,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
default: Optional[float] = None,
error_empty: str = "❌ Error: Input cannot be empty.",
error_invalid: str = "❌ Invalid input. Please enter a valid number.",
) -> float:
while True:
try:
val_str = input(prompt).strip()
if not val_str:
if default is not None:
return default
print(error_empty)
continue
val = float(val_str)
if min_value is not None and val < min_value:
print(f"❌ Please enter a number greater than or equal to {min_value}.")
continue
if max_value is not None and val > max_value:
print(f"❌ Please enter a number less than or equal to {max_value}.")
continue
return val
except ValueError:
print(error_invalid)
def get_non_empty_string(
prompt: str,
default: Optional[str] = None,
error_empty: str = "❌ Error: Input cannot be empty.",
) -> str:
while True:
val_str = input(prompt).strip()
if not val_str:
if default is not None:
return default
print(error_empty)
continue
return val_str
def get_choice(
prompt: str,
choices: List[str],
default: Optional[str] = None,
error_empty: str = "❌ Error: Input cannot be empty.",
error_invalid: Optional[str] = None,
) -> str:
choices_lower = [c.lower() for c in choices]
while True:
val_str = input(prompt).strip()
if not val_str:
if default is not None:
return default
print(error_empty)
continue
if val_str.lower() in choices_lower:
idx = choices_lower.index(val_str.lower())
return choices[idx]
if error_invalid is not None:
print(error_invalid)
else:
print(f"❌ Invalid selection. Please choose from: {', '.join(choices)}")
def get_yes_no(prompt: str, default: Optional[str] = None) -> bool:
while True:
val_str = input(prompt).strip().lower()
if not val_str:
if default is not None:
return default.lower() in ['y', 'yes']
print("❌ Error: Input cannot be empty. Please enter 'y' or 'n'.")
continue
if val_str in ['y', 'yes']:
return True
if val_str in ['n', 'no']:
return False
print("❌ Invalid choice. Please enter 'y' or 'n'.")
def get_int_list(
prompt: str,
min_len: Optional[int] = None,
max_len: Optional[int] = None,
error_empty: str = "❌ Error: Input cannot be empty.",
error_invalid: str = "❌ Error: Please enter valid integers only.",
) -> List[int]:
while True:
val_str = input(prompt).strip()
if not val_str:
print(error_empty)
continue
try:
val_list = [int(x) for x in val_str.split()]
if min_len is not None and len(val_list) < min_len:
print(f"❌ Error: Please enter at least {min_len} numbers.")
continue
if max_len is not None and len(val_list) > max_len:
print(f"❌ Error: Please enter at most {max_len} numbers.")
continue
return val_list
except ValueError:
print(error_invalid)
def get_float_list(
prompt: str,
min_len: Optional[int] = None,
max_len: Optional[int] = None,
error_empty: str = "❌ Error: Input cannot be empty.",
error_invalid: str = "❌ Error: Please enter valid numbers only.",
) -> List[float]:
while True:
val_str = input(prompt).strip()
if not val_str:
print(error_empty)
continue
try:
val_list = [float(x) for x in val_str.split()]
if min_len is not None and len(val_list) < min_len:
print(f"❌ Error: Please enter at least {min_len} numbers.")
continue
if max_len is not None and len(val_list) > max_len:
print(f"❌ Error: Please enter at most {max_len} numbers.")
continue
return val_list
except ValueError:
print(error_invalid)