LoopSniff
LoopSniff is a static analysis tool that scans your Python code for inefficient iterative processing patterns—especially those involving SQLAlchemy query results. If you’ve ever manually looped over query results to add rows one-by-one or used list comprehensions on query results, LoopSniff will help you identify and optimize these patterns for better performance.
Features
-
Detects Inefficient Iteration:
Finds for-loops iterating over SQLAlchemy query results where items are added individually using.add(),.append(), or via augmented assignment (+=). -
List Comprehension Analysis:
Flags list comprehensions that iterate over query results (e.g.,[a for a in session.query(MyModel).all()]) to help you rethink bulk operations. -
Extended Pattern Matching:
Recognizes both direct query calls (e.g.,session.query(MyModel)) and calls via partial functions (e.g.,query = session.querythenitems = query(MyModel).all()). -
Additional Inefficiency Warnings:
Detects other performance bottlenecks in loops, such as calling.commit(),.filter(),.update(), etc., inside loops. -
User-Friendly Output:
Uses the Rich library for colorful, engaging console output with explanations and code snippets for each issue found. -
Easy-to-Use CLI:
Built with Click for a dynamic command-line interface that lets you scan directories with ease. -
Tested & Reliable:
Comes with comprehensive tests using pytest, ensuring robust detection across a variety of edge cases.
Installation
Clone the repository and install the dependencies using pip:
git clone https://github.com/yourusername/loopsniff.git
cd loopsniff
pip install -r requirements.txt
Note: Make sure your requirements.txt includes dependencies such as click, rich, and pytest.
Usage
To scan your code for inefficient SQLAlchemy patterns, run:
loopsniff /path/to/your/project
This command recursively scans all .py files in the specified directory and prints out a concise, color-coded summary of any detected patterns, along with code snippets and recommendations for improvement.
Example Output
myfile.py | For-loop at line 10 (.add()/.append() at line 11): Iteratively processing 'item' from query 'items' is slow. Consider bulk operations for better performance. | Code: for item in items:
myfile.py | At line 15 (listcomp): List comprehension over query results is suboptimal. Rethink your approach. | Code: [a for a in session.query(MyModel).all()]
Testing
LoopSniff is tested with pytest. To run the tests, simply execute:
pytest
This runs a suite of tests covering various edge cases—from standard for-loops with .add()/.append() and += to inefficient list comprehensions.
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests for enhancements, bug fixes, or new features. Please follow standard GitHub contribution guidelines.
License
This project is licensed under the MIT License.
Acknowledgments
- Thanks to the Rich and Click communities for their fantastic libraries.
- Inspired by real-world performance challenges in SQLAlchemy-based applications.
Happy scanning and optimizing!
Metadata
Release files for loopsniff 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| loopsniff-0.1.2.tar.gz | 7.3 kB | Without provenance | Details for loopsniff-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| loopsniff-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for loopsniff-0.1.2-py3-none-any.whl |
Total release size: 15.6 kB
Release files / loopsniff-0.1.2.tar.gz
| Download URL | loopsniff-0.1.2.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a78f31511a133303a19bbdb49e9dc13aee0f21a1dd5c5847696b6ab656e4b4c6
|
|
BLAKE2b-256 checksum How to use checksums |
f17d6c09a7612cfce020a96883cf1c4ddb6f132363da39397b3552300248627e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.1 CPython/3.13.0 Windows/10
|
Release files / loopsniff-0.1.2-py3-none-any.whl
| Download URL | loopsniff-0.1.2-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
44d8a60d794d88c3dc44d8402b16f8d0667fb805e8fcdab928fa45c07e4c54c4
|
|
BLAKE2b-256 checksum How to use checksums |
f69cfe78a89f53271d6bf2a0200a114a116c5f421abbe0f09ca71810eb03eab0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.1 CPython/3.13.0 Windows/10
|
Security for loopsniff 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.