Name | base-decorator JSON |
Version |
0.2
JSON |
| download |
home_page | |
Summary | base_decorator,make decorator easy to write |
upload_time | 2023-06-06 02:35:16 |
maintainer | ydf |
docs_url | None |
author | bfzs |
requires_python | |
license | BSD License |
keywords |
base_decorator
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# base_decorator
pip install base_decorator
```
通用的装饰器基类,使写装饰器变得更简单。
```
```python
import functools
import abc
import sys
class Undefind:
pass
class BaseDecorator(metaclass=abc.ABCMeta):
"""
简化了装饰器的编写。
用户的装饰器需要继承这个,用户可以按需重新定义 before,after,when_exception 方法。
为了一致性和省事,统一采用有参数装饰器,用户的装饰器后面必须带括号。
"""
# def __init__(self, *args, **kwargs):
# pass
raw_fun = Undefind()
raw_result = Undefind()
exc_info = Undefind()
final_result = Undefind() # 用户可以自己定义final_result的值,如果定义了就把这个值作为函数的结果,否则把函数原始结果作为结果。
def __call__(self, fun, *args, **kwargs):
# print(locals())
if not callable(fun) or args or kwargs: # 正常是只有fun一个参数,除非是装饰器没加括号造成的。
raise ValueError('为了简单和一致起见,所有装饰器都采用有参数装饰器,被装饰函数上面的装饰器后面别忘了加括号')
self.raw_fun = fun
f = functools.partial(BaseDecorator._execute, self) # 比 self.execute 利于补全
functools.update_wrapper(f, fun, )
return f
def _execute(self, *args, **kwargs):
self.before()
try:
self.raw_result = self.raw_fun(*args, **kwargs)
self.after()
except Exception as e:
self.exc_info = sys.exc_info()
self.when_exception()
if not isinstance(self.final_result, Undefind): # 用户可以自己定义final_result的值,如果定义了就把这个值作为函数的结果。
return self.final_result
else:
return self.raw_result
def before(self):
pass
def after(self):
pass
def when_exception(self):
# print(self.exc_info) # (<class 'ZeroDivisionError'>, ZeroDivisionError('division by zero',), <traceback object at 0x000001D22BA3FD48>)
raise self.exc_info[1]
if __name__ == '__main__':
import nb_log # noqa
class MyDeco(BaseDecorator):
def __init__(self, a=5, b=6):
self.a = a
self.b = b
def before(self):
print('开始执行')
# noinspection PyAttributeOutsideInit
def after(self):
self.final_result = self.a * self.b * self.raw_result
def common_deco(a=5, b=6):
""" 上面的逻辑如果用常规方式写"""
def _inner(f):
@functools.wraps(f)
def __inner(*args, **kwargs):
try:
print('开始执行')
result = f(*args, **kwargs)
return a * b * result
except Exception as e:
raise e
return __inner
return _inner
@MyDeco(b=4)
# @common_deco(b=4)
def fun3(x):
print(x)
return x * 2
print(type(fun3))
print(fun3)
print(fun3.__wrapped__) # noqa
print(fun3(10))
```
Raw data
{
"_id": null,
"home_page": "",
"name": "base-decorator",
"maintainer": "ydf",
"docs_url": null,
"requires_python": "",
"maintainer_email": "ydf0509@sohu.com",
"keywords": "base_decorator",
"author": "bfzs",
"author_email": "ydf0509@sohu.com",
"download_url": "https://files.pythonhosted.org/packages/a9/f8/4104a6e096ee54bc39e31297b21e67b8e9872109951a816c5cf91a7176ec/base_decorator-0.2.tar.gz",
"platform": "all",
"description": "# base_decorator\r\n\r\npip install base_decorator\r\n```\r\n\u901a\u7528\u7684\u88c5\u9970\u5668\u57fa\u7c7b\uff0c\u4f7f\u5199\u88c5\u9970\u5668\u53d8\u5f97\u66f4\u7b80\u5355\u3002\r\n```\r\n\r\n```python\r\nimport functools\r\nimport abc\r\nimport sys\r\n\r\n\r\nclass Undefind:\r\n pass\r\n\r\n\r\nclass BaseDecorator(metaclass=abc.ABCMeta):\r\n \"\"\"\r\n \u7b80\u5316\u4e86\u88c5\u9970\u5668\u7684\u7f16\u5199\u3002\r\n\r\n \u7528\u6237\u7684\u88c5\u9970\u5668\u9700\u8981\u7ee7\u627f\u8fd9\u4e2a\uff0c\u7528\u6237\u53ef\u4ee5\u6309\u9700\u91cd\u65b0\u5b9a\u4e49 before\uff0cafter\uff0cwhen_exception \u65b9\u6cd5\u3002\r\n\r\n \u4e3a\u4e86\u4e00\u81f4\u6027\u548c\u7701\u4e8b\uff0c\u7edf\u4e00\u91c7\u7528\u6709\u53c2\u6570\u88c5\u9970\u5668\uff0c\u7528\u6237\u7684\u88c5\u9970\u5668\u540e\u9762\u5fc5\u987b\u5e26\u62ec\u53f7\u3002\r\n\r\n\r\n \"\"\"\r\n\r\n # def __init__(self, *args, **kwargs):\r\n # pass\r\n\r\n raw_fun = Undefind()\r\n raw_result = Undefind()\r\n exc_info = Undefind()\r\n final_result = Undefind() # \u7528\u6237\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49final_result\u7684\u503c\uff0c\u5982\u679c\u5b9a\u4e49\u4e86\u5c31\u628a\u8fd9\u4e2a\u503c\u4f5c\u4e3a\u51fd\u6570\u7684\u7ed3\u679c\uff0c\u5426\u5219\u628a\u51fd\u6570\u539f\u59cb\u7ed3\u679c\u4f5c\u4e3a\u7ed3\u679c\u3002\r\n\r\n def __call__(self, fun, *args, **kwargs):\r\n # print(locals())\r\n if not callable(fun) or args or kwargs: # \u6b63\u5e38\u662f\u53ea\u6709fun\u4e00\u4e2a\u53c2\u6570\uff0c\u9664\u975e\u662f\u88c5\u9970\u5668\u6ca1\u52a0\u62ec\u53f7\u9020\u6210\u7684\u3002\r\n raise ValueError('\u4e3a\u4e86\u7b80\u5355\u548c\u4e00\u81f4\u8d77\u89c1\uff0c\u6240\u6709\u88c5\u9970\u5668\u90fd\u91c7\u7528\u6709\u53c2\u6570\u88c5\u9970\u5668\uff0c\u88ab\u88c5\u9970\u51fd\u6570\u4e0a\u9762\u7684\u88c5\u9970\u5668\u540e\u9762\u522b\u5fd8\u4e86\u52a0\u62ec\u53f7')\r\n self.raw_fun = fun\r\n f = functools.partial(BaseDecorator._execute, self) # \u6bd4 self.execute \u5229\u4e8e\u8865\u5168\r\n functools.update_wrapper(f, fun, )\r\n return f\r\n\r\n def _execute(self, *args, **kwargs):\r\n self.before()\r\n try:\r\n self.raw_result = self.raw_fun(*args, **kwargs)\r\n self.after()\r\n except Exception as e:\r\n self.exc_info = sys.exc_info()\r\n self.when_exception()\r\n if not isinstance(self.final_result, Undefind): # \u7528\u6237\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49final_result\u7684\u503c\uff0c\u5982\u679c\u5b9a\u4e49\u4e86\u5c31\u628a\u8fd9\u4e2a\u503c\u4f5c\u4e3a\u51fd\u6570\u7684\u7ed3\u679c\u3002\r\n return self.final_result\r\n else:\r\n return self.raw_result\r\n\r\n def before(self):\r\n pass\r\n\r\n def after(self):\r\n pass\r\n\r\n def when_exception(self):\r\n # print(self.exc_info) # (<class 'ZeroDivisionError'>, ZeroDivisionError('division by zero',), <traceback object at 0x000001D22BA3FD48>)\r\n raise self.exc_info[1]\r\n\r\n\r\nif __name__ == '__main__':\r\n import nb_log # noqa\r\n\r\n\r\n class MyDeco(BaseDecorator):\r\n def __init__(self, a=5, b=6):\r\n self.a = a\r\n self.b = b\r\n\r\n def before(self):\r\n print('\u5f00\u59cb\u6267\u884c')\r\n\r\n # noinspection PyAttributeOutsideInit\r\n def after(self):\r\n self.final_result = self.a * self.b * self.raw_result\r\n\r\n\r\n def common_deco(a=5, b=6):\r\n \"\"\" \u4e0a\u9762\u7684\u903b\u8f91\u5982\u679c\u7528\u5e38\u89c4\u65b9\u5f0f\u5199\"\"\"\r\n\r\n def _inner(f):\r\n @functools.wraps(f)\r\n def __inner(*args, **kwargs):\r\n try:\r\n print('\u5f00\u59cb\u6267\u884c')\r\n result = f(*args, **kwargs)\r\n return a * b * result\r\n except Exception as e:\r\n raise e\r\n\r\n return __inner\r\n\r\n return _inner\r\n\r\n\r\n @MyDeco(b=4)\r\n # @common_deco(b=4)\r\n def fun3(x):\r\n print(x)\r\n return x * 2\r\n\r\n\r\n print(type(fun3))\r\n print(fun3)\r\n print(fun3.__wrapped__) # noqa\r\n print(fun3(10))\r\n \r\n```\r\n\r\n",
"bugtrack_url": null,
"license": "BSD License",
"summary": "base_decorator,make decorator easy to write",
"version": "0.2",
"project_urls": null,
"split_keywords": [
"base_decorator"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a9f84104a6e096ee54bc39e31297b21e67b8e9872109951a816c5cf91a7176ec",
"md5": "fa8b1ed08abc63a9481da2f00d0b9377",
"sha256": "82e444432c4e33496fc24e7e367b708531cbae42ee79c0e71ca6148efd3c21b8"
},
"downloads": -1,
"filename": "base_decorator-0.2.tar.gz",
"has_sig": false,
"md5_digest": "fa8b1ed08abc63a9481da2f00d0b9377",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3054,
"upload_time": "2023-06-06T02:35:16",
"upload_time_iso_8601": "2023-06-06T02:35:16.490808Z",
"url": "https://files.pythonhosted.org/packages/a9/f8/4104a6e096ee54bc39e31297b21e67b8e9872109951a816c5cf91a7176ec/base_decorator-0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-06 02:35:16",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "base-decorator"
}