类、函数和元类装饰器 -- 即使在Python 2.3中(现在带有生成代码的源调试)!
项目描述
想要使用装饰器,但仍然需要支持Python 2.3?希望您能够使用类装饰器、装饰任意赋值或将装饰函数签名匹配到其原始函数?想要在不创建元类的情况下获得元类功能?同步方法怎么样?
“DecoratorTools”为您提供了所有这些以及更多。一些快速示例
# Method decorator example from peak.util.decorators import decorate class Demo1(object): decorate(classmethod) # equivalent to @classmethod def example(cls): print "hello from", cls # Class decorator example from peak.util.decorators import decorate_class def my_class_decorator(): def decorator(cls): print "decorating", cls return cls decorate_class(decorator) class Demo2: my_class_decorator() # "decorating <class Demo2>" will be printed when execution gets here
安装DecoratorTools(使用"easy_install DecoratorTools"或"setup.py install")将使您能够访问peak.util.decorators模块。这个模块中的工具已经捆绑在PEAK、PyProtocols、RuleDispatch和zope.interface包中多年,因此已经被广泛使用和测试。(当然也包括单元测试。)
此独立版本与捆绑版本向后兼容,因此您可以混合使用此包中的装饰器与zope.interface、TurboGears等提供的装饰器。
有关完整文档,请参阅DecoratorTools手册。
自1.7版本以来的更改
@template_function 装饰器现在支持使用返回值而不是文档字符串,以便与Python的“-OO”选项一起使用;强烈建议您更新模板函数以使用返回值而不是文档字符串。(对于缺失文档字符串的情况,错误消息也进行了改进。)
修复了Python 2.6+中混入抽象类(例如collections.Sequence)的classy子类中的元类冲突。
自1.6版本以来的更改
添加了synchronized装饰器,以支持在方法执行期间锁定对象。
自1.5版本以来的更改
添加了classy基类,允许您在不实际需要元类的情况下执行最常用的元类行为。
自1.4版本以来的更改
添加了 enclosing_frame() 函数,以便在由 DecoratorTools 函数调用时,调用 DecoratorTools 函数的复杂装饰器可以正常工作。
自版本 1.3 以来的更改
增加了对生成代码的调试支持,包括由 rewrap() 和 template_function 生成的代码。
自版本 1.2 以来的更改
添加了 rewrap() 函数和 template_function 装饰器,以支持装饰函数的签名匹配。(这些功能类似于 Michele Simionato 的“decorator”包所提供的功能,但不需要 Python 2.4,并且不会改变创建装饰器函数的标准习惯。)
除非 allow_duplicates 参数为 true,否则 decorate_class() 将不会应用重复的类装饰器回调。
自版本 1.1 以来的更改
修复了不同结构类型实例可能相等的问题
自版本 1.0 以来的更改
使用 struct() 装饰器通过装饰构造函数,可以轻松创建类似元组的自定义数据结构类型。
项目详情
DecoratorTools-1.8.zip 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | da73f03a9fab80a205c8febcecee83f8a989eab8ce81affd7989210e4a3d9ea8 |
|
MD5 | f161004115c3d04ed976c230c8a91d87 |
|
BLAKE2b-256 | 86788de8bc5f64c125d17be429c5da33344bf1690f745f121737d5b6fbfe828c |