跳转到主要内容

CodeStructure是一个Python包,它分析Python文件的结构并提取有关其类、函数及其相关属性的信息。

项目描述

:building_construction: CodeStructure

不执行代码提取代码签名 :snake

PyPI PyPI - Python Version PyPI - License GitHub Workflow Status

该程序通过分析给定Python文件的抽象语法树(AST)来提取Python代码的结构信息。它提取有关类、它们的属性和函数的信息,包括它们的签名、参数、返回类型、装饰器和文档字符串。然后,该脚本以语法高亮格式以人类可读的方式打印这些信息。它还可以将输出复制到剪贴板。这对于理解和记录复杂的代码库、编写设计文档或在ChatGPT中粘贴一些代码特别有用。

:star: 功能

  • 🔎 不执行代码即可识别类和函数签名
    • 📝 获取类属性
    • 📚 收集函数参数及其对应的类型
    • 🔄 确定函数返回类型
    • 🎨 识别装饰器和文档字符串
  • 🌟 以易于理解的方式呈现代码,具有语法高亮
  • ✅ 具有百分之百的测试覆盖率

:books: 目录

:package: 安装

您可以通过pip安装CodeStructure

pip install codestructure

:book: 使用

要使用CodeStructure,只需运行脚本并指定您要分析的Python文件路径

codestructure path/to/your/python_file.py

脚本将输出分析的代码结构,如果已安装pyperclip包,则将其复制到剪贴板。

使用--help标志运行脚本以查看所有可用选项

codestructure --help

输出内容

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: 示例

给定以下内容的Python文件(tests/example.py

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将输出

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: 贡献

欢迎为CodeStructure做出贡献!如果您发现了一个错误或者有功能请求,请在本GitHub仓库上创建一个问题。如果您想贡献代码,请fork仓库并提交一个pull request。

:page_with_curl: 许可证

CodeStructure遵循Apache 2.0许可证发布。更多信息,请参阅LICENSE文件。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

codestructure-1.1.0.tar.gz (18.2 kB 查看哈希值)

上传时间 源代码

构建分发

codestructure-1.1.0-py3-none-any.whl (14.6 kB 查看哈希值)

上传时间 Python 3

由以下组织支持