Skip to content

Commit

Permalink
perf: improve dataclass serialization (#43)
Browse files Browse the repository at this point in the history
* switch to typing.NamedTuple for dataclasses

* update requirements.txt

* parse diets as list[str] instead of str

* fix list typings
  • Loading branch information
jkerola authored Jan 29, 2024
1 parent 2fe18a5 commit d08450c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build==1.0.0
build==1.0.3
certifi==2023.7.22
charset-normalizer==3.2.0
exceptiongroup==1.1.3
Expand All @@ -7,7 +7,7 @@ iniconfig==2.0.0
packaging==23.1
pluggy==1.3.0
pyproject_hooks==1.0.0
pytest==7.4.2
pytest==8.0.0
requests==2.31.0
tomli==2.0.1
urllib3==2.0.4
2 changes: 1 addition & 1 deletion src/jmenu/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ def _parse_items(data: list[dict], relevant_menus: list[str] = []) -> list[MenuI
for opt in mealopts:
for item in opt["menuItems"]:
if item["name"] not in SKIPPED_ITEMS and len(item["name"]) > 0:
items.append(MenuItem(item["name"], item["diets"]))
items.append(MenuItem(item["name"], item["diets"].split(" ")))
return items
44 changes: 27 additions & 17 deletions src/jmenu/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,55 @@
* SKIPPED_ITEMS
"""

from collections import namedtuple
from typing import NamedTuple
from collections.abc import Iterable


_MenuItem = namedtuple("MenuItem", ["name", "diets"])


class MenuItem(_MenuItem):
class MenuItem(NamedTuple):
"""Dataclass for single menu items and their properties
Attributes:
name (str):
name of the dish
diets (str):
diets ([str]):
list of allergen markers
Methods:
diets_to_string: returns the list of diets as a joined string.
"""

name: str
diets: Iterable[str]

_Restaurant = namedtuple(
"Restaurant", ["name", "client_id", "kitchen_id", "menu_type", "relevant_menus"]
)
def diets_to_string(self) -> str:
"""Returns the diets associated with this MenuItem as spaced string."""
return " ".join(self.diets)


class Restaurant(_Restaurant):
class Restaurant(NamedTuple):
"""Dataclass for relevant restaurant information
Attributes:
name (str):
name of the restaurant
client_id (str):
client_id (int):
internal jamix identifier used for restaurant providers
kitchen_id (str):
kitchen_id (int):
internal jamix identifier used to assign menu content
menu_type (str):
menu_type (int):
internal jamix identifier used to classify menus based on content
relevant_menus (str):
relevant_menus ([str]):
menu names used for filtering out desserts etc.
"""

name: str
client_id: int
kitchen_id: int
menu_type: int
relevant_menus: Iterable[str]

_Marker = namedtuple("Marker", ["letters", "explanation"])


class Marker(_Marker):
class Marker(NamedTuple):
"""Dataclass for allergen information markings
Attributes:
Expand All @@ -65,7 +71,11 @@ class Marker(_Marker):
extended information about the marker
"""

letters: str
explanation: str


# TODO: Remove extra space when the API response is fixed
SKIPPED_ITEMS = [
"proteiinilisäke",
"Täysjyväriisi",
Expand Down
15 changes: 9 additions & 6 deletions src/jmenu/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def _print_menu(args: _ArgsNamespace) -> bool:
else:
print(res.name)
if not allergens:
print(*[f"\t {item.name} {item.diets}" for item in items], sep="\n")
print(
*[f"\t {item.name} {item.diets_to_string()}" for item in items],
sep="\n",
)
else:
_print_highlight(items, allergens)

Expand All @@ -137,13 +140,13 @@ def _print_explanations():
print(mark.letters, "\t", mark.explanation)


def _print_highlight(items: list[MenuItem], allergens: list[str]):
def _print_highlight(items: list[MenuItem], queried_allergens: list[str]):
for item in items:
diets = [diets.strip().lower() for diets in item.diets.split(",")]
if all(marker in diets for marker in allergens):
print("\033[92m", "\t", item.name, item.diets, "\033[0m")
item_markers = [diet.strip().lower() for diet in item.diets]
if all(marker in item_markers for marker in queried_allergens):
print("\033[92m", "\t", item.name, item.diets_to_string(), "\033[0m")
else:
print("\t", item.name, item.diets)
print("\t", item.name, item.diets_to_string())


def _print_header(fetch_date: datetime):
Expand Down

0 comments on commit d08450c

Please sign in to comment.