Python zope.deprecation - zhongjiajie/zhongjiajie.github.com GitHub Wiki

zope.deprecation

This package provides a simple function called deprecated(names, reason) to mark deprecated modules, classes, functions, methods and properties. readthedocs

Usage

function

对函数进行弃用说明,readthedocs使用将例子直接放到zope.deprecationpackage路径的操作,tmp_d = tempfile.mkdtemp('deprecation'); zope.deprecation.__path__.append(tmp_d)这里的例子没有这样做.

# cat deprecate demo.py
from zope.deprecation import deprecated
# in function
def demo1():
    return 1
deprecated('demo1', 'demo1 is no more.')

def demo4():
    return 4
def deprecatedemo4():
    """Demonstrate that deprecated() also works in a local scope."""
    deprecated('demo4', 'demo4 is no more.')

测试deprecated是否可用

>>> import warnings
>>> import demo
>>> with warnings.catch_warnings(record=True) as log:
...     del warnings.filters[:]
...     doctest_ex.demo1()
1
>>> print log[0].category.__name__
DeprecationWarning
>>> print log[0].message
demo1: demo1 is no more.

>>> import demo
>>> with warnings.catch_warnings(record=True) as log:
...     del warnings.filters[:]
...     zope.deprecation.doctest_ex.demo2()
2
>>> print log[0].message
demo2: demo2 is no more.

properties and function in class

from zope.deprecation import deprecation
class MyComponent(object):
    foo = property(lambda self: 1)
    # properties
    foo = deprecation.deprecated(foo, 'foo is no more.')

    bar = 2

    def blah(self):
        return 3
    blah = deprecation.deprecated(blah, 'blah() is no more.')

    def splat(self):
        return 4

    @deprecation.deprecate("clap() is no more.")
    def clap(self):
        return 5

⚠️ **GitHub.com Fallback** ⚠️