Diese Seite wurde maschinell übersetzt und wurde noch nicht von einer Person überprüft. Englisch ist die Referenz; Korrekturen auf GitHub sind willkommen. GitHub
Cookwala / SDK und Szenarien / 065
Record a distribution and its per-meal menu by SMS
DIST records meals, people and kilograms for today at the sender's site. MENU adds per-meal nutrients that the school and sodium rule packs compare with their limits.
Zuerst lesen: /docs/HUMANITARIAN-PROFILE/ /humanitarian/
Schritte
A school program served 410 meals to 410 people from 96 kg.
parseSmsPer-meal energy, sodium and fruit-and-vegetable grams for distribution D12.
parseSmsPROTEIN is the fourth nutrient the grammar knows.
parseSmsAn incomplete DIST is a usage error.
parseSmsAn unknown nutrient word is a usage error, not a silent zero.
parseSms
Ergebnis
You can report a day's meals and menu from a phone. Next: scenario 072 shows which nutrients the rule packs read.
Kommandozeile
cookwala sms "DIST 410 MEALS 410 PEOPLE 96KG"
cookwala sms "MENU D12 KCAL650 SODIUM540 FV95"Aufgezeichnete Ausgabe
Status: ok
[1] parseSms: {"ok": true, "command": "DIST", "meals": 410, "people": 410, "kg": 96.0, "findings": []}
[2] parseSms: {"ok": true, "command": "MENU", "distribution": "D12", "perMeal": {"energyKcal": 650.0, "sodiumMg": 540.0, "fruitVegG": 95.0}, "findings": []}
[3] parseSms: {"ok": true, "command": "MENU", "distribution": "D12", "perMeal": {"energyKcal": 650.0, "proteinG": 22.0, "fruitVegG": 120.0}, "findings": []}
[4] parseSms: {"ok": false, "error": "usage"}
[5] parseSms: {"ok": false, "error": "usage"}Code
Ausführen: python scenarios/out/065-distribution-and-menu-by-sms/python.py
# Scenario 065: Record a distribution and its per-meal menu by SMS
# DIST records meals, people and kilograms for today at the sender's site. MENU adds per-meal nutrients that the school and sodium rule packs compare with their limits.
# Run a hub first: python hub/cookwala_hub.py --recipes examples
import json, os
from cookwala.client import CookwalaClient, CookwalaProblem
def load(path):
with open(path, encoding="utf-8") as f:
return json.load(f)
def pick(obj, path):
for part in path.replace("]", "").replace("[", ".").split("."):
obj = obj[int(part)] if part.isdigit() else (len(obj) if part == "length" else obj.get(part))
return obj
c = CookwalaClient(os.environ.get("COOKWALA_HUB", "http://localhost:7878"))
# Step 1: A school program served 410 meals to 410 people from 96 kg.
dist = c.parse_sms("DIST 410 MEALS 410 PEOPLE 96KG")
assert pick(dist, "ok") == True, pick(dist, "ok")
print("ok", "ok", "=", True)
assert pick(dist, "command") == "DIST", pick(dist, "command")
print("ok", "command", "=", "DIST")
assert pick(dist, "meals") == 410, pick(dist, "meals")
print("ok", "meals", "=", 410)
assert pick(dist, "people") == 410, pick(dist, "people")
print("ok", "people", "=", 410)
assert pick(dist, "kg") == 96.0, pick(dist, "kg")
print("ok", "kg", "=", 96.0)
# Step 2: Per-meal energy, sodium and fruit-and-vegetable grams for distribution D12.
menu = c.parse_sms("MENU D12 KCAL650 SODIUM540 FV95")
assert pick(menu, "command") == "MENU", pick(menu, "command")
print("ok", "command", "=", "MENU")
assert pick(menu, "distribution") == "D12", pick(menu, "distribution")
print("ok", "distribution", "=", "D12")
assert pick(menu, "perMeal.energyKcal") == 650.0, pick(menu, "perMeal.energyKcal")
print("ok", "perMeal.energyKcal", "=", 650.0)
assert pick(menu, "perMeal.sodiumMg") == 540.0, pick(menu, "perMeal.sodiumMg")
print("ok", "perMeal.sodiumMg", "=", 540.0)
assert pick(menu, "perMeal.fruitVegG") == 95.0, pick(menu, "perMeal.fruitVegG")
print("ok", "perMeal.fruitVegG", "=", 95.0)
# Step 3: PROTEIN is the fourth nutrient the grammar knows.
protein = c.parse_sms("MENU D12 KCAL650 PROTEIN22 FV120")
assert pick(protein, "perMeal.proteinG") == 22.0, pick(protein, "perMeal.proteinG")
print("ok", "perMeal.proteinG", "=", 22.0)
# Step 4: An incomplete DIST is a usage error.
usage = c.parse_sms("DIST 410 MEALS")
assert pick(usage, "ok") == False, pick(usage, "ok")
print("ok", "ok", "=", False)
assert pick(usage, "error") == "usage", pick(usage, "error")
print("ok", "error", "=", "usage")
# Step 5: An unknown nutrient word is a usage error, not a silent zero.
usage2 = c.parse_sms("MENU D12 SALT5")
assert pick(usage2, "ok") == False, pick(usage2, "ok")
print("ok", "ok", "=", False)
assert pick(usage2, "error") == "usage", pick(usage2, "error")
print("ok", "error", "=", "usage")
print("scenario complete")