-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
55 lines (41 loc) · 1.02 KB
/
models.py
File metadata and controls
55 lines (41 loc) · 1.02 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
from dataclasses import dataclass, asdict
import json
@dataclass
class FoodNutrient:
food_nutrient_id: int
fdc_id: int
food_nutrient_amount: float
nutrient_id: int
nutrient_name: str
nutrient_unit_name: str
food_description: str
food_data_type: str
@dataclass
class FoodNutrientMinimal:
food_nutrient_id: int
food_nutrient_amount: float
nutrient_id: int
nutrient_name: str
nutrient_unit_name: str
is_macro: bool
def to_json(self):
return json.dumps(self.__dict__)
@dataclass
class Food:
id: int
description: str
data_type: str
nutrients: list[FoodNutrientMinimal]
def to_json(self):
return json.dumps(asdict(self))
def create_food_from_dict(data: dict) -> Food:
return Food(
id=data["id"],
description=data["description"],
data_type=data["data_type"],
nutrients=[FoodNutrientMinimal(**values) for values in data["nutrients"]],
)
@dataclass
class TypesenseFood:
food_id: int
food_name: str