This is a modern lightweight Python decorator utility module.
Project description
简述
adorner 是一个现代轻量级 Python 装饰器辅助模块。
本项目采用 MIT 许可证。请查看 LICENSE 文件以了解更多信息。
特性
-
充分利用 Python 面向对象的基本语法
-
内置 4 个常用的辅助类,具备以下功能:
Decorator:标记装饰器函数,使装饰器的构造更简单Timer:函数计时装饰器类Cacher:函数缓存装饰器类Retryer:函数异常重试处理类
-
为上层开发者提供更便利、灵活的装饰器辅助,让代码更少、更精
安装
1. 本地
如果您不想从源代码安装,您可以直接安装发布到 GitHub Releases 页面的打包版本。
首先,下载最新的发布包 gpa-obscuror-x.x.x.tar.gz 从 GitHub Releases 页面。
接着,您可以使用 pip 等包管理工具来安装下载的 .tar.gz 文件:
pip install ./adorner-x.x.x.tar.gz
其中 x.x.x 为您下载 .tar.gz 文件确版本,并确保指定安装包文件路径是正确的。
2. 网络
pip install adorner
假如上述命令执行后发生了一些错误,可以选择使用pip3命令或者添加镜像源等等方式解决。
快速使用
安装完本模块后,您可以按照以下方式使用 adorner:
from adorner import Decorator
@Decorator
def timing_decorator(self: Decorator):
"""
记录执行时间的装饰器
:param self: 装饰器 Decorator 实例
:return: 被修饰函数的执行结果
"""
start_time = time.perf_counter()
result = self.execute()
end_time = time.perf_counter()
print(f"执行时间: {end_time - start_time:.8f} 秒")
return result
@Decorator
def exception_decorator(self: Decorator):
"""
捕获异常日志的装饰器
:param self: 装饰器 Decorator 实例
:return: 被修饰函数的执行结果
"""
try:
result = self.execute()
return result
except Exception as e:
print(f"捕获异常: {e}")
raise
@timing_decorator
@exception_decorator
def process_data(data, sleep: int = 0):
if sleep:
time.sleep(sleep)
if not data:
raise ValueError("数据不能为空")
print(f"处理数据: {data}")
return f"结果: {data}"
result = process_data("有效数据") # 正常的执行
print(result)
process_data("") # 试错
如果你想要了解更多的使用案例,请参考这篇文章:adorner 使用示例
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file adorner-1.0.1.tar.gz.
File metadata
- Download URL: adorner-1.0.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.8.10 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c797fe377c93ae3c4801dc16d95ea3d538b589ce7aa1e929b36b0be775e089e
|
|
| MD5 |
54cc9efcd4e5cbd54c1e41765130e97e
|
|
| BLAKE2b-256 |
619758078983649b9320c871fe14d1afd878410f81d96d8ad6ffd5b6745b3455
|
File details
Details for the file adorner-1.0.1-py3-none-any.whl.
File metadata
- Download URL: adorner-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.8.10 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d4bfae27d8bb083f1f622b7a2db3fc1b0d3d0867cbbd7d85302b93ccae67d29
|
|
| MD5 |
0a0b6e6b3af618eac546f353588fa46e
|
|
| BLAKE2b-256 |
d13ba80b3c4d6f99e625de474277b97f20fb63578a6a8ec362d2fc18d83122cb
|