轻松创建可以访问原始函数的fqn的多用途装饰器。
fqn-decorators的Python项目详细描述
fqn装饰器图片::https://secure.travis-ci.org/kpn-digital/py-fqn-decorators.svg?branch=master
:目标:http://travis-ci.org/kpn-digital/py-fqn-decorators?分支=主
…图片::https://img.shields.io/codecov/c/github/kpn digital/py fqn decorators/master.svg
:目标:http://codecov.io/github/kpn-digital/py-fqn-decorators?分支=主
…图片::https://img.shields.io/pypi/v/fqn decorators.svg
:目标:https://pypi.python.org/pypi/fqn decorators
…图片::https://readthedocs.org/projects/fqn-decorators/badge/?version=latest
:目标:http://fqn-decorators.readthedocs.org/en/latest/?徽章=最新安装在命令行:
$pip install fqn decorators
结束安装
开始使用
…py:currentmodule::fqn_decorators.decorators通过扩展:类:`~decorator`类,您可以创建简单的decorators。
实现:meth:`~decorator.before`和/或:meth:`~decorator.after`方法以在执行修饰项之前或之后执行操作。
:meth:`~decorator.before`方法可以通过更改:attr:`~decorator.args`和:attr:`~decorator.kwargs`属性来访问装饰项的参数。
:meth:`~decorator.after`方法可以使用:attr:`~decorator.result`属性访问或更改结果。
:meth:`~decorator.exception`方法可以用于do出现了一个异常。
在所有三个方法中:attr:`~decorator.fqn`和:attr:`~decorator.func`属性都可用。
simple decorator
---------
create a simple decorator::
import fqn戥decorators
import time
时间(fqn_decorators.decorator):
def before(self):
self.start=time.time()
def after(self):
duration=time.time()-self.start
打印({0}用了{1}秒)。格式(self.fqn,持续时间)
@time-it
def my-u function():
time.sleep(1)
>; u main-u.my-u function花了1.0029397903秒
注意:
无法使用*非关键字*参数创建装饰符。
若要创建支持非关键字参数的装饰符,请参见:ref:`advanced usage<;usage\u advanced\u non-keyword\u decorators>;`节。
示例:
import fqn_decorators
import time
类阈值(fqn_decorators.decorator):
self.start=time.time()
def after(self):
duration=time.time()-self.start
treshold=self.params.get('threshold')
如果threshold和duration>;threshold:
引发异常('execution花费的时间比threshold长')
@threshold(threshold=2)
def my function():
time.sleep(3)
>;my_function()
异常:执行时间超过阈值
async decorator
----
还支持装饰协同路由(或任何可等待的),仅限python>;=3.5。
只继承自
:类:`~fqn_decorators.async.asyncDecorator`而不是。
示例::
import asyncio
import time
from fqn_decorators.async import asyncDecorator
def after(self):
duration=time.time()-self.start
print({0}用了{1}秒)。format(self.fqn,duration))
@time\u it\u async
async def coro():
等待asyncio.sleep(1)
>;loop=asyncio.get\u event\u loop()
>;>loop.run until\u complete(coro())
_主考罗花了1.001493215560913秒。结束使用
:目标:http://travis-ci.org/kpn-digital/py-fqn-decorators?分支=主
…图片::https://img.shields.io/codecov/c/github/kpn digital/py fqn decorators/master.svg
:目标:http://codecov.io/github/kpn-digital/py-fqn-decorators?分支=主
…图片::https://img.shields.io/pypi/v/fqn decorators.svg
:目标:https://pypi.python.org/pypi/fqn decorators
…图片::https://readthedocs.org/projects/fqn-decorators/badge/?version=latest
:目标:http://fqn-decorators.readthedocs.org/en/latest/?徽章=最新安装在命令行:
$pip install fqn decorators
结束安装
开始使用
…py:currentmodule::fqn_decorators.decorators通过扩展:类:`~decorator`类,您可以创建简单的decorators。
实现:meth:`~decorator.before`和/或:meth:`~decorator.after`方法以在执行修饰项之前或之后执行操作。
:meth:`~decorator.before`方法可以通过更改:attr:`~decorator.args`和:attr:`~decorator.kwargs`属性来访问装饰项的参数。
:meth:`~decorator.after`方法可以使用:attr:`~decorator.result`属性访问或更改结果。
:meth:`~decorator.exception`方法可以用于do出现了一个异常。
在所有三个方法中:attr:`~decorator.fqn`和:attr:`~decorator.func`属性都可用。
simple decorator
---------
create a simple decorator::
import fqn戥decorators
import time
时间(fqn_decorators.decorator):
def before(self):
self.start=time.time()
def after(self):
duration=time.time()-self.start
打印({0}用了{1}秒)。格式(self.fqn,持续时间)
@time-it
def my-u function():
time.sleep(1)
>;
注意:
无法使用*非关键字*参数创建装饰符。
若要创建支持非关键字参数的装饰符,请参见:ref:`advanced usage<;usage\u advanced\u non-keyword\u decorators>;`节。
示例:
import fqn_decorators
import time
类阈值(fqn_decorators.decorator):
def after(self):
duration=time.time()-self.start
treshold=self.params.get('threshold')
如果threshold和duration>;threshold:
引发异常('execution花费的时间比threshold长')
@threshold(threshold=2)
def my function():
time.sleep(3)
>;my_function()
异常:执行时间超过阈值
async decorator
----
还支持装饰协同路由(或任何可等待的),仅限python>;=3.5。
只继承自
:类:`~fqn_decorators.async.asyncDecorator`而不是。
示例::
import asyncio
import time
from fqn_decorators.async import asyncDecorator
def after(self):
duration=time.time()-self.start
print({0}用了{1}秒)。format(self.fqn,duration))
@time\u it\u async
async def coro():
等待asyncio.sleep(1)
>;loop=asyncio.get\u event\u loop()
>;>loop.run until\u complete(coro())
_主考罗花了1.001493215560913秒。结束使用