类、函数和元类装饰器——甚至在Python2.3中(现在已经对生成的代码进行了源代码调试)!

DecoratorTools的Python项目详细描述


想要使用decorators,但仍然需要支持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、涡轮齿轮等

有关完整文档,请参见DecoratorTools manual

版本1.7之后的更改:

  • The ^{tt4}$ decorator now supports using a return value instead of a docstring, in order to work with the “-OO” option to Python; it’s highly recommended that you update your template functions to use a return value instead of a docstring. (The error message has also been improved for the missing docstring case.)
  • Fixed metaclass collisions in ^{tt5}$ subclasses that mix in abstract classes (e.g. ^{tt6}$) in Python 2.6+.

版本1.6之后的更改:

  • Added ^{tt7}$ decorator to support locking objects during method execution.

版本1.5之后的更改:

  • Added ^{tt5}$ base class that allows you to do the most often-needed metaclass behviors without needing an actual metaclass.

版本1.4之后的更改:

  • Added ^{tt9}$ function, so that complex decorators that call DecoratorTools functions while being called by DecoratorTools functions, will work correctly.

版本1.3之后的更改:

  • Added support for debugging generated code, including the code generated by ^{tt10}$ and ^{tt11}$.

版本1.2之后的更改:

  • Added ^{tt10}$ function and ^{tt11}$ decorator to support signature matching for decorated functions. (These features are similar to the ones provided by Michele Simionato’s “decorator” package, but do not require Python 2.4 and don’t change the standard idioms for creating decorator functions.)
  • ^{tt14}$ will no longer apply duplicate class decorator callbacks unless the ^{tt15}$ argument is true.

版本1.1之后的更改:

  • Fixed a problem where instances of different struct types could equal each other

版本1.0之后的更改:

  • The ^{tt16}$ decorator makes it easy to create tuple-like data structure types, by decorating a constructor function.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Rest DSL路由无法启动路由,因为同一端点不允许有多个使用者   jvm有没有像JConsole或VisualVM这样的工具可以告诉我“类、对象、引用变量在java中存储在哪里?”   java为什么我的列表中的所有元素看起来都一样?   java运行时。运行shell脚本的exec无法打开文件   JPopupMenu的JMenuItem的java热键   会话在Java中被覆盖   如何在java中去除字符串中的尖锐重音?   java Mockito监视一个接口,模拟它的默认方法,获取NullPointerException   javascript如何在jquery中禁用同一时间选择   将数组列表附加到现有CSV文件,但它会附加并清除存储在Java文件中的以前的数据   java从我的jar中访问pdf文件   java Sonar issue参数必须为非Null,但标记为可为Null   java Salesforce流式API:在网络故障之后和重新订阅之前获取事件   在Java 1.8.0_65上运行Play2.0应用程序时出现playframework错误   java为什么字母的ASCII由方法自动转换。toCharArray()?   java如何知道JDialog是否关闭?   java向服务器发送POST请求,服务器的响应为null,启动   java如何设置JTable中特定单元格的值?   ImagePlus中的java保存问题