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.1.tar.gz (14.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.1-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: smart_code-0.4.1.tar.gz
  • Upload date:
  • Size: 14.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.1.tar.gz
Algorithm Hash digest
SHA256 654cf7acafc1d95840747ab4df416616abaf1e002ea5e2050457f489fbf589c3
MD5 29e61ecb332f6eeca508c65a1fd2b495
BLAKE2b-256 d98be63b52bff985f0e2a18394f11ca1f76ed68b92a33f63d81853703c0890f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: smart_code-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 16.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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f2ebf103cbde94fc046fce41bb37171a1f0aa9f2d6a596ac2e72b624d067d830
MD5 e66b57da6f821a098ef949709f68ebbf
BLAKE2b-256 e932aedd7c4308628e7520ee0dd470111d17e988d6593146343a05d1fccbc14b

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