Cookwala / حزمة التطوير والسيناريوهات / 007

Convert kitchen units, and see that volume to mass needs a density

Kitchen units have exact metric values in the standard (a tablespoon is 15 ml, a cup 240 ml). Converting a volume to a mass requires a density; without one the tool answers an error, never a guess.

الفئة: home cook · الهدف: صحة أفضل للناس · المستوى: beginner

اقرأ أولًا: /docs/CORE/ /docs/QUICKSTART/

الخطوات

  1. Two tablespoons of oil in millilitres. convert

  2. One cup of flour in grams, with the density of flour given as 0.53 g/ml. convert

  3. Half a kilogram, the other way round. convert

  4. Thirty millilitres back to tablespoons. convert

  5. A pinch is about 0.36 g. convert

النتيجة

You can scale and convert the quantities in a recipe. Asking for a cup in grams without a density returns an HTTP 400 problem, which is the standard refusing to guess. Next: scenario 019 converts quantities read from a recipe document.

سطر الأوامر

cookwala convert 2 tbsp ml
cookwala convert 1 cup g --density 0.53
cookwala convert 1 cup g   # error: volume<->mass conversion needs a density
curl -s -X POST http://localhost:7878/v1/tools/convert -H 'Content-Type: application/json' -d '{"value": 2, "unit": "tbsp", "to": "ml"}'

المخرجات المسجلة

الحالة: ok

[1] convert: {"value": 30.0, "unit": "ml"}
[2] convert: {"value": 127.2, "unit": "g"}
[3] convert: {"value": 0.5, "unit": "kg"}
[4] convert: {"value": 2.0, "unit": "tbsp"}
[5] convert: {"value": 0.36, "unit": "g"}

الكود

التشغيل: python scenarios/out/007-convert-kitchen-units/python.py

# Scenario 007: Convert kitchen units, and see that volume to mass needs a density
# Kitchen units have exact metric values in the standard (a tablespoon is 15 ml, a cup 240 ml). Converting a volume to a mass requires a density; without one the tool answers an error, never a guess.
# 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: Two tablespoons of oil in millilitres.
oil = c.convert(2, "tbsp", "ml")
assert pick(oil, "value") == 30.0, pick(oil, "value")
print("ok", "value", "=", 30.0)
assert pick(oil, "unit") == "ml", pick(oil, "unit")
print("ok", "unit", "=", "ml")

# Step 2: One cup of flour in grams, with the density of flour given as 0.53 g/ml.
flour = c.convert(1, "cup", "g", 0.53)
assert pick(flour, "value") == 127.2, pick(flour, "value")
print("ok", "value", "=", 127.2)
assert pick(flour, "unit") == "g", pick(flour, "unit")
print("ok", "unit", "=", "g")

# Step 3: Half a kilogram, the other way round.
kg = c.convert(500, "g", "kg")
assert pick(kg, "value") == 0.5, pick(kg, "value")
print("ok", "value", "=", 0.5)

# Step 4: Thirty millilitres back to tablespoons.
tbsp = c.convert(30, "ml", "tbsp")
assert pick(tbsp, "value") == 2.0, pick(tbsp, "value")
print("ok", "value", "=", 2.0)

# Step 5: A pinch is about 0.36 g.
pinch = c.convert(1, "pinch", "g")
assert pick(pinch, "value") == 0.36, pick(pinch, "value")
print("ok", "value", "=", 0.36)

print("scenario complete")

scenarios/007-convert-kitchen-units.json

السابق: 006 التالي: 008