Python生成代码导图和UML类图

安装依赖环境

这里使用的是Python2.7
安装依赖包:

pip install pycallgraph

生成流程图

方式一:

在python代码中执行:

from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
with PyCallGraph(output=GraphvizOutput):
	# 添加执行的代码、函数
    pass

在执行结束之后会生成流程图

方式二:

安装环境

windows上安装graphviz可执行文件
安装包:https://pythondict.com/go/?url=https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi
安装完成后,为方便使用,将安装路径添加到环境变量中

这种方式需要使用bash来执行,我这里使用的是git bash
安装地址: https://gitforwindows.org/

示例

# 文件名: graphs.py
def A():
    pass

def B():
    A()

def C():
    A()
    B()

if __name__ == '__main__':
    C()

打开git bash:
在这里插入图片描述
执行:

 pycallgraph graphviz graphs.py

在本地会生成一个png图片:
在这里插入图片描述

生成UML类图

安装pylint:

pip install pylint

打开git bash, 命令格式
  pyreverse -o 生成的文件类型 文件或工程目录
如:

#文件名: graphs.py

class A(object):
    def a(self):
        pass

class B(A):
    def b(self):
        pass

class C(A):
    def c(self):
        pass

class D(B, C):
    def d(self):
# 建议文件类型选用pdf, 这样生成的图比较清晰
pyreverse -o pdf graphs.py

生成如下:
在这里插入图片描述

// --help查看更多选项
$ pyreverse --help
Usage:
  pyreverse [options] <packages>

  create UML diagrams for classes and modules in <packages>


Options:
  -h, --help            show this help message and exit
  -f <mode>, --filter-mode=<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 [current: PUB_ONLY]
  -c <class>, --class=<class>
                        create a class diagram with all classes related to
                        <class>; this uses by default the options -ASmy
                        [current: none]
  -a <ancestor>, --show-ancestors=<ancestor>
                        show <ancestor> generations of ancestor classes not in
                        <projects>
  -A, --all-ancestors   show all ancestors off all classes in <projects>
  -s <ass_level>, --show-associated=<ass_level>
                        show <ass_level> levels of associated classes not in
                        <projects>
  -S, --all-associated  show recursively all associated off all associated
                        classes
  -b, --show-builtin    include builtin objects in representation of classes
  -m [yn], --module-names=[yn]
                        include module name in representation of classes
  -k, --only-classnames
                        don't show attributes and methods in the class boxes;
                        this disables -f values
  -o <format>, --output=<format>
                        create a *.<format> output file if format available.
                        [current: dot]
  --ignore=<file[,file...]>
                        Add files or directories to the blacklist. They should
                        be base names, not paths. [current: CVS]
  -p <project name>, --project=<project name>
                        set the project name. [current: none]

猜你喜欢

转载自blog.csdn.net/weixin_43749427/article/details/112977556