python输出class的引用关系图(UML)


1. 安装 pylint

pip install pylint

如果安装过 graphviz,那么下面使用以下命令可以输出 class 的引用关系:

pyreverse -ASmy -o svg model.py 

Graphviz 支持输出的格式:canon cmap cmapx cmapx_np dot dot_json eps fig gv imap imap_np ismap json json0 pic plain plain-ext pov ps ps2 svg svg_inline svgz tk xdot xdot1.2 xdot1.4 xdot_json

如果报错:

'Graphviz' needs to be installed for your chosen output format.

那么需要安装 Graphviz

2. 安装 Graphviz

网上各种安装方法,目前我成功的就是源码编译安装。

源码地址:https://graphviz.org/download/source/

2.1 下载源码

下载最新版本即可:

下载后解压。

2.2 编译安装

进入到解压文件中,依次执行:

./configure

无报错再执行:

make

最后执行:

make install

验证是否安装成功:

dot -version

输出如下信息则成功安装:
在这里插入图片描述

3. 输出class的引用关系

pyreverse 是 Python 的一个实用工具,它是 pylint 包的一部分,用于从 Python 源代码生成 UML(统一建模语言)图,包括类图和包依赖图。这可以帮助开发者更好地理解大型项目的结构,进行代码审查或文档编写。

完整用法说明如下:

usage: pyreverse [options]

Create UML diagrams for classes and modules in <packages>.

options:
  -h, --help            show this help message and exit

Pyreverse:
  Base class providing common behaviour for pyreverse commands.

  --filter-mode <mode>, -f <mode>
                        filter attributes and functions according to <mode>. Correct modes are : 'PUB_ONLY' filter all non public attributes [DEFAULT], equivalent to PRIVATE+SPECIAL_A 'ALL' no filter 'SPECIAL'
                        filter Python special functions except constructor 'OTHER' filter protected and private attributes (default: PUB_ONLY)
  --class <class>, -c <class>
                        create a class diagram with all classes related to <class>; this uses by default the options -ASmy (default: [])
  --show-ancestors <ancestor>, -a <ancestor>
                        show <ancestor> generations of ancestor classes not in <projects> (default: None)
  --all-ancestors, -A   show all ancestors off all classes in <projects> (default: None)
  --show-associated <association_level>, -s <association_level>
                        show <association_level> levels of associated classes not in <projects> (default: None)
  --all-associated, -S  show recursively all associated off all associated classes (default: None)
  --show-builtin, -b    include builtin objects in representation of classes (default: False)
  --show-stdlib, -L     include standard library objects in representation of classes (default: False)
  --module-names <y or n>, -m <y or n>
                        include module name in representation of classes (default: None)
  --only-classnames, -k
                        don't show attributes and methods in the class boxes; this disables -f values (default: False)
  --no-standalone       only show nodes with connections (default: False)
  --output <format>, -o <format>
                        create a *.<format> output file if format is available. Available formats are: dot, puml, plantuml, mmd, html. Any other format will be tried to create by means of the 'dot' command
                        line tool, which requires a graphviz installation. (default: dot)
  --colorized           Use colored output. Classes/modules of the same package get the same color. (default: False)
  --max-color-depth <depth>
                        Use separate colors up to package depth of <depth> (default: 2)
  --color-palette <color1,color2,...>
                        Comma separated list of colors to use (default: ('#77AADD', '#99DDFF', '#44BB99', '#BBCC33', '#AAAA00', '#EEDD88', '#EE8866', '#FFAABB', '#DDDDDD'))
  --ignore <file[,file...]>
                        Files or directories to be skipped. They should be base names, not paths. (default: ('CVS',))
  --project <project name>, -p <project name>
                        set the project name. (default: )
  --output-directory <output_directory>, -d <output_directory>
                        set the output directory path. (default: )
  --source-roots <path>[,<path>...]
                        Add paths to the list of the source roots. Supports globbing patterns. The source root is an absolute path or a path relative to the current working directory used to determine a
                        package namespace for modules located under the source root. (default: ())
  --verbose             Makes pyreverse more verbose/talkative. Mostly useful for debugging. (default: False)

3.1 关键选项解释

  • 过滤模式 (--filter-mode <mode>, -f <mode>): 控制如何过滤属性和函数。可选模式包括:

    • PUB_ONLY: 默认值,仅显示公共属性。
    • ALL: 不进行过滤,显示所有属性。
    • SPECIAL: 过滤掉Python特殊函数(除构造函数外)。
    • OTHER: 过滤受保护和私有属性。
  • 创建特定类的图表 (--class <class>, -c <class>): 创建一个包含与 <class> 相关的所有类的类图。默认情况下使用 -ASmy 选项。

  • 祖先代数 (--show-ancestors <ancestor>, -a <ancestor>; --all-ancestors, -A): 控制是否以及多少代祖先类应被显示。

  • 关联级别 (--show-associated <association_level>, -s <association_level>; --all-associated, -S): 控制是否以及多少级相关联的类应该被显示。

  • 显示内置对象 (--show-builtin, -b): 是否在类表示中包含内置对象。

  • 模块名称 (--module-names <y or n>, -m <y or n>): 决定是否在类的表示中包含模块名。

  • 不显示方法和属性 (--only-classnames, -k): 如果设置,将不在类框中显示属性和方法,这会覆盖 -f 的设置。

  • 输出格式 (--output <format>, -o <format>): 设置输出文件的格式。支持的格式包括:dot, puml, plantuml, mmd, html 等。对于其他格式,如果安装了 Graphviz,可以尝试使用 dot 工具转换。

  • 颜色化输出 (--colorized): 启用彩色输出,属于同一包的类/模块获得相同颜色。

  • 忽略文件或目录 (--ignore <file[,file...]>): 指定需要跳过的文件或目录列表。

  • 项目名称 (--project <project name>, -p <project name>): 设置项目名称。

  • 输出目录 (--output-directory <output_directory>, -d <output_directory>): 设置输出目录路径。

3.2 示例

  1. 基本示例:为给定项目生成类图和包图(.dot 格式)。
pyreverse path/to/your/python/project/
  1. 生成 PNG 图像:结合 Graphviz 将 .dot 文件转换为 SVG 图像。
dot -Tpng classes.dot -o output.svg
  1. 仅显示公共成员:确保只显示公共成员而不包括私有变量。
pyreverse --filter-mode PUB_ONLY path/to/your/python/project/
  1. 专注于类引用关系:若想专注于类之间的引用关系而非详细展示每个类的所有成员,可以使用 -k 参数不显示属性和方法。
pyreverse --only-classnames -p myprojectname path/to/your/python/project/

例如输出的结果如下:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SmallerFL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值