蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

行程价格、预算与本地预订草稿

PYTHONCPython 3.12 标准库,独立临时文件和 SQLite,本机显式规则决策overview.py
overview.py55 行
"""Plan a trip from local listings. Payment never connects to a service."""
import json

from lab_tools import run_exercise

OFFERS = {"train": 240, "room": 420, "museum": 60}
PLAN = ("train", "room", "museum")


def itinerary(limit, allow_booking, approved, workspace):
    trace = []
    selected = []
    for service in PLAN:
        price = OFFERS[service]
        trace.append({"lookup": service, "observed_cost": price})
        selected.append((service, price))
    total = sum(price for _, price in selected)
    state = "over_budget" if total > limit else "ready"
    if state == "ready":
        if not allow_booking:
            state = "planning_only"
        elif not approved:
            state = "awaiting_approval"
        else:
            # In this exercise confirmation writes a local preview, with no reservation.
            path = workspace / "reservation-preview.json"
            payload = {"total": total, "services": [name for name, _ in selected]}
            path.write_text(json.dumps(payload), encoding="utf-8")
            state = "local_preview_created"
    return {"total": total, "state": state, "trace": trace}


def exercise(checks, workspace):
    result = itinerary(750, True, False, workspace)
    expect = checks.expect
    expect("overview_loop", [750, True, False], "Read three local offers, add costs, check approval",
           {"total": 720, "state": "awaiting_approval", "lookups": list(PLAN)},
           {"total": result["total"], "state": result["state"],
            "lookups": [step["lookup"] for step in result["trace"]]})
    dest = workspace / "reservation-preview.json"
    expect("overview_unapproved", False, "Check the temporary destination after withholding approval",
           False, dest.exists())
    expect("overview_budget", 650, "Recompute under a lower budget", "over_budget",
           itinerary(650, True, True, workspace)["state"])
    expect("overview_permission", False, "Plan with booking permission disabled", "planning_only",
           itinerary(750, False, True, workspace)["state"])
    result = itinerary(750, True, True, workspace)
    expect("overview_local_receipt", [750, True, True], "Read back the approved local preview file",
           ["local_preview_created", {"total": 720, "services": list(PLAN)}],
           [result["state"], json.loads(dest.read_text(encoding="utf-8"))])


if __name__ == "__main__":
    run_exercise(exercise)
本例需在 CPython 3.12 标准库,独立临时文件和 SQLite,本机显式规则决策 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:CPython 3.12 标准库,独立临时文件和 SQLite,本机显式规则决策

入口文件:projects/ai-agent-foundations/overview.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 overview.py,或下载右侧课程工程包。
  2. 确认本地已准备 CPython 3.12 标准库,独立临时文件和 SQLite,本机显式规则决策。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:AI Agent(智能体)

课程关联工程包(登记关联48章) · 含当前章节或实例 · mbskycom202612a3073.zip

下载课程关联工程包:AI Agent(智能体)

课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a3082.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习