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.1.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.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pycodelens-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 22250b6f15de7b4c2d1e40d7b497faa2da5218f1728959e7bc21516cb571567a
MD5 773276f3db71affd93a9e0f269551dff
BLAKE2b-256 0aca6d02699df36c560632464aa21845d99ff747d21ac8b7d81b7b17cc18487e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pycodelens-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 88fcaa6df51685f9aef4c8a6c63b5dbcab7e8ef92a192baa79de94175b82dd50
MD5 a0f2874f59923ee3e2d0d758b844b6e2
BLAKE2b-256 50d17a060fa604443c7981324e2b189f90eeafbedb25aecd88ad8600dc6231d8

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