-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbcli.py
121 lines (91 loc) · 4 KB
/
dbcli.py
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
import json
from string import capwords
from typing import Iterable
from customtypes import Flavours, DrinkMenu
with open("flavours.json") as f:
raw_json: dict = json.load(f)
DRINKS: DrinkMenu = raw_json.get("lemonade") # type: ignore
DRINK_NAMES = {drink_name for drink_name in DRINKS}
"""All drink names on the menu."""
DRINK_FLAVOURS = {flavour for flavours in DRINKS.values() for flavour in flavours}
"""All unique flavours from all the drinks on the menu."""
def underscores_to_spaces(strings: Iterable[str]) -> set[str]:
return set(map(lambda string: string.replace("_", " "), strings))
def all_flavours_in_flavour_list(flavours_to_check: Flavours, flavour_list: Flavours):
flavours_to_check = underscores_to_spaces(flavours_to_check)
return all({flavour in flavour_list for flavour in flavours_to_check})
def all_flavours_are_valid(flavours: Flavours):
return all_flavours_in_flavour_list(flavours, DRINK_FLAVOURS)
def is_valid_flavour(flavour: str):
return flavour.replace("_", " ") in DRINK_FLAVOURS
def is_valid_drink(drink_name: str):
return drink_name.replace("_", " ") in DRINK_NAMES
def _print_invalid_flavours(flavours: Flavours):
for flavour in flavours:
if flavour not in DRINK_FLAVOURS:
print(f"{capwords(flavour)} is not a valid flavour.")
def _print_drink(drink_name: str, flavours_not_passed: Flavours) -> None:
if flavours_not_passed:
formatted_flavours = str()
for idx, flav in enumerate(flavours_not_passed):
if idx != 0:
formatted_flavours += ","
formatted_flavours += f" {capwords(flav)}"
print(f"{capwords(drink_name)}:{formatted_flavours}")
else:
print(capwords(drink_name))
def closest_dutch_bros() -> str:
raise NotImplementedError
def process(cmd: str) -> None:
match cmd.lower().split():
case ["quit" | "exit"]:
exit()
case ["flavours"]:
for flavour in sorted(DRINK_FLAVOURS):
print(capwords(flavour))
case ["find"]:
try:
print(closest_dutch_bros())
except NotImplementedError:
print("This feature not currently implemented. Check back soon! :)\n")
print("https://www.dutchbros.com/locations")
case ["drinks", *flavours, "except", flavour_to_exclude]:
if not all_flavours_are_valid(flavours):
_print_invalid_flavours(flavours)
return
if not is_valid_flavour(flavour_to_exclude):
print(
f'"{flavour_to_exclude}" is not a flavour, so it can\'t be excluded...'
)
print("Here's the drinks with the ones you mentioned anyway:\n")
for drink_name, drink_flavours in DRINKS.items():
if (
all_flavours_in_flavour_list(flavours, drink_flavours)
and flavour_to_exclude not in drink_flavours
):
flavours_not_passed = set(drink_flavours).difference(
underscores_to_spaces(flavours)
)
_print_drink(drink_name, flavours_not_passed)
case ["drinks", *flavours]:
if not all_flavours_are_valid(flavours):
_print_invalid_flavours(flavours)
return
for drink_name, drink_flavours in DRINKS.items():
if all_flavours_in_flavour_list(flavours, drink_flavours):
flavours_not_passed = set(drink_flavours).difference(
underscores_to_spaces(flavours)
)
_print_drink(drink_name, flavours_not_passed)
case [drink] if is_valid_drink(drink):
for flavour in DRINKS.get(drink.replace("_", " "), list()):
print(capwords(flavour))
case _:
pass
def run_shell() -> None:
while True:
cmd = input("> ")
print()
process(cmd)
print()
run_shell()