forked from coddingtonbear/python-myfitnesspal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meal.py
53 lines (40 loc) · 1.42 KB
/
meal.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
from typing import List
from myfitnesspal.base import MFPBase
from myfitnesspal.types import NutritionDict
from . import types
from .entry import Entry
class Meal(MFPBase):
"""Stores information about a particular meal."""
def __init__(self, name: str, entries: List[Entry]):
self._name = name
self._entries = entries
def __getitem__(self, value: int) -> Entry:
"""Returns a particular entry for thsi meal."""
if not isinstance(value, int):
raise ValueError("Index must be an integer")
return self.entries[value]
def __len__(self) -> int:
return len(self.entries)
@property
def entries(self) -> List[Entry]:
"""Entries for this meal."""
return self._entries
@property
def name(self) -> str:
"""Name of this meal."""
return self._name
@property
def totals(self) -> NutritionDict:
"""Nutrition totals for all entries for this meal."""
nutrition = {}
for entry in self.entries:
for k, v in entry.nutrition_information.items():
if k not in nutrition:
nutrition[k] = v
else:
nutrition[k] += v
return nutrition
def get_as_list(self) -> List[types.MealEntry]:
return [e.get_as_dict() for e in self.entries]
def __str__(self) -> str:
return f"{self.name.title()} {self.totals}"