Skip to main content

: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.

Metadata

Release files for codestructure 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for codestructure 1.1.0
File Size Uploaded
codestructure-1.1.0.tar.gz 18.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for codestructure 1.1.0
File Interpreter ABI Platform
codestructure-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 32.7 kB

Release files / codestructure-1.1.0.tar.gz

Download URL codestructure-1.1.0.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
58f235ffa69c1a1cb055cb7f9cb427c794e6dd76d003910d9dbfcca6913ce45d
BLAKE2b-256 checksum
How to use checksums
62a762063f7232b76c58659fa65f5f2515a6a0063723917411b7c24996cbe7a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / codestructure-1.1.0-py3-none-any.whl

Download URL codestructure-1.1.0-py3-none-any.whl
Size 14.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d05ae57bbee9f105b64d525bd7f18ce398e938a94ee9821f6ba3582b20d5300
BLAKE2b-256 checksum
How to use checksums
530e6109d4bdabc0227e0914e750ef6c493d9517cd21944a52f6101ba0f7ed24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page