Skip to main content

Static analysis tool for Python code optimization suggestions

Project description

smart_code

smart_code is a static analysis tool that detects inefficient Python code patterns (AST-based) and provides optimization suggestions. It covers Pandas/NumPy anti-patterns, algorithmic inefficiencies, and common Python pitfalls.

Features

  • AST-Based Analysis: Accurately detects code patterns without executing code.
  • Rich Pattern Library: Covers a wide range of anti-patterns from Pandas usage to algorithmic mistakes.
  • Click-to-Navigate Output: Terminal output is formatted as file:line: message, allowing you to jump directly to the code in modern IDEs (like VS Code or PyCharm).
  • Multi-language Support: Suggestions can be displayed in English or Chinese.

Usage

Command-Line Interface

Analyze one or more Python files:

smart_code path/to/your_script.py [another_file.py ...]

To receive suggestions in English, use the --lang en flag:

smart_code path/to/your_script.py --lang en

The output will look like this, and you can typically Ctrl+Click or Cmd+Click the link to navigate:

path/to/your_script.py:15: [Line 15] Detected use of DataFrame.iterrows() to iterate over rows
☛ Suggestion:Use itertuples or vectorized operations instead
Complexity:O(n) vs O(n)
Hint:iterrows converts each row into a Series, which has poor performance

Python API

You can also use smart_code programmatically in your own scripts.

from smart_code.analyzer import CodeAnalyzer
from smart_code.suggest import format_issue

# Initialize the analyzer in a specific language ('zh' or 'en')
analyzer = CodeAnalyzer(lang='en')

issues = analyzer.analyze_file("script.py")

for issue in issues:
    # Format the output for printing
    formatted_message = format_issue(issue, lang='en')
    print(f"script.py:{issue['lineno']}: {formatted_message}")

# The raw issue dictionary is also available
# print(issues)

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

smart_code-0.4.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

smart_code-0.4.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file smart_code-0.4.0.tar.gz.

File metadata

  • Download URL: smart_code-0.4.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.10 Windows/11

File hashes

Hashes for smart_code-0.4.0.tar.gz
Algorithm Hash digest
SHA256 76406882bd9dd80f5748fcb11a8797e0c912692f649605696c5785f69727592a
MD5 c0dde7d710ea81c7b2a1640d37767492
BLAKE2b-256 aa3d2e3d892e6a17aa58505a5f2d40dd6edf00c2a07bf82f3d9e30949e5bf5bc

See more details on using hashes here.

File details

Details for the file smart_code-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: smart_code-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.10 Windows/11

File hashes

Hashes for smart_code-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac98f0ecd1d10d9192ab8fdef49d18d411893db77787ed8d0d53a114b6d67539
MD5 20f9fe61bf3d57146e5c133f8c7402ef
BLAKE2b-256 f1b8bdc0ab83adcacde898fa459f2b86e17924c167816845da480b9318cbc4a3

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