Skip to main content

A tool to extract code elements from Python files

Project description

PyCodeLens

A Python code analysis tool to extract and manipulate code elements like functions, decorators, classes, and print statements. It supports Python, JavaScript, and TypeScript files.

Installation

pip install pycodelens

Features

PyCodeLens allows you to extract and analyze:

  1. Functions

    • Count of functions in a file
    • Function names
    • Line numbers (start and end)
    • Associated decorators
  2. Decorators

    • Count of decorators in a file
    • Decorator names
    • Line numbers where they appear
  3. Classes

    • Count of classes in a file
    • Class names
    • Line numbers (start and end)
    • Methods within each class
  4. Print Statements

    • Count of print statements
    • Line numbers
    • Number of arguments
  5. Code Replacement

    • Replace a function with new code
    • Replace a class with new code
    • Replace specific lines with new code
    • Automatic indentation handling

Usage

Command Line

# Basic usage (shows count of all elements)
pycodelens path/to/your_file.py

# Show detailed information about functions
pycodelens path/to/your_file.py --functions

# Show only decorators
pycodelens path/to/your_file.py --decorators

# Show information about all elements
pycodelens path/to/your_file.py --all

# Show only counts
pycodelens path/to/your_file.py --counts

# Output in JSON format
pycodelens path/to/your_file.py --json

# Show verbose information
pycodelens path/to/your_file.py --all --verbose

# Print source code for a specific function
pycodelens path/to/your_file.py --function-name my_function

# Print source code for a specific class
pycodelens path/to/your_file.py --class-name MyClass

# Print specific lines from the file
pycodelens path/to/your_file.py --lines 10-20

Code Replacement

# Replace a function with code from a file
pycodelens path/to/your_file.py --replace-function my_function --replacement-file new_function.py

# Replace a class with inline content
pycodelens path/to/your_file.py --replace-class MyClass --replacement-content "class MyClass:\n    def new_method(self):\n        pass"

# Replace specific lines
pycodelens path/to/your_file.py --replace-lines 10-20 --replacement-file new_code.py

Python API

from pycodelens import analyze_file, replace_element

# Analyze a file
analysis = analyze_file('path/to/your_file.py')

# Access the summary
summary = analysis['summary']
print(f"Number of functions: {summary['num_functions']}")
print(f"Function names: {summary['function_names']}")

# Access raw results
raw_results = analysis['raw_results']
for func in raw_results['functions']:
    print(f"Function {func['name']} on lines {func['line_start']}-{func['line_end']}")
    if func['decorators']:
        print(f"  Has decorators: {', '.join('@' + d['name'] for d in func['decorators'])}")

# Replace a function
success, message = replace_element(
    'path/to/your_file.py',
    'function',
    'my_function',
    replacement_file='new_function.py'
)
print(message)

Example Output

File: example.py
  Functions: 5
  Decorators: 3
  Classes: 2
  Print statements: 7

FUNCTIONS:
  main (lines 10-20)
    Decorators: @app.route, @login_required
  process_data (lines 22-30)
  helper_function (lines 32-35)
    Decorators: @staticmethod
  ...

DECORATORS:
  @app.route: 2 occurrences (lines: 10, 45)
  @login_required: 1 occurrences (lines: 11)
  @staticmethod: 2 occurrences (lines: 32, 50)
  ...

Supported Languages

  • Python - Full support with detailed analysis
  • JavaScript - Basic support for functions and classes
  • TypeScript - Basic support with interface detection

Requirements

  • Python 3.7+
  • astroid library

License

MIT

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

pycodelens-0.2.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

pycodelens-0.2.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file pycodelens-0.2.0.tar.gz.

File metadata

  • Download URL: pycodelens-0.2.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for pycodelens-0.2.0.tar.gz
Algorithm Hash digest
SHA256 040b27d5febc207c984f78152618135c079eaba9022ef3c9ef11a1ec7732f469
MD5 6d4e670a6736b9439be1c211843cb778
BLAKE2b-256 1db2227a57208d9158330b7e73659c3dcbd8304e4dffc4f7a64aa2eab75b05cf

See more details on using hashes here.

File details

Details for the file pycodelens-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pycodelens-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for pycodelens-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d42c310f81449327f026c52f7f80711f9b562393dc66e489d0652ad691923360
MD5 04bb7bdd6c4bd84642fdbbe0fb815ad1
BLAKE2b-256 a4026c11aa1ead0ce6b98781112dbfed3dbc95ad4b2774cc774095184d5a0d5e

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