-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFood.py
More file actions
67 lines (55 loc) · 1.22 KB
/
Food.py
File metadata and controls
67 lines (55 loc) · 1.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
from pydantic import BaseModel, PositiveInt
from fastapi import FastAPI
import tracemalloc
#from main import app
EXPECTED_FOOD1 = {
"id": 1,
"name": "egg",
"serving_size": "piece",
"kcal_per_serving": 78,
"protein_grams": 6.2,
"fibre_grams": 0,
}
EXPECTED_FOOD2 = {
"id": 2,
"name": "oatmeal",
"serving_size": "100 grams",
"kcal_per_serving": 336,
"protein_grams": 13.2,
"fibre_grams": 10.1,
}
class Food (BaseModel):
id: int
name: str
serving_size: str
kcal_per_serving: int
protein_grams: float
fibre_grams: float
app = FastAPI()
foods: dict[int, Food] = {}
@app.post("/")
async def create_food(food:Food):
Food[food] = food
return food
@app.get('/', response_model=list[Food])
async def get_root():
return Food.values()
@app.get('/{item}', response_model=Food)
async def get_item(item:int):
return Foods[item]
print(create_food(EXPECTED_FOOD1))
print(get_item(2))
print(get_item(1))
print(get_root())
external_data = {
'id': 123,
'name': 'banana',
'serving_size': '1 banana',
'kcal_per_serving' : 90,
'protein_grams' : 1.3,
'fibre_grams': 3.1,
}
food = Food(**external_data)
print(food.id)
#> 123
print(food.model_dump())