本特先生知道他的号码。
mr.bent的Python项目详细描述
一个穿着无可挑剔的黑色夹克和细条纹裤子的挑剔的小男人。
简介
bent先生是一个允许在python中收集配置文件数据的框架 应用程序,并在不同的逻辑级别查看。三个概念 涉及的是一个plugin,一段描述应用程序的代码,一个 context,要为其报告数据的逻辑代码块,以及 filter,一种获取有关 上下文来自。
插件
插件是提供给“mkwrapper”函数的可调用函数,该函数 将其应用于应用程序中的函数。
这看起来像:
mr.bent.wrapper.mkwrapper(foo.bar, plugincallable, "myplugin")
这将导致每次调用 “foo.bar”,并将插件的结果添加到当前上下文中 “我的插件”。
插件可以返回数字或iterable。如果它返回一个iterable 它必须包含字符串或数字。返回数字的情况 被认为等同于返回长度为1的整数。
上下文
上下文存储插件生成的数据。在任何时候,新的上下文都可以是 已启动,它将是当前活动上下文的“子上下文”。如果 当前没有活动上下文将创建新的顶级上下文。
上下文是用创建它们的函数的虚线名称命名的 并将其数据返回回回调。
这看起来像:
def mycallback(context, result, stats): return "%s <!-- %s -->" % (result, `stats`) mr.bent.wrapper.mkcontext(bar.foo, mycallback)
这个例子将调用bar.foo,一个返回XML的函数, 在下面的注释中返回包含上下文dict的repr的xml。
当上下文结束时,它返回它收集的数据的映射。作为上下文 每次父上下文包含其子上下文的数据时都嵌套。 因此,顶级上下文返回整个概要分析;不需要 手动聚合数据。
过滤器
像本特先生的大多数作品一样,过滤器是函数的包装器。这个 将默认为可调用的虚线名称,但另一种方法是, 可以改为使用特定于应用程序的名称。这对于 用于呈现多个不同逻辑内容块的函数。
这看起来像:
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: | A timing plugin This plugin returns the number of milliseconds between it being invoked and it being stopped |
---|---|
d: | A database access counting plugin This plugin returns how many times data was retrieved from a database. |
返回值可能如下所示:
{'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 | `----------------'
因此,用户在他定义的每个级别都有数据,然后他可以 按他喜欢的方式处理。
让我们以医生的身份再看一遍(抱歉,弗洛里安!):
>>> 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): | |
---|---|
Wraps a function with a plugin which writes its data to the current context as name | |
mr.bent.wrapper.mkcontext(function, callback): | |
Wraps a function to create a new context on invocation, and close it when it finishes, and give the data to callback to handle reporting. | |
mr.bent.wrapper.mkfilter(function): | |
Wraps a function to be a key that context reporting data can be filtered on. |
低级方法
mr.bent.mavolio.create(name): | |
---|---|
Creates a new context called name. | |
mr.bent.mavolio.destroy(): | |
Ends the current context and returns the statistics. | |
mr.bent.mavolio.current(): | |
Returns the current, in progress, context dict. |
变更日志
1.0a1-未发布
- 初次发行 [马修威尔克斯,弗斯丘尔泽,威奇奇]