调用python函数的库,其参数在运行时由名称决定。
calllib的Python项目详细描述
概述
calllib提供3个函数,apply,getargs,和 inspect_params。这些函数用于检查和调用 参数事先未知的python函数,但是 在运行时根据可用对象的映射确定。
这在插件框架中特别有用,而您不需要 希望每个回调函数都必须具有相同的 签名。相反,每个函数都可以取可用的 参数。
例如:
>>> from __future__ import print_function >>> import calllib >>> def callback1(time): ... print('callback1 called at:', time) >>> def callback2(time, reason): ... print('callback2 called at:', time, 'reason:', reason) # register the callbacks >>> callbacks = [callback1, callback2] # elsewhere: compute the total universe of possible # callback arguments >>> args = {'time': 'noon', 'reason': 'abort'} >>> for callback in callbacks: ... calllib.apply(callback, args) # execute each callback callback1 called at: noon callback2 called at: noon reason: abort
最后一行显示您可以调用任何回调例程 知道它的确切参数,只要它的参数是 可用的参数。
应用()
apply(callable, args)
- callable - Any callable object that can be inspected with the inspect module.
- args - A mapping object, typically a dict, that contains the available arguments that will be passed to callable.
callable is inpsected with ^{tt3}$ and the its parameters are extracted by name. For each parameter the corresponding value is retrieved from args by name and passed to the callable.
^{tt2}$ returns the result of executing callable with the computed arguments.
getargs()
getargs(callable, args)
- callable - Any callable object that can be inspected with the inspect module.
- args - A mapping object, typically a dict, that contains the available arguments that could be passed to callable.
callable is inspected to determine its parameters. For each parameter the corresponding value is retrieved from args. If a parameter is not found in args callable has a default value for that parameter, the default value is retrieved.
^{tt3}$ returns a list of actual argument values that would be passed to callable.
检查_params()
inspect_params(callable)
- callable - Any callable object that can be inspected with the inspect module.
callable is inspected to deterine its parameters and default values, if any. ^{tt4}$ returns a tuple containing a list of parameter names and a dict with default values, if any. For example:
>>> def foo(x, y=0, z=6): pass ... >>> calllib.inspect_params(foo) (['x', 'y', 'z'], {'y': 0, 'z': 6}) >>> class Baz(object): ... def __init__(self, x, y='hello'): pass ... >>> calllib.inspect_params(Baz) (['x', 'y'], {'y': 'hello'})
支持的可调用类型
calllib支持任何用python编写的可调用。这包括 函数、绑定和解除绑定的方法、类和对象实例 与会员通话。
因为它们不可由内置的inspect模块进行内省 python函数如len不能与apply一起使用。
默认参数
具有默认参数的函数完全受 calllib。如果参数未在args参数中指定 对于apply并且它具有默认值,将使用默认值。
测试
要测试,请运行“python setup.py test”。在python>;=3.0上,它还运行doctests。
更改日志
1.8 2016年10月27日Eric V.Smith
- 删除更改RPM名称的黑客程序(问题7)。
- 始终需要安装工具(第6版)。
- 没有代码更改。
1.7 2015-05-16埃里克V.史密斯
- 删除了“test”包,因此bdist不会安装它。它还是 包括在SDists中。
- 没有代码更改。
1.6 2015年5月15日Eric V.Smith
- 如果使用Python3运行,则将RPM名称更改为python3 calllib。
- 没有代码更改。
1.5 2014-12-07埃里克v.史密斯
- 增加了检查参数(第5期)。
1.4 2014-07-24埃里克诉史密斯
- 发布版本1.4。没有代码更改。
- 在运行测试套件时添加readme.txt条目。
- 修复sdist中缺少的test/\uu init\uu.py。
2014年3月14日Eric V.Smith
- 将manifest.in添加到manifest.in,这样它将包含在sdists中 (第4期)。
1.2 2014年2月12日Eric V.Smith
- 新版本只是为了更新开发状态分类器。
1.1 2014-02-12埃里克V.史密斯
- 生成一个名为python calllib的RPM(问题3)。
- 支持python3(问题2)。
- 将测试移动到单独的模块(问题1)。
1.0 2011年11月10日Eric V.Smith
- 最终确定的API。
- 为派生类添加测试。
0.2 2011年11月10日Eric V.Smith
- 允许没有初始化方法的类。
- 规范化测试名称。
0.1 2011-11-09埃里克诉史密斯
- 初次发布。