Skip to main content

简述

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 使用示例

Metadata

Release files for adorner 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for adorner 1.0.1
File Size Uploaded Provenance
adorner-1.0.1.tar.gz 5.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for adorner 1.0.1
File Interpreter ABI Platform
adorner-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.0 kB

Release files / adorner-1.0.1.tar.gz

Download URL adorner-1.0.1.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5c797fe377c93ae3c4801dc16d95ea3d538b589ce7aa1e929b36b0be775e089e
BLAKE2b-256 checksum
How to use checksums
619758078983649b9320c871fe14d1afd878410f81d96d8ad6ffd5b6745b3455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.10 Windows/10

Release files / adorner-1.0.1-py3-none-any.whl

Download URL adorner-1.0.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d4bfae27d8bb083f1f622b7a2db3fc1b0d3d0867cbbd7d85302b93ccae67d29
BLAKE2b-256 checksum
How to use checksums
d13ba80b3c4d6f99e625de474277b97f20fb63578a6a8ec362d2fc18d83122cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.10 Windows/10

Security for adorner 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page