Graham Dumpleton发布wrapture:AI辅助的Python测
Introducing wrapture
Introducing wrapture
介绍 wrapture
New from Graham Dumpleton (of wrapt, mod_wsgi, and New Relic's Python agent fame), who describes Wrapture as taking the monkeypatching ideas from wrapt and extending them to apply to testing and tracing at the same time.
来自 Graham Dumpleton(wrapt、mod_wsgi 和 New Relic Python 代理的知名开发者)的新作品,他将 Wrapture 描述为将 wrapt 中的 monkeypatching 思想扩展至同时应用于测试和追踪。
Wrapture (full documentation here) makes it easy to wrap any function or method such that all access can be traced, or can be overridden to return a different value.
Wrapture(完整文档见此处)使得包装任何函数或方法变得容易,从而可以追踪所有访问,或者覆盖以返回不同的值。
It acts as both an alternative to unittest.mock and a way to implement tracing against an existing project:
它既可作为 unittest.mock 的替代方案,也可作为针对现有项目实现追踪的方式:
Attaching observation to code you do not control, recording what flows through it, and doing so without disturbing the program being watched, is a problem I have never really stopped thinking about.
将观测附加到你无法控制的代码上,记录流经其中的数据,且在不干扰被监控程序的情况下做到这一点,是我从未真正停止思考的问题。
Wrapture includes OpenTelemetry support and even has an entirely configuration-based mechanism for adding tracing to an existing Python project, which looks like this:
Wrapture 包含 OpenTelemetry 支持,甚至拥有完全基于配置的机制,用于向现有 Python 项目添加追踪,如下所示:
capture = "summary"
[[observe]]
target = "domain:Calculator"
name = ["outer", "inner"]
[[sink]]
type = "jsonlines"
path = "trace.jsonl"capture = "summary"
[[observe]]
target = "domain:Calculator"
name = ["outer", "inner"]
[[sink]]
type = "jsonlines"
path = "trace.jsonl"This is still a very young project - just a few weeks old - but it's off to a very promising start.
这仍然是一个非常年轻的项目——仅几周大——但它起步非常令人期待。
Interestingly, this is also Graham's first attempt at large entirely agent-driven project:
有趣的是,这也是 Graham 首次尝试完全由智能体驱动的大型项目:
Every line of code and documentation in wrapture was written by an AI assistant working under my direction. I want to be upfront about that, and equally upfront about what it was not. This was not vibe coding, where a one-shot prompt produces a pile of generated code and the person driving hopes for the best because they lack the knowledge to judge what came back. Vibe coding has earned its bad reputation. I engineered wrapture carefully from the start. I have spent a long time in this particular corner of Python and knew exactly what the result needed to be, and the AI was the means of producing it rather than the source of the design.
wrapture 中的每一行代码和文档都是在我的指导下由 AI 助手编写的。我想坦诚地说明这一点,同时也坦诚地说明它不是什么。这不是“氛围编程”(vibe coding),即通过一次性提示生成一堆代码,而操作者因缺乏判断能力而只能寄希望于最好的结果。氛围编程已赢得了其糟糕的名声。我从一开始就精心设计了 wrapture。我在这个特定的 Python 领域深耕已久,确切知道结果需要是什么,AI 只是产生结果的手段,而非设计的来源。
In a follow-up post, Unit testing with wrapture, Graham shows the testing patterns supported by the new library:
在后续文章《使用 wrapture 进行单元测试》中,Graham 展示了新库支持的测试模式:
def test_stub_with_wrapture():
with wrapture.binding(
Gateway, "charge"
).on_call.returns({
"id": "stub", "amount": 0}
):
assert OrderService().place(
500
)["id"] == "stub"def test_stub_with_wrapture():
with wrapture.binding(
Gateway, "charge"
).on_call.returns({
"id": "stub", "amount": 0}
):
assert OrderService().place(
500
)["id"] == "stub"And this neat example of a test that calls and then modifies the return value from the original method:
以及这个巧妙的示例,一个调用原始方法并随后修改其返回值的测试:
def test_pinned_result_with_wrapture():
charge = wrapture.binding(
Gateway, "charge"
)
charge.on_call.transforms_result(
lambda r: {**r, "id": "ch_TEST"}
)
with charge:
assert OrderService().place(
500
) == {
"id": "ch_TEST", "amount": 500
}def test_pinned_result_with_wrapture():
charge = wrapture.binding(
Gateway, "charge"
)
charge.on_call.transforms_result(
lambda r: {**r, "id": "ch_TEST"}
)
with charge:
assert OrderService().place(
500
) == {
"id": "ch_TEST", "amount": 500
}(In both of these examples the OrderService().place(...) method calls Gateway().charge(...).)
(在这两个示例中,OrderService().place(...) 方法调用了 Gateway().charge(...)。)
Tags: graham-dumpleton, monkey-patching, python, testing, pytest, observability, ai-assisted-programming, agentic-engineering, opentelemetry
标签:graham-dumpleton, monkey-patching, python, testing, pytest, observability, ai-assisted-programming, agentic-engineering, opentelemetry
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力