在大型python项目中查找死代码 [英] Finding dead code in large python project

查看:41
本文介绍了在大型python项目中查找死代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过你如何在Python 代码? 但这真的很旧,并不能真正回答我的问题.

I've seen How can you find unused functions in Python code? but that's really old, and doesn't really answer my question.

我有一个包含多个库的大型 Python 项目,这些库由多个入口点脚本共享.这个项目已经与许多作者一起积累了很多年,所以有很多死代码.你知道该怎么做.

I have a large python project with multiple libraries that are shared by multiple entry point scripts. This project has been accreting for many years with many authors, so there's a whole lot of dead code. You know the drill.

我知道找到所有死代码是不可判定的.我所需要的只是一个工具,它可以找到所有未在任何地方调用的函数.我们没有对基于函数名字符串调用函数做任何花哨的事情,所以我不担心任何病态的......

I know that finding all dead code is un-decidable. All I need is a tool that will find all functions that are not called anywhere. We're not doing anything fancy with calling functions based on the string of the function name, so I'm not worried about anything pathological...

我刚安装了 pylint,但它似乎是基于文件的,并不太注意文件间的依赖关系,甚至函数依赖关系.

I just installed pylint, but it appears to be file based, and not paying much attention to interfile dependencies, or even function dependencies.

显然,我可以在所有文件中 grep 为 def,从中获取所有函数名称,并为每个函数名称执行 grep.我只是希望已经有比这更聪明的东西了.

Clearly, I could grep for def in all of the files, get all of the function names from that, and do a grep for each of those function names. I'm just hoping that there's something a little smarter than that out there already.

ETA:请注意,我不期望或想要完美的东西.我知道我的停机问题证明和任何人一样(不是真的,当我在看一些递归可枚举的东西时,我教授计算理论我知道).任何试图通过实际运行代码来近似它的事情都将花费太长时间.我只是想要一些语法上通过代码并说这个函数肯定会被使用.这个函数可能会被使用,而这个函数绝对不会被使用,其他人甚至似乎都不知道它存在!"前两个类别并不重要.

ETA: Please note that I don't expect or want something perfect. I know my halting-problem-proof just as well anyone (No really I taught theory of computation I know when I'm looking at something that is recursively enumerable). Any thing that tries to approximate it by actually running the code is going to take way too long. I just want something that syntactically goes through the code and says "This function is definitely used. This function MIGHT be used, and this function is definitely NOT used, no one else even seems to know it exists!" And the first two categories aren't important.

推荐答案

您可能想尝试一下 vulture.由于 Python 的动态特性,它无法捕获所有内容,但它可以捕获相当多的内容,而无需像coverage.py 和其他需要工作的完整测试套件.

You might want to try out vulture. It can't catch everything due to Python's dynamic nature, but it catches quite a bit without needing a full test suite like coverage.py and others need to work.

这篇关于在大型python项目中查找死代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆