Skip to main content

jyhelper

一些常用的类、方法,不定时更新。

PyPI: https://pypi.org/project/jyhelper/


模块说明

模块 说明 额外依赖
db / mysql MySQL 数据库操作 基础包(pymysql)
mongo MongoDB 操作 jyhelper[mongo]
timeHelper 时间日期相关 无
file / fileHelper 文件读写与操作 无
common 常用小工具方法 无
feishuRobot 飞书机器人发消息 无
sysHelper 系统、命令行、并发任务 无
strHelper 字符串处理 无
listHelper 列表处理 无
dictHelper 字典处理 无
docHelper 文档相关 无
mailHelper 邮件收发 无
apiHelper 基于 FastAPI 快速搭建 API jyhelper[api]

安装

基础安装

默认包含:pymysql、requests、sqlparse

pip install jyhelper
# 或
python3 -m pip install jyhelper

按需安装扩展

# API 服务(fastapi + uvicorn)
pip install jyhelper[api]

# MongoDB
pip install jyhelper[mongo]

# 安装全部扩展
pip install jyhelper[all]

# 组合安装
pip install jyhelper[api,mongo]

更新

pip install jyhelper -U 只升级 jyhelper 本体 及基础依赖,不会自动更新扩展依赖。 更新时需带上当初安装的 extras:

# 只装了基础包
pip install jyhelper -U

# 装了 API 扩展,更新时要带上 [api]
pip install jyhelper[api] -U

# 装了多个扩展
pip install jyhelper[api,mongo] -U

# 装了全部扩展
pip install jyhelper[all] -U

# 指定 PyPI 源
pip install jyhelper[api] --upgrade -i https://pypi.org/simple
python3 -m pip install jyhelper[api] --upgrade -i https://pypi.org/simple

使用说明

基础工具

from jyhelper import db, timeHelper, common, fileHelper, feishuRobot

dbIns = db(host='', user='root', password='', port=3306, db='')
for row in dbIns.table('table').select():
    print(row)

print(common.explodeList([1, 2, 3], 2))
print(timeHelper.getDate())
fileHelper.read(file_path='')

apiHelper — 快速搭建 API

需先安装:pip install jyhelper[api]

启动后访问:

  • Swagger 文档:http://127.0.0.1:8787/docs
  • ReDoc 文档:http://127.0.0.1:8787/redoc

方式 1:routes 列表(推荐)

from jyhelper import apiHelper

def hello():
    return apiHelper.success(data={"message": "Hello, World!"})

apiHelper(
    port=8787,
    title="My API",
    routes=[
        {
            "method": "GET",
            "path": "/hello",
            "handler": hello,
            "summary": "Hello 示例",
            "auth": None,
        },
    ],
)

运行:

python adminAPI.py

方式 2:装饰器 + 热重载 / 多进程

from jyhelper import apiHelper

helper = apiHelper(port=8787, auto_run=False)
app = helper.app

@helper.route("GET", "/hello", summary="Hello 示例")
def hello():
    return apiHelper.success(data={"message": "Hello, World!"})

@helper.route("POST", "/hello", auth="change-me", summary="Hello POST 示例")
def hello_post():
    return apiHelper.success(data={"message": "Hello, POST!"})

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "adminAPI:app",
        host=helper.host,
        port=helper.port,
        log_config=helper.log_config,
        reload=True,       # 开发热重载
        # workers=4,       # 生产多进程(与 reload 不能同时用)
    )

统一响应格式

# 成功
return apiHelper.success(data={"id": 1})
return apiHelper.success(message="操作成功", data={"id": 1})

# 错误(HTTP 仍为 200,错误体现在 body.code)
return apiHelper.error(message="参数错误", code=1001)

鉴权

auth 为非空字符串时,请求需携带 Header:

Authorization: Bearer <token>
curl http://127.0.0.1:8787/hello
curl -X POST http://127.0.0.1:8787/hello -H "Authorization: Bearer change-me"

后台运行(Linux)

nohup python3 adminAPI.py >> adminAPI.log 2>&1 &

导入方式

所有模块均可从包根导入:

from jyhelper import apiHelper, db, mysql, mongo, timeHelper, common

要求

  • Python >= 3.5

Metadata

Release files for jyhelper 26.9.30.0

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

Source distribution (sdist)

Source distribution for jyhelper 26.9.30.0
File Size Uploaded Provenance Actions
jyhelper-26.9.30.0.tar.gz 124.4 kB Without provenance Details for jyhelper-26.9.30.0.tar.gz

Built distribution (wheel)

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

Total release size: 257.8 kB

Release files / jyhelper-26.9.30.0.tar.gz

Download URL jyhelper-26.9.30.0.tar.gz
Size 124.4 kB
Tags Source
SHA-256 checksum
How to use checksums
863859e45cfd344e96d945b4989fcbd42960a9814d7316613f2b4ffbcea9a19b
BLAKE2b-256 checksum
How to use checksums
1499be6eb9a40fc2b25efaa596a96e2a130486ed69af235ced4ef625f0c9c7da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Release files / jyhelper-26.9.30.0-py3-none-any.whl

Download URL jyhelper-26.9.30.0-py3-none-any.whl
Size 133.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2702eefe578541ea962cc21769b54d2b5b8df4f532bc0dfec610b70a7c52365b
BLAKE2b-256 checksum
How to use checksums
172767cb4d97f75912d860383e049a068095fe8e6fce6ec05e30e91ab009c8cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Security for jyhelper 26.9.30.0

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

26.9.30.0 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