【文章标题】:Introducing wrapture
【文章标题】:wrapture 简介
【文章正文】:
Introducing wrapture
wrapture 简介
New from Graham Dumpleton (of
来自 Graham Dumpleton(以开发
wrapt
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.
、mod_wsgi 和 New Relic 的 Python 代理而闻名)的新作。他将 Wrapture 描述为从 wrapt 中汲取了猴子补丁(monkeypatching)的思想,并将其扩展到同时应用于测试和追踪。
Wrapture (
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.
)可以轻松包装任何函数或方法,从而追踪所有访问,或覆盖以返回不同的值。
It acts as both an alternative to
它既是
unittest.mock
unittest.mock
and a way to implement tracing against an existing project:
的替代方案,也是一种为现有项目实现追踪的方式:
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
Wrapture 包含
OpenTelemetry support
OpenTelemetry 支持
and even has an entirely configuration-based mechanism for adding tracing to an existing Python project, which looks like this:
甚至还有一个完全基于配置的机制,用于为现有 Python 项目添加追踪,如下所示:
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 a large entirely agent-driven project:
有趣的是,这也是 Graham 首次尝试完全由 AI 驱动的大型项目:
“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
《使用 wrapture 进行单元测试》
, Graham shows the testing patterns supported by the new library:
Graham 展示了这个新库支持的测试模式:
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} Tags:
标签:
graham-dumpleton
graham-dumpleton
,
、
python
python
,
、
monkeypatch
monkeypatch