Python - 运行流程图, call graph, 调用图
解决方案
- pycallgraph(感觉直接用pycallgraph grahviz命令生成的图并不是我想要的)
- 如何去阅读并学习一些优秀的开源框架的源码? - mailto1587的回答 - 知乎
https://www.zhihu.com/question/26766601/answer/33952627 - traceback(没有图,直接在控制台打印)
解决方案2(用django来示范)
# django里的一个view
# views.py
class ArticleSearchView(View):
def get(self, request):
form = SearchForm(request.GET)
from .utils import cheese # 在这里插入
cheese() # 在这里插入
if form.is_valid():
keyword = form.cleaned_data.get("keyword")
if keyword:
article_list = Article.objects.filter(title__icontains=keyword)
return render(request, 'blog/search.html', {'form': form, 'article_list': article_list})
else:
form = SearchForm()
return render(request, 'blog/search.html', {'form': form, 'article_list': False, })
# utils.py
from __future__ import unicode_literals
def cheese(frame=None, slient=False):
import sys
import tempfile
import webbrowser
import pygraphviz as pgv
if not frame:
frame = sys._getframe().f_back
G = pgv.AGraph(strict=False, directed=True)
stack = []
node_set = set()
subgraph_set = {}
while frame:
filename = frame.f_code.co_filename
firstlineno = frame.f_code.co_firstlineno
function = frame.f_code.co_name
node = '{0}:{1}:{2}'.format(filename, firstlineno, function)
if node not in node_set:
node_set.add(node)
if filename not in subgraph_set:
subgraph_set[filename] = G.add_subgraph(
name='cluster' + filename,
label=filename
)
subgraph = subgraph_set[filename]
subgraph.add_node(
node,
label='{0}:{1}'.format(firstlineno, function)
)
stack.append(frame)
frame = frame.f_back
stack.reverse()
len_stack = len(stack)
for index, start in enumerate(stack):
if index + 1 < len_stack:
start_filename = start.f_code.co_filename
start_firstlineno = start.f_code.co_firstlineno
start_function = start.f_code.co_name
start_lineno = start.f_lineno
start_subgraph = subgraph_set[start_filename]
end = stack[index + 1]
end_filename = end.f_code.co_filename
end_firstlineno = end.f_code.co_firstlineno
end_function = end.f_code.co_name
end_subgraph = subgraph_set[end_filename]
if index == 0:
color = 'green'
elif index == len_stack - 2:
color = 'red'
else:
color = 'black'
G.add_edge(
'{0}:{1}:{2}'.format(start_filename,
start_firstlineno,
start_function),
'{0}:{1}:{2}'.format(end_filename,
end_firstlineno,
end_function),
color=color,
ltail=start_subgraph.name,
lhead=end_subgraph.name,
label='#{0} at {1}'.format(index + 1, start_lineno)
)
fd, name = tempfile.mkstemp('.png')
G.draw(name, prog='dot')
G.close()
if not slient:
webbrowser.open('file://' + name)
return name
解决方案2的结果图

解决方案3(用Flask来示范)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
import traceback
traceback.print_stack()
return 'Yet another hello!'
if __name__ == '__main__':
app.run()
解决方案3的结果图

Python - 运行流程图, call graph, 调用图的更多相关文章
- 使用gprof2dot和graphivz生成程序运行调用图
使用gprof2dot和graphivz生成程序运行调用图 gprof2dot是一个将gprof生成的输出转换为dot脚本的工具.通过给定一个gprof的输出文件,将其转换为生成程序调用图的dot脚本 ...
- CodeViz产生函数调用图
一.安装过程 1.CodeViz的安装 0).确认你已经安装Graphviz[ http://www.graphviz.org/] 1).下载CodeViz.[ http://www.csn.ul ...
- 如何在本地使用scala或python运行Spark程序
如何在本地使用scala或python运行Spark程序 包含两个部分: 本地scala语言编写程序,并编译打包成jar,在本地运行. 本地使用python语言编写程序,直接调用spark的接口, ...
- Python黑帽编程1.3 Python运行时与包管理工具
Python黑帽编程1.3 Python运行时与包管理工具 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and ...
- Java和Python运行速度对比
Java和Python运行速度对比:同一个函数运行一百万次,Java耗时0.577秒,Python耗时78秒--135倍的差距. 版本:Java 8,Python 2.7.10 Java测试代码: i ...
- python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案
python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案 importerror:no mo ...
- Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案
#Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案 ##问题描述 使用Python2.7.x运行GAE时有时会报这个错误 ```py ...
- 嵌入式Linux系统运行流程图
/************************************************************************ * 嵌入式Linux系统运行流程图 * 说明: * ...
- calltree看代码调用图
calltree是在linux下面看c代码(尤其是复杂的内核代码)的神器. 推荐 calltree+vim + ctags + cscope + taglist [ vim: 搭建vim看代码的环境 ...
随机推荐
- windows10 安装盘制作以及重装系统
1,安装盘制作 1),需要有系统的源文件才能装机,源文件请百度“windows10”选择microsoft官网的链接:这里,选择立即下载工具 2),很快就能下载好,双击运行,选择制作启动盘.启动盘的特 ...
- 4817 [Sdoi2017]树点涂色
题目描述 Bob 有一棵 n 个点的有根树,其中 1 号点是根节点.Bob 在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...
- 0002 增加APP配置
创建好工程后的第一步就是配置APP,目前有两个APP,配置APP的步骤如下: 01 APP目录配置 01.1 在APP目录下创建Templates目录,用于存储模板文件 01.2 在APP目录下创建v ...
- AcWing 1017. 怪盗基德的滑翔翼
#include<iostream> using namespace std ; ; int f[N],g[N]; int w[N]; int main() { int t; cin> ...
- Python中读取目录里的文件并按排序列出
1.os.listdir():用于返回指定的文件夹包含的文件或文件夹的名字的列表. 如: dir ='F:/Home_01/img'#当前目录 filenames=os.listdir(dir)#fi ...
- SQL Server 检查和处理死锁问题
SELECT spid, blocked, DB_NAME(sp.dbid) AS DBName, program_name, waitresource, lastwaittype, sp.login ...
- 【Python】简单计算器
#python 3.7.1 print("operation codes are:") print("1 for multiply") print(" ...
- 五、抗DDOS防火墙
简介 DDoS全名是Distribution Denial of service (分布式拒绝服务攻击).拒绝服务攻击的攻击方式有很多种,最基本的Dos攻击就是利用合理的服务请求来占用过多的服务资源, ...
- 2、Spring-RootApplicationContext-refresh
上一篇文中提到父容器root applicationContext最后是调用XmlWebApplicationContext去实现的, 但是什么时候开始解析标签(默认标签.自定义标签).注册bean以 ...
- Git 添加远程github仓库的时候提示错误:fatal: remote origin already exists.
1.先删除远程 Git 仓库 $ git remote rm origin 2.再添加远程 Git 仓库 $ git remote add origin git@github.com:wsydxian ...