Skip to main content

A static analysis tool for detecting inefficient SQLAlchemy loops

Project description

LoopSniff

image PyPI - Python Version

Downloads

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.query then items = 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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

loopsniff-0.1.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loopsniff-0.1.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file loopsniff-0.1.2.tar.gz.

File metadata

  • Download URL: loopsniff-0.1.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.0 Windows/10

File hashes

Hashes for loopsniff-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a78f31511a133303a19bbdb49e9dc13aee0f21a1dd5c5847696b6ab656e4b4c6
MD5 10bc390614d0023c081f4d09796a16c0
BLAKE2b-256 f17d6c09a7612cfce020a96883cf1c4ddb6f132363da39397b3552300248627e

See more details on using hashes here.

File details

Details for the file loopsniff-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: loopsniff-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.0 Windows/10

File hashes

Hashes for loopsniff-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 44d8a60d794d88c3dc44d8402b16f8d0667fb805e8fcdab928fa45c07e4c54c4
MD5 f736671bc3b0dc9060d7d58f502aa2b8
BLAKE2b-256 f69cfe78a89f53271d6bf2a0200a114a116c5f421abbe0f09ca71810eb03eab0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page