Evaluator that detects and penalizes non-built-in Python imports in source files
Project description
Swarmauri Evaluator External Imports
Evaluator that detects and penalizes non–standard-library imports in Python source files.
Purpose
ExternalImportsEvaluator gauges dependency hygiene by walking the Python
sources referenced by a swarmauri_core.programs.IProgram
and flagging modules that are not part of the Python standard library.
What it does
- Recursively walks every
.pyfile beneathprogram.path. - Parses each file's abstract syntax tree to collect
importandfrom ... import ...statements. - Compares imported modules against the Python standard library (including built-ins and a curated set of commonly used packages).
- Returns both a score and metadata describing any external dependencies that were found.
How scoring works
- The evaluator starts at a perfect score of 1.0.
- Each unique external module subtracts 0.1 from the score.
- The score never drops below 0.0 regardless of how many modules are flagged.
Metadata provided
evaluate returns the score alongside a dictionary with:
total_imports: Count of all import statements encountered.external_imports_count: Number of non-standard imports detected.unique_external_modules: How many distinct external modules were used.external_modules: A list of the external module names.external_imports_details: Rich per-import entries containing the module, alias, line number, import type, and whether the module was considered standard.files_analyzedandpython_files: Insights into the scan coverage.execution_time: Added byEvaluatorBaseto show runtime of the analysis.
Installation
Pick the workflow that matches your toolchain:
# pip
pip install swarmauri_evaluator_externalimports
# Poetry
poetry add swarmauri_evaluator_externalimports
# uv (install the tool if you have not already)
curl -Ls https://astral.sh/uv/install.sh | sh
uv add swarmauri_evaluator_externalimports
Example
The snippet below creates a temporary project, evaluates it, and prints the
results. You can drop it into a file such as example.py and run
python example.py.
from pathlib import Path
import tempfile
from typing import Any
from swarmauri_core.programs.IProgram import IProgram
from swarmauri_evaluator_externalimports import ExternalImportsEvaluator
class DirectoryProgram(IProgram):
"""Minimal IProgram implementation backed by a filesystem path."""
def __init__(self, path: str):
self.path = path
def diff(self, other: "IProgram"):
raise NotImplementedError("Diffing is outside the scope of this example")
def apply_diff(self, diff: Any) -> "IProgram":
raise NotImplementedError("Diffing is outside the scope of this example")
def validate(self) -> bool:
return True
def clone(self) -> "IProgram":
return DirectoryProgram(self.path)
def evaluate_example() -> tuple[float, dict[str, Any]]:
evaluator = ExternalImportsEvaluator()
with tempfile.TemporaryDirectory() as workdir:
project = Path(workdir)
project.joinpath("app.py").write_text(
"""import os\nimport numpy\n\nprint(os.name, numpy.__version__)\n""",
encoding="utf-8",
)
program = DirectoryProgram(workdir)
score, details = evaluator.evaluate(program)
print("Score:", score)
print("External modules:", sorted(details["external_modules"]))
return score, details
if __name__ == "__main__":
evaluate_example()
Want to help?
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swarmauri_evaluator_externalimports-0.3.0.dev45.tar.gz.
File metadata
- Download URL: swarmauri_evaluator_externalimports-0.3.0.dev45.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35fd61f31c8ff41036b690216c76d88cd651df18e62bbd1abe1732fcab742484
|
|
| MD5 |
2c0d4f24a02b78d1999631843062afee
|
|
| BLAKE2b-256 |
fa9e9b457b05181e6a8ac0feb8cc548ec8d76ed2e94dcb15faf530c28094ee77
|
File details
Details for the file swarmauri_evaluator_externalimports-0.3.0.dev45-py3-none-any.whl.
File metadata
- Download URL: swarmauri_evaluator_externalimports-0.3.0.dev45-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34ad994a5d4a1835e3518b742fdf9fdf0867c01e3093d9ce76d7e373bb93b1a1
|
|
| MD5 |
05741a79840006e57cbbbd28d41e401c
|
|
| BLAKE2b-256 |
1b0e0a289f6a94db6bcadaa3ac02c90233ac670f59da98cae2741fbd3b4c0a1a
|