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 / 061

Claim an offer in full or by the kilogram

CLAIM names the offer and either ALL or a quantity. Kilograms may be written with or without KG; anything else is a usage error and nothing is written.

Zielgruppe: food bank · Ziel: Hunger bekämpfen · Level: beginner

Zuerst lesen: /docs/HUMANITARIAN-PROFILE/ /humanitarian/

Schritte

  1. Claim the whole offer. parseSms

  2. Claim 20 kg of it. parseSms

  3. The KG suffix is accepted too. parseSms

  4. No quantity: usage error. parseSms

  5. A word where a quantity should be: usage error. parseSms

Ergebnis

You can claim and know the two forms the gateway accepts. Next: scenario 062 records the handover.

Kommandozeile

cookwala sms "CLAIM A7K ALL"
cookwala sms "CLAIM A7K 20KG"

Aufgezeichnete Ausgabe

Status: ok

[1] parseSms: {"ok": true, "command": "CLAIM", "offer": "A7K", "all": true, "findings": []}
[2] parseSms: {"ok": true, "command": "CLAIM", "offer": "A7K", "kg": 20.0, "findings": []}
[3] parseSms: {"ok": true, "command": "CLAIM", "offer": "A7K", "kg": 20.0, "findings": []}
[4] parseSms: {"ok": false, "error": "usage"}
[5] parseSms: {"ok": false, "error": "usage"}

Code

Ausführen: python scenarios/out/061-claim-all-or-a-quantity/python.py

# Scenario 061: Claim an offer in full or by the kilogram
# CLAIM names the offer and either ALL or a quantity. Kilograms may be written with or without KG; anything else is a usage error and nothing is written.
# 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: Claim the whole offer.
all = c.parse_sms("CLAIM A7K ALL")
assert pick(all, "ok") == True, pick(all, "ok")
print("ok", "ok", "=", True)
assert pick(all, "command") == "CLAIM", pick(all, "command")
print("ok", "command", "=", "CLAIM")
assert pick(all, "offer") == "A7K", pick(all, "offer")
print("ok", "offer", "=", "A7K")
assert pick(all, "all") == True, pick(all, "all")
print("ok", "all", "=", True)

# Step 2: Claim 20 kg of it.
kg = c.parse_sms("CLAIM A7K 20")
assert pick(kg, "kg") == 20.0, pick(kg, "kg")
print("ok", "kg", "=", 20.0)
assert pick(kg, "all") == None, pick(kg, "all")
print("ok", "all", "=", None)

# Step 3: The KG suffix is accepted too.
kgSuffix = c.parse_sms("CLAIM A7K 20KG")
assert pick(kgSuffix, "kg") == 20.0, pick(kgSuffix, "kg")
print("ok", "kg", "=", 20.0)

# Step 4: No quantity: usage error.
missing = c.parse_sms("CLAIM A7K")
assert pick(missing, "ok") == False, pick(missing, "ok")
print("ok", "ok", "=", False)
assert pick(missing, "error") == "usage", pick(missing, "error")
print("ok", "error", "=", "usage")

# Step 5: A word where a quantity should be: usage error.
word = c.parse_sms("CLAIM A7K SOME")
assert pick(word, "ok") == False, pick(word, "ok")
print("ok", "ok", "=", False)
assert pick(word, "error") == "usage", pick(word, "error")
print("ok", "error", "=", "usage")

print("scenario complete")

scenarios/061-claim-all-or-a-quantity.json

Zurück: 060 Weiter: 062