类、函数和元类装饰器——甚至在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我试图使用@OneToOne作为双向映射来映射实体,但却遇到了奇怪的异常   性能Java异步如何工作?异步方法似乎不是异步运行的   java这个代码可以更短吗   线程“main”Java中的csv Java ArrayList异常。lang.NegativeArraySizeException:28   java确定LayoutManager预布局中的显示视图   java如何在FirestorePagingAdapter中通过方法onLoadingStateChanged隐藏/显示进度条?   在Java中,如何打印一个类似于中间有“过道”的座位表的2d数组?   http Java实现字节范围服务,而不使用仅使用Java api的servlet   java无法使用命名根元素生成json   java如何在注销侦听器中获取http会话id?   数组内部输入(java)?   java如何为特殊情况提供更简单的构造函数   java在swing应用程序中显示JavaFX后台   java如何启用系统。出来在eclipse中运行Junit测试时使用println()?   如何在Java中实现Oracle用户定义的聚合函数