在日常开发中,pandas、requests、selenium 等常见库几乎是标配,但随着时间推移,它们带来的“惊喜”逐渐消退。几个月前,我发现自己的脚本运行缓慢、重复冗余,维护起来枯燥乏味。于是,我开始尝试在主流榜单之外寻找更轻量、更高效的工具。结果意外收获了 10 个鲜为人知的 Python 库,它们极大改善了我的自动化流程。更重要的是,大部分上手不到 5 分钟即可使用。

1. Gino —— 高性能异步 ORM

传统 ORM 在异步环境下常常处理繁琐,Gino 基于 SQLAlchemy Core 构建,专为 FastAPI、Sanic 等异步框架设计。

from gino import Gino

db = Gino()

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String())

async def get_users():
    await db.set_bind('postgresql://user:pass@localhost/db')
    users = await User.query.gino.all()
    print(users)

优势:简化数据库异步调用,消除传统 ORM 的阻塞问题。

2. Arrow —— 更优雅的时间处理

相比 datetime,Arrow 语法更直观,尤其在多时区、格式转换等场景下更友好。

import arrow

utc = arrow.utcnow()
local_time = utc.to('Asia/Shanghai')
print(local_time.format('YYYY-MM-DD HH:mm'))

优势:适合跨时区任务调度、日志时间戳处理。

3. Tenacity —— 专业级重试机制

网络不稳定、API 超时常见,但手写重试逻辑冗余。Tenacity 用装饰器即可解决。

from tenacity import retry, stop_after_attempt
import random

@retry(stop=stop_after_attempt(3))
def unstable_task():
    if random.randint(0, 1) == 0:
        raise Exception("Network error")
    print("Success!")

unstable_task()

优势:轻松提升脚本稳定性。

4. RapidFuzz —— 高速模糊匹配

相比 Levenshtein,RapidFuzz 在大规模文本匹配场景下更快。

from rapidfuzz import fuzz

score = fuzz.ratio("automation", "automatoin")
print(score)

优势:适合数据清洗、模糊查重。

5. Prefect —— 自动化任务编排

脚本一旦涉及多步骤流程,维护难度急剧上升。Prefect 提供可视化、可追踪的工作流编排。

from prefect import flow, task

@task
def extract():
    return "data"

@task
def transform(data):
    return data.upper()

@flow
def etl_flow():
    data = extract()
    clean = transform(data)
    print(clean)

etl_flow()

优势:让零散脚本转化为可管理的流水线。

6. Meilisearch-Py —— 轻量级搜索引擎

相比 Elasticsearch,Meilisearch 更轻巧,部署简单。

from meilisearch import Client

client = Client("http://127.0.0.1:7700")
client.index("books").add_documents([
    {"id": 1, "title": "Deep Learning with Python"}
])

优势:快速构建搜索功能,无需庞大依赖。

7. PyInputPlus —— 智能输入验证

在需要人工输入时,input() 功能有限。PyInputPlus 内置验证、重试、超时机制。

import pyinputplus as pyip

age = pyip.inputInt("请输入年龄: ", min=0, max=120)
print(age)

优势:减少重复的校验代码。

8. CacheTools —— 高效缓存

频繁调用 API 时,缓存可节省请求次数和费用。CacheTools 提供灵活的缓存策略。

from cachetools import cached, TTLCache

cache = TTLCache(maxsize=100, ttl=300)

@cached(cache)
def get_data(x):
    print("Fetching...")
    return x * 2

print(get_data(5))
print(get_data(5))

优势:避免重复请求,优化性能。

9. Zstandard —— 快速压缩

在日志归档、备份等任务中,Zstandard 在压缩比和速度上均优于 gzip。

import zstandard as zstd

cctx = zstd.ZstdCompressor()
compressed = cctx.compress(b"some large data")

优势:降低存储成本,加快处理速度。

10. PikePDF —— 现代化 PDF 处理

相比 PyPDF2,PikePDF 更高效、稳定,底层基于 QPDF。

import pikepdf

pdf = pikepdf.open("input.pdf")
pdf.pages.rotate(0, 90)
pdf.save("output.pdf")

优势:批量处理 PDF 文件更轻松。

这些库的价值不仅体现在功能上,更在于它们能自然融入自动化流程,减少冗余代码,提升可维护性。

建议开发者平时多做探索,不必等到“刚好需要”时才去寻找工具。提前储备,问题出现时自然能快速找到最优解。

最后修改:2025 年 08 月 19 日
本站福利|微信扫描二维码,永久享受 96 折充话费电费