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

外观:简化工作流与真实事务:独立运行与结果核对

PYTHONWindows/Python3.12.14标准库;静态浏览器页面使用独立Edge会话,不在线执行Pythonpython-facade.py
python-facade.py42 行
from pathlib import Path
import json,sys
from pattern_cases import *
from pattern_extensions import *

def facade(c,out):
    theater=TheaterFacade();theater.watch("学习视频")
    c.eq("ordered start",theater.events,["lights.dim","screen.down","projector.on","amplifier.on","player.play:学习视频"])
    c.raises("cannot double start",RuntimeError,lambda:theater.watch("other"));theater.end();before=list(theater.events);theater.end();c.eq("end idempotent",theater.events,before)
    c.eq("final device model",theater.state,"off")
    db=DatabaseFacade(out/"facade.sqlite");db.transaction([("CREATE TABLE items(id INTEGER PRIMARY KEY,title TEXT NOT NULL)",())])
    db.transaction([("INSERT INTO items VALUES(?,?)",(1,"first"))])
    c.raises("transaction failure surfaced",sqlite3.IntegrityError,lambda:db.transaction([("INSERT INTO items VALUES(?,?)",(2,"second")),("INSERT INTO items VALUES(?,?)",(1,"duplicate"))]))
    c.eq("actual rollback of first op",db.query("SELECT id,title FROM items ORDER BY id"),[(1,"first")])
    input_file=out/"input.txt";output_file=out/"output.txt";input_file.write_text("  mbsky.com  ",encoding="utf8")
    def process_file(src,dst):
        text=src.read_text(encoding="utf8");value=text.strip().upper();dst.write_text(value,encoding="utf8");return value
    c.eq("real file workflow",process_file(input_file,output_file),"MBSKY.COM")
    c.eq("actual output file",output_file.read_text(encoding="utf8"),"MBSKY.COM")
    c.raises("missing file propagates",FileNotFoundError,lambda:process_file(out/"missing.txt",output_file))

def extra_facade(c,out):
    class Entertainment(TheaterFacade):
        def music(self,title):
            if self.state!="off":raise RuntimeError("already active")
            self.events.extend(["amplifier.on","audio.play:"+title]);self.state="music"
        def game(self,title):
            if self.state!="off":raise RuntimeError("already active")
            self.events.extend(["screen.down","projector.on","game.play:"+title]);self.state="game"
    music=Entertainment();music.music("学习背景音乐");c.eq("music does not activate projector","projector.on"in music.events,False);game=Entertainment();game.game("本地游戏模型");c.eq("game actual model state",game.state,"game")
    order=OrderCoordinator();result=order.place("facade-demo",1);c.eq("order facade actual stock",order.stock,1);c.eq("order notification local record",result["notification"],"recorded")

def run_case(out):
    c=Checks()
    facade(c,out)
    extra_facade(c,out)
    return {"chapter":'python-facade',"checks":c.rows,"passed":True,"scope":"named owned all24chapter named cases only; full source coverage pending"}

if __name__ == "__main__":
    output=Path(sys.argv[1]);output.mkdir(parents=True,exist_ok=False)
    print(json.dumps(run_case(output),ensure_ascii=False,default=repr))
本例需在 Windows/Python3.12.14标准库;静态浏览器页面使用独立Edge会话,不在线执行Python 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows/Python3.12.14标准库;静态浏览器页面使用独立Edge会话,不在线执行Python

入口文件:projects/patterns-lab/python-facade.py

预期结果与验证条件观察提示
  • 当前固定输入命名检查符合明确实际结果及失败策略
  • 每章输出、源码和预期分别对应;本地模型不冒充生产系统,未验能力继续单列

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:Python 设计模式

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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