Mr. Bent懂得他的数字。
项目描述
一个穿着完美黑色夹克和条纹裤子的挑剔小男人。
简介
Mr Bent是一个框架,允许在Python应用程序中收集配置文件数据并在不同的逻辑级别查看。涉及到的三个概念是:一个插件,用于分析应用程序的代码片段;一个上下文,您想要报告数据的逻辑代码块;以及一个过滤器,一种获取有关上下文结果来源的细粒度信息的方法。
插件
插件是可调用的,被传递给“mkwrapper”函数,该函数将其应用于您的应用程序中的函数。
这看起来像
mr.bent.wrapper.mkwrapper(foo.bar, plugincallable, "myplugin")
这将导致在每次调用“foo.bar”时调用“plugincallable”,并将插件的输出添加到当前上下文中作为“myplugin”。
插件可以返回一个数字或一个可迭代对象。如果它返回一个可迭代对象,则必须包含字符串或数字。返回数字的情况被视为返回长度为1的数字的可迭代对象。
上下文
上下文存储由插件生成数据。在任何时候,都可以开始一个新的上下文,它将成为当前活动上下文的“子上下文”。如果没有当前活动上下文,将创建一个新的顶层上下文。
上下文使用创建它们的函数的点分名称命名,并将数据返回给回调。
这看起来像
def mycallback(context, result, stats):
return "%s <!-- %s -->" % (result, `stats`)
mr.bent.wrapper.mkcontext(bar.foo, mycallback)
这个例子将导致调用返回XML的函数bar.foo,在随后的注释中返回上下文字典的repr。
当上下文结束时,它返回收集到的数据的映射。由于上下文是嵌套的,因此每次父上下文都会包含其子上下文的数据。因此,顶层上下文返回整体分析;无需手动汇总数据。
过滤器
过滤器,就像Bent先生中的大多数东西一样,是函数的包装器。这默认为可调用项的点分名称,但可以使用替代的应用程序特定名称。这对于用于渲染多个不同逻辑内容块的函数特别有用。
这看起来像
mr.bent.wrapper.mkfilter(take.me.to.the.foo.bar)
具体示例
在这个例子中,我们有一个渲染包含逻辑上不同的文件片段的HTML页面的应用程序,这些文件片段随后被包含到主页面中。
示例 1
.-------------.
| Top level |
`-------------'
|
| .--------------------.
|---------| Left hand column |
| `--------------------'
| |
| | .-------------.
| |--------------| Login box |
| | `-------------'
| |
| |
| | .------------------.
| `--------------| Navigation box |
| `------------------'
|
| .-----------------.
|---------| Content block |
| `-----------------'
|
|
| .---------------------.
`---------| Right hand column |
`---------------------'
|
| .----------------.
`--------------| Calendar box |
`----------------'
在这个系统中,我们有以下概念插件(为简洁起见使用简称)
- t:
计时插件 此插件返回调用和停止之间的毫秒数
- d:
数据库访问计数插件 此插件返回从数据库检索数据次数。
返回值可能如下所示
{'t': [5, 15, 85, 25], 'd': [0, 1, 2, 8]}
.-------------.
| Top level |
`-------------'
| {'t': [5, 15], 'd': [0,1]}
| .--------------------.
|---------| Left hand column |
| `--------------------'
| | {'t': [5], 'd': [0]}
| | .-------------.
| |--------------| Login box |
| | `-------------'
| |
| | {'t': [15], 'd': [1]}
| | .------------------.
| `--------------| Navigation box |
| `------------------'
| {'t': [85], 'd': [2]}
| .-----------------.
|---------| Content block |
| `-----------------'
|
| {'t': [25], 'd': [8]}
| .---------------------.
`---------| Right hand column |
`---------------------'
| {'t': [25], 'd': [8]}
| .----------------.
`--------------| Calendar box |
`----------------'
因此,用户在其定义的每个级别都有数据,然后可以按自己的喜好处理这些数据。
让我们再次作为doctest看一下(对不起,Florian!)
>>> from mr.bent.mavolio import create, destroy, current
>>> create("top") # Create the top level context
>>> create("lefthand") # Create the left hand column
>>> create("login") # and the login portlet
>>> current() # show that it's an empty context
{}
>>> current()['t'] = [5] # Simulate plugin results being added to context
>>> current()['d'] = [0]
>>> destroy() # Leave context
{'t': [5], 'd': [0]}
>>> create("nav") # Create nav
>>> current()['t']=[15]
>>> current()['d']=[1]
>>> destroy() # Leave nav
{'t': [15], 'd': [1]}
>>> destroy() # Leave left hand column
{'t': [5, 15], 'd': [0, 1]}
>>> create("content") # Enter content block
>>> current()['t'] = [85]
>>> current()['d'] = [2]
>>> destroy() # Leave content block
{'t': [85], 'd': [2]}
>>> create("righthand") # Enter right hand column
>>> create("cal") # Enter calendar box
>>> current()['t']=[25]
>>> current()['d']=[8]
>>> destroy() # Leave calendar
{'t': [25], 'd': [8]}
>>> destroy() # Leave right hand column
{'t': [25], 'd': [8]}
>>> destroy() # Leave the top level context, get totals
{'t': [5, 15, 85, 25], 'd': [0, 1, 2, 8]}
方法引用
实用方法
- mr.bent.wrapper.mkwrapper(function, plugin, name):
使用名称将插件包装在函数周围
- mr.bent.wrapper.mkcontext(function, callback):
在调用时包装函数以创建新的上下文,并在完成时关闭它,并将数据交给回调处理。
- mr.bent.wrapper.mkfilter(function):
将函数包装为上下文报告数据可以过滤的键。
底层方法
- mr.bent.mavolio.create(name):
创建一个名为 name 的新上下文。
- mr.bent.mavolio.destroy():
结束当前上下文并返回统计信息。
- mr.bent.mavolio.current():
返回当前,正在进行的上下文字典。
变更日志
1.0a1 - 未发布
初始发布 [matthewwilkes, fschulze, witsch]
项目详情
mr.bent-1.0a1.zip 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 738802d4c678bc79fb3f5327ca2563c475b0948f4de20b0650c8490b2f32aea5 |
|
| MD5 | 0422d8a28383df9c97ec11c6216aac3b |
|
| BLAKE2b-256 | 3c2c7e83ba5bfc668f9125f519129bda9024efea76faceab289707c737c531f0 |