Skip to main content

Static analysis tool for Python code optimization suggestions

Project description

smart_code

Current version: 0.4.2

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.2.tar.gz (14.5 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.2-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: smart_code-0.4.2.tar.gz
  • Upload date:
  • Size: 14.5 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.2.tar.gz
Algorithm Hash digest
SHA256 b54bd8667847ee05b8adac5e9883023f7a9cea79cc004e7efa3fdd1a0276158e
MD5 47bb04b6795e24ff22aad5b989112d7a
BLAKE2b-256 2383ff2803dbb75a73b0615e54277536d2bcdb199658ab8c2526962e1a1d9dc1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: smart_code-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 16.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 065341de2b429f9f65dec8000d02e11ba13a7c2fee32fecc6d1b92301c1c1227
MD5 09febc17e689cde15fbcc337f0a781cb
BLAKE2b-256 4a671311ea4e9b8090c752317c3d370da17a88483a201ce6c28020a55e9332b1

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