-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Dianel edited this page Apr 9, 2026
·
1 revision
import graphoptim as go
# Analyze a source string
code = """
def process_data(items):
results = []
for item in items:
if item is None:
continue
if item > 100:
results.append(item * 2)
elif item > 50:
results.append(item * 1.5)
else:
results.append(item)
return results
print("dead code") # Unreachable
"""
report = go.analyze(code)
print(f"Score: {report.total_score}/100")
for func in report.functions:
print(f"\n{func.function_name}(): {func.score}/100 {func.status}")
print(f" Cyclomatic complexity: {func.metrics.cyclomatic_complexity}")
print(f" Dead nodes: {func.metrics.dead_nodes_count}")
print(f" Suggested passes: {func.suggested_passes}")report = go.analyze_file("mymodule.py")
print(report.summary())reports = go.analyze_project("./src")
for report in reports:
if report.total_score < 70:
print(f"⚠ {report.filepath}: {report.total_score}/100")optimized = go.optimize(code)
print(optimized) # Dead code removedoptimized = go.optimize(code, passes=["dead_code"])
optimized = go.optimize(code, passes=["dead_code", "path_shortener"])optimized = go.optimize(
code,
budget={"max_changes": 5, "min_improvement": 0.1}
)# Creates automatic .bak backup
go.optimize_file("mymodule.py", inplace=True)go.optimize_file("mymodule.py", output="mymodule_optimized.py")# Analyze
graphoptim analyze myfile.py
graphoptim analyze myfile.py --verbose
graphoptim analyze ./src --format json --output report.json
# Optimize
graphoptim optimize myfile.py # Dry run (preview)
graphoptim optimize myfile.py --inplace # Modify in-place
graphoptim optimize myfile.py -o output.py # Write to new file
graphoptim optimize myfile.py -p dead_code # Specific pass
# Benchmark (requires API keys)
graphoptim benchmark --samples 20 --models claude
# Configuration
graphoptim config show| Score | Status | Meaning |
|---|---|---|
| 80 - 100 | ✓ good | Clean, well-structured code |
| 60 - 79 | ~ acceptable | Minor improvements possible |
| 0 - 59 | ⚠ needs attention | Significant structural issues |
Scoring factors:
- Cyclomatic complexity > 7: Up to -30 points
- Dead nodes: -15 per unreachable block
- Bottleneck nodes: -10 per high-centrality node
- Deep chains: -8 per overly deep path
- Redundant paths: -12 per duplicate branch