Skip to main content

Py-Tools

Py-Tools 是一个面向 Python 开发者的实用工具集和可复用组件库,重点解决项目初始化和日常开发里高频、重复、容易写散的基础问题。

适合需要统一配置、日志、HTTP 调用和数据库 CRUD 基础封装的 Python 项目。

设计细节可参考掘金专栏:https://juejin.cn/column/7131286129713610766

安装

从 0.6.0 开始,包名由 hui-tools 调整为 huidevkit。历史包名仍可安装,但不再推荐继续使用。

默认安装:

pip install huidevkit

按需安装:

pip install huidevkit[db-orm, db-redis, excel-tools]

全部能力:

pip install huidevkit[all]

主要能力

  • py_tools.logging:基于 loguru 的日志初始化与默认配置
  • py_tools.utils.ConfigUtil:统一读取环境变量、.env、YAML 和 Settings
  • py_tools.connections.http:同步 / 异步 HTTP 客户端、上传文件、流式读取
  • py_tools.connections.db.mysql:基于 SQLAlchemy 的数据库管理与常见 CRUD 封装
  • py_tools.utils.AsyncUtil:同步异步互转、后台任务执行
  • py_tools.utils:时间、文件、JWT、编码、序列化、树结构等通用工具

顶层 py_tools 包默认不聚合所有子模块,请从对应子模块导入。

常用示例

README 只保留最常用、最能体现封装价值的示例。

1. 快速初始化项目日志

from py_tools.constants import BASE_DIR
from py_tools.logging import logger, setup_logging


setup_logging(log_dir=BASE_DIR / "logs")
logger.info("service started")
logger.error("something happened")

对应示例:demo/logging/logging_demo.py

2. 统一读取配置

from pydantic_settings import SettingsConfigDict

from py_tools.utils import BaseConfigSettings, ConfigUtil


class AppSettings(BaseConfigSettings):
    app_name: str = "py-tools-demo"
    app_port: int = 8000
    debug: bool = False

    model_config = SettingsConfigDict(env_prefix="", env_file=".env", extra="ignore")


settings = ConfigUtil.load_merged_settings(
    AppSettings,
    yaml_file="config.yaml",
    env_file=".env",
    app_name="runtime-app",
)
print(settings.model_dump())

默认优先级:

显式传参(kwargs) > 环境变量 > .env > yaml > 默认值

对应示例:demo/utils/config_util_demo.py

3. HTTP 客户端

import asyncio

from py_tools.connections.http import AsyncHttpClient


async def main():
    url = "https://juejin.cn/"

    resp = await AsyncHttpClient().get(url).execute()
    text_data = await AsyncHttpClient().get(url, params={"test": "hui"}).text()
    json_data = await AsyncHttpClient().post(url, data={"test": "hui"}).json()
    byte_data = await AsyncHttpClient().get(url).bytes()

    print(text_data)
    print(resp.status)
    print(json_data)
    print(byte_data[:10])

    async for chunk in AsyncHttpClient().get(url).stream(chunk_size=512):
        print(chunk)
        break


asyncio.run(main())

对应示例:demo/connections/http_client_demo.py

上传文件:

import asyncio

from py_tools.connections.http import AsyncHttpClient


async def main():
    upload_file_ret = await AsyncHttpClient().upload_file(
        "https://httpbin.org/post",
        file="test.txt",
    ).json()
    print(upload_file_ret)


asyncio.run(main())

4. 数据库 CRUD 封装

import asyncio

from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column

from py_tools.connections.db.mysql import BaseOrmTable, DBManager, SQLAlchemyManager


class UserFileTable(BaseOrmTable):
    __tablename__ = "user_file"

    filename: Mapped[str] = mapped_column(String(100), default="")
    oss_key: Mapped[str] = mapped_column(String(100), default="")


class UserFileManager(DBManager):
    orm_table = UserFileTable


db_client = SQLAlchemyManager(
    host="127.0.0.1",
    port=3306,
    user="root",
    password="123456",
    db_name="db_demo",
)


async def main():
    db_client.init_mysql_engine()
    DBManager.init_db_client(db_client)

    file_id = await UserFileManager().add({"filename": "demo.txt", "oss_key": "demo-key"})
    file_obj = await UserFileManager().query_by_id(file_id)
    file_list = await UserFileManager().query_all(cols=["id", "filename", "oss_key"])

    await UserFileManager().update(
        values={"filename": "demo-updated.txt"},
        conds=[UserFileTable.id == file_id],
    )
    await UserFileManager().delete_by_id(file_id)

    print(file_obj)
    print(file_list)


asyncio.run(main())

对应示例:

更多示例

示例统一放在 demo/ 目录,避免文档示例与代码示例重复维护。

开发与验证

pip install -e .[test]
python -m pytest -q
ruff check .

贡献

  1. Fork 并克隆仓库
  2. 安装依赖
  3. 运行测试与静态检查
  4. 提交 Pull Request

Metadata

Release files for huidevkit 0.6.3

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

Source distribution (sdist)

Source distribution for huidevkit 0.6.3
File Size Uploaded
huidevkit-0.6.3.tar.gz 60.9 kB Details

Built distribution (wheel)

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

Total release size: 146.5 kB

Release files / huidevkit-0.6.3.tar.gz

Download URL huidevkit-0.6.3.tar.gz
Size 60.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6c40dc72e21b4a6cb0a96bc0066a8cadc0b32e2f80ded2be52f27ac8149ae3ad
BLAKE2b-256 checksum
How to use checksums
c93c43e47a0ee2026247d98a51201f1c9ae83bc8582efd5298087c34dd219cd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release files / huidevkit-0.6.3-py3-none-any.whl

Download URL huidevkit-0.6.3-py3-none-any.whl
Size 85.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1885c0cc85a24dcbc5d0355b4a5f26c58d61813698bc8f84b1a979a7e7c3ddc9
BLAKE2b-256 checksum
How to use checksums
72f21c8fd8d657a4a8cd27aaf2d73d2278fb81ff9b9414c651a9b62eb33021c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.6.3 This release

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

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