⚡
高性能
采用优化算法的超快解析速度,毫秒级解析 1000+ 个依赖项。
package main
import (
"fmt"
"log"
"github.com/scagogogo/python-requirements-parser/pkg/parser"
"github.com/scagogogo/python-requirements-parser/pkg/editor"
)
func main() {
// 解析 requirements.txt
p := parser.New()
reqs, err := p.ParseFile("requirements.txt")
if err != nil {
log.Fatal(err)
}
// 使用位置感知编辑器(最小化 diff)
editor := editor.NewPositionAwareEditor()
doc, err := editor.ParseRequirementsFile(content)
if err != nil {
log.Fatal(err)
}
// 更新包版本
updates := map[string]string{
"flask": "==2.0.1",
"django": ">=3.2.13",
"requests": ">=2.28.0",
}
err = editor.BatchUpdateVersions(doc, updates)
if err != nil {
log.Fatal(err)
}
// 序列化为最小变更
result := editor.SerializeToString(doc)
fmt.Println(result)
}
操作 | 时间 | 内存 | 分配次数 |
---|---|---|---|
解析 100 个包 | 357 µs | 480 KB | 4301 allocs |
单个更新 | 67.67 ns | 8 B | 1 alloc |
批量更新(10 个包) | 374.1 ns | 0 B | 0 allocs |
序列化 100 个包 | 4.3 µs | 8.2 KB | 102 allocs |
PositionAwareEditor 相比传统编辑器实现了 50% 更少的变更:
完美保持:
git+https://...
)https://...
)-r requirements-dev.txt
); python_version >= "3.7"
)--index-url https://...
)全面支持所有 pip 兼容格式:
# 基础依赖
flask==2.0.1
django>=3.2.0,<4.0.0
requests~=2.25.0
# 带 extras 的依赖
django[rest,auth]>=3.2.0
uvicorn[standard]>=0.15.0
# 环境标记
pywin32>=1.0; platform_system == "Windows"
dataclasses>=0.6; python_version < "3.7"
# VCS 依赖
git+https://github.com/user/project.git#egg=project
-e git+https://github.com/dev/project.git@develop#egg=project
# URL 依赖
https://example.com/package.whl
http://mirrors.aliyun.com/pypi/web/package-1.0.0.tar.gz
# 文件引用
-r requirements-dev.txt
-c constraints.txt
# 全局选项
--index-url https://pypi.example.com
--extra-index-url https://private.pypi.com
--trusted-host pypi.example.com
# 哈希验证
flask==2.0.1 --hash=sha256:abcdef1234567890
基于 MIT 许可证 发布。