⚡
High Performance
Blazing fast parsing with optimized algorithms. Parse 1000+ dependencies in milliseconds.
Parse, edit, and manage Python dependencies with ease
package main
import (
"fmt"
"log"
"github.com/scagogogo/python-requirements-parser/pkg/parser"
"github.com/scagogogo/python-requirements-parser/pkg/editor"
)
func main() {
// Parse requirements.txt
p := parser.New()
reqs, err := p.ParseFile("requirements.txt")
if err != nil {
log.Fatal(err)
}
// Edit with position-aware editor (minimal diff)
editor := editor.NewPositionAwareEditor()
doc, err := editor.ParseRequirementsFile(content)
if err != nil {
log.Fatal(err)
}
// Update package versions
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)
}
// Serialize with minimal changes
result := editor.SerializeToString(doc)
fmt.Println(result)
}
Operation | Time | Memory | Allocations |
---|---|---|---|
Parse 100 packages | 357 µs | 480 KB | 4301 allocs |
Single update | 67.67 ns | 8 B | 1 alloc |
Batch update (10 packages) | 374.1 ns | 0 B | 0 allocs |
Serialize 100 packages | 4.3 µs | 8.2 KB | 102 allocs |
The PositionAwareEditor achieves 50% fewer changes compared to traditional editors:
Perfect preservation of:
git+https://...
)https://...
)-r requirements-dev.txt
); python_version >= "3.7"
)--index-url https://...
)Full support for all pip-compatible formats:
# Basic dependencies
flask==2.0.1
django>=3.2.0,<4.0.0
requests~=2.25.0
# Dependencies with extras
django[rest,auth]>=3.2.0
uvicorn[standard]>=0.15.0
# Environment markers
pywin32>=1.0; platform_system == "Windows"
dataclasses>=0.6; python_version < "3.7"
# VCS dependencies
git+https://github.com/user/project.git#egg=project
-e git+https://github.com/dev/project.git@develop#egg=project
# URL dependencies
https://example.com/package.whl
http://mirrors.aliyun.com/pypi/web/package-1.0.0.tar.gz
# File references
-r requirements-dev.txt
-c constraints.txt
# Global options
--index-url https://pypi.example.com
--extra-index-url https://private.pypi.com
--trusted-host pypi.example.com
# Hash verification
flask==2.0.1 --hash=sha256:abcdef1234567890
Released under the MIT License.