Skip to main content

CodeStructure is a Python package that analyzes the structure of a Python file and extracts information about its classes, functions, and their associated attributes.

Project description

:building_construction: CodeStructure

Extract code signatures without the code :snake:

PyPI PyPI - Python Version PyPI - License GitHub Workflow Status

This program extracts information about the structure of Python code by analyzing the abstract syntax tree (AST) of a given Python file. It extracts information about classes, their attributes, and functions, as well as their signatures, parameters, return types, decorators, and docstrings. The script then print this information in a human-readable format with syntax highlighting. It can also copy the output to the clipboard. It is particularly useful for understanding and documenting complex codebases and writing design docs or when pasting in some code in ChatGPT.

:star: Features

  • 🔎 Identifies class and function signatures without executing the code
    • 📝 Retrieves class attributes
    • 📚 Gathers function parameters and their corresponding types
    • 🔄 Determines function return types
    • 🎨 Recognizes decorators and docstrings
  • 🌟 Presents the code in an easily understandable format with Syntax Highlighting
  • ✅ Has 100% test coverage

:books: Table of Contents

:package: Installation

You can install CodeStructure via pip:

pip install codestructure

:book: Usage

To use CodeStructure, simply run the script with the path to the Python file you want to analyze:

codestructure path/to/your/python_file.py

The script will output the analyzed code structure and copy it to the clipboard if the pyperclip package is installed.

Run the script with the --help flag to see all available options:

codestructure --help

Which outputs:

usage: codestructure [-h] [--no-private] [--no-copy] [--backticks] [--no-rich]
                     [--line-numbers]
                     module_file_path

Analyze the code structure of a Python file.

positional arguments:
  module_file_path  Path to the Python file.

options:
  -h, --help        show this help message and exit
  --no-private      Do not print private functions.
  --no-copy         Do not copy the output to the clipboard.
  --backticks       Use backticks for code blocks.
  --no-rich         Do not use rich to print the output.
  --line-numbers    Print line numbers for the code blocks.

:memo: Example

Given a Python file (tests/example.py) with the following content:

class MyClass:
    my_attr: str
    _my_private_attr: int

    def my_method(self, arg1: int) -> bool:
        """My docstring."""
        x = 1 + 2
        ...


def my_function(arg2: float) -> None:
    arg2 = arg2 + 1
    return arg2**2


def _private_function():
    """My private function."""
    x = 1 + 2
    y = x + 4
    return y

CodeStructure will output:

codestructure --no-private tests/example.py
class MyClass:
    my_attr: str
    def my_method(self, arg1: int) -> bool:
        """My docstring."""


def my_function(arg2: float) -> None:
    ...

:busts_in_silhouette: Contributing

We welcome contributions to CodeStructure! If you find a bug or have a feature request, please create an issue on the GitHub repository. If you would like to contribute code, please fork the repository and submit a pull request.

:page_with_curl: License

CodeStructure is released under the Apache 2.0 License. For more information, please see the LICENSE file.

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

codestructure-1.1.0.tar.gz (18.2 kB view hashes)

Uploaded Source

Built Distribution

codestructure-1.1.0-py3-none-any.whl (14.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page