Repository navigation
Expand file tree
/
Copy pathcli_input.py
More file actions
49 lines (39 loc) · 1.43 KB
/
Copy pathcli_input.py
File metadata and controls
49 lines (39 loc) · 1.43 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
from decimal import Decimal, InvalidOperation
import constants
MAX_PRICE_CENTS = 2**63 - 1
def read_price(message: str) -> Decimal:
"""Read a positive product price from the terminal."""
while True:
try:
price = Decimal(input(message).strip())
if not price.is_finite() or price <= 0:
print(constants.ERROR_NEGATIVE_PRICE)
continue
cents = price * 100
if cents != cents.to_integral_value() or int(cents) > MAX_PRICE_CENTS:
print(constants.ERROR_INVALID_PRICE)
continue
return price
except InvalidOperation:
print(constants.ERROR_INVALID_PRICE)
def read_product_name(message: str) -> str | None:
"""Read and trim a non-empty product name from the terminal."""
name = input(message).strip()
if not name:
print(constants.ERROR_EMPTY_NAME)
return None
return name
def read_product_id(message: str) -> int | None:
"""Read a positive product ID from the terminal."""
try:
product_id = int(input(message))
if product_id <= 0:
print(constants.ERROR_INVALID_ID)
return None
return product_id
except ValueError:
print(constants.ERROR_INVALID_ID_VALUE)
return None
def read_menu_option() -> str:
"""Read a menu option from the terminal."""
return input(constants.PROMPT_MENU_OPTION).strip()