Skip to main content

各种实用、常用的小函数、类

Project description

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]

安装

基础安装

默认包含:pymysqlrequestssqlparse

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

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

jyhelper-26.7.14.1.tar.gz (65.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jyhelper-26.7.14.1-py3-none-any.whl (73.2 kB view details)

Uploaded Python 3

File details

Details for the file jyhelper-26.7.14.1.tar.gz.

File metadata

  • Download URL: jyhelper-26.7.14.1.tar.gz
  • Upload date:
  • Size: 65.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.11

File hashes

Hashes for jyhelper-26.7.14.1.tar.gz
Algorithm Hash digest
SHA256 dd6fc9d4497349264071cdfa8aac323db3c36c5456b9e78b4b59a94c56c2f005
MD5 d32cb6ab715c0b743c482d3bd18c67c4
BLAKE2b-256 159e22c2a8a564eb11d042a13adc46609ca783377dd1085a77d8b242f801a3e9

See more details on using hashes here.

File details

Details for the file jyhelper-26.7.14.1-py3-none-any.whl.

File metadata

  • Download URL: jyhelper-26.7.14.1-py3-none-any.whl
  • Upload date:
  • Size: 73.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.11

File hashes

Hashes for jyhelper-26.7.14.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ea39ed4a05dc6c33ea6d8c33481a6b1b67131aa8538f95668f86a3ddd5f1f04
MD5 20b80cde3e748d45949749a79f89f1e8
BLAKE2b-256 38476ffe0c61f9b49cd8c563d01e71aeefb8b2a2ffc3b25d67471204d330b602

See more details on using hashes here.

Supported by

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