Skip to main content

A library to simplify decorators in Python

Project description

好的,这里是一个基于你的建议修改为中文的README.md文件模板:

Easy-Decorator

Easy-Decorator (EasyD) 是一个旨在简化装饰器创建和使用的Python库。它提供了一系列有用的装饰器和工具,帮助您轻松地创建自定义装饰器。

功能特性

  • 使用 decorator_factory 简化装饰器的创建
  • 常见函数装饰器: timer, retry
  • 类装饰器: singleton
  • 异步装饰器: async_retry
  • 可扩展的架构,支持添加自定义装饰器

安装

您可以使用 pip 安装 Easy-Decorator:

pip install Easys-Decorator

快速入门

以下是一个使用 Easy-Decorator 的快速示例:

from EasyD import timer

@timer
def my_function():
    # 您的代码在此
    for i in range(1000000):
        pass

my_function()
# 输出: my_function 用时 0.05 秒

使用方法

装饰器工厂

轻松创建自定义装饰器:

from EasyD import decorator_factory

def before_func():
    print("在函数执行之前")

def after_func(result):
    print(f"在函数执行之后,结果为: {result}")

@decorator_factory(before=before_func, after=after_func)
def my_function():
    return "Hello, World!"

my_function()
# 输出:
# 在函数执行之前
# 在函数执行之后,结果为: Hello, World!

重试装饰器

自动重试失败的函数:

from EasyD import retry
import random

@retry(max_attempts=3, delay=1)
def unreliable_function():
    if random.random() < 0.7:
        raise ValueError("随机错误")
    return "成功!"

result = unreliable_function()
print(result)

单例装饰器

确保一个类只有一个实例:

from EasyD import singleton

@singleton
class DatabaseConnection:
    def __init__(self):
        print("初始化数据库连接")

# 这将只打印一次"初始化数据库连接"
db1 = DatabaseConnection()
db2 = DatabaseConnection()
print(db1 is db2)  # True

贡献

欢迎贡献! 请随时提交 Pull Request。

许可证

本项目采用 MIT 许可证 - 详情请查看 LICENSE 文件。

联系方式

如果您有任何问题或反馈,请在 GitHub 上提交 Issue,或联系 SoulCodingYanhun

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easys_decorator-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

Easys_Decorator-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file easys_decorator-0.1.1.tar.gz.

File metadata

  • Download URL: easys_decorator-0.1.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for easys_decorator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 53cd82372dda9d2f1dbe39222a66af1b2d52b52a535278d1f07ab853546d067e
MD5 c5cd71cdf7a1d6998b790e29527fbbaa
BLAKE2b-256 e6572d23e0a365b279f01317b8ee58c5d5c7460128a61d449b0626e137e0d29c

See more details on using hashes here.

File details

Details for the file Easys_Decorator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for Easys_Decorator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b92d2f4547dd05dee612440360f186717d2b5b2e7f93658e175a4f556489ba8c
MD5 0409660af5599d8fd994b3e18338bac7
BLAKE2b-256 f46f228d08beab11302e87f3fff9e8e27844736605a7011739d802c1f8622128

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page