蓝色天空网 · mbsky.com 全部实例

外观:简化工作流与真实事务

统一入口协调子系统,失败与补偿也应有明确结果。

本页展示最近一次实际本地Python命名测试结果,不是在线Python解释器。外部服务、真实硬件及完整来源语义仍分别验收。

Python 3.12.14 · 13 项实际检查 · 当前入口SHA256 ff6999828ef764332fb5c3a62d43dc5587d9766d39884b17e5d81bdc5c5f5f2c

检查项实际结果预期结果
ordered start["lights.dim", "screen.down", "projector.on", "amplifier.on", "player.play:学习视频"]["lights.dim", "screen.down", "projector.on", "amplifier.on", "player.play:学习视频"]
cannot double start"RuntimeError""RuntimeError"
end idempotent["lights.dim", "screen.down", "projector.on", "amplifier.on", "player.play:学习视频", "player.stop", "amplifier.off", "projector.off", "screen.up", "lights.on"]["lights.dim", "screen.down", "projector.on", "amplifier.on", "player.play:学习视频", "player.stop", "amplifier.off", "projector.off", "screen.up", "lights.on"]
final device model"off""off"
transaction failure surfaced"IntegrityError""IntegrityError"
actual rollback of first op[[1, "first"]][[1, "first"]]
real file workflow"MBSKY.COM""MBSKY.COM"
actual output file"MBSKY.COM""MBSKY.COM"
missing file propagates"FileNotFoundError""FileNotFoundError"
music does not activate projectorfalsefalse
game actual model state"game""game"
order facade actual stock11
order notification local record"recorded""recorded"

本章源码与生成文件

下载独立入口 共享实现 补充实例实现 本章结果JSON

查看当前核心代码
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))

上图是本站运行生成文件,不能当作来源原图已采集。