如何找到Python代码中未使用的函数?

发布于 2024-07-16 04:13:05 字数 293 浏览 8 评论 0原文

因此,您在一个相当庞大的项目中存在一些遗留代码。 如何找到并删除死函数?

我看过这两个参考文献:查找未使用的代码用于查找 php 项目中未使用函数的工具,但它们似乎分别特定于 C# 和 PHP。

是否有一个 Python 工具可以帮助您找到源代码中其他任何地方都没有引用的函数(尽管有反射/等等)?

So you've got some legacy code lying around in a fairly hefty project. How can you find and delete dead functions?

I've seen these two references: Find unused code and Tool to find unused functions in php project, but they seem specific to C# and PHP, respectively.

Is there a Python tool that'll help you find functions that aren't referenced anywhere else in the source code (notwithstanding reflection/etc.)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

把梦留给海 2024-07-23 04:13:06

在 Python 中,您可以使用动态或静态代码分析器找到未使用的代码。 动态分析器的两个示例是 coveragefigleaf。 它们的缺点是您必须运行代码的所有可能的分支才能找到未使用的部分,但它们也有一个优点,即您可以获得非常可靠的结果。

或者,您可以使用静态代码分析器,它只查看代码,但不实际运行它。 它们运行得更快,但由于 Python 的动态特性,结果可能包含误报。
此类别中的两个工具是 pyflakesvulture。 Pyflakes 查找未使用的导入和未使用的局部变量。 Vulture 可以找到各种未使用和无法访问的代码。 (全面披露:我是 Vulture 的维护者。)

这些工具可在 Python 包索引 https://pypi.org/ 中找到。

In Python you can find unused code by using dynamic or static code analyzers. Two examples for dynamic analyzers are coverage and figleaf. They have the drawback that you have to run all possible branches of your code in order to find unused parts, but they also have the advantage that you get very reliable results.

Alternatively, you can use static code analyzers that just look at your code, but don't actually run it. They run much faster, but due to Python's dynamic nature the results may contain false positives.
Two tools in this category are pyflakes and vulture. Pyflakes finds unused imports and unused local variables. Vulture finds all kinds of unused and unreachable code. (Full disclosure: I'm the maintainer of Vulture.)

The tools are available in the Python Package Index https://pypi.org/.

冷血 2024-07-23 04:13:06

另一个选择是 deadcode Python 包。 它检测全局定义但未使用的变量、函数和类名称。 该工具用作静态代码检查器:它首先解析所有全局定义的名称,然后计算它们在整个代码库中被引用的次数。

您可以安装它:

pip install deadcode

并运行它

deadcode .

我已经实现了这个工具,因为其他工具没有为我完成这项工作。 使用 deadcode 作为 CI/CD 管道的一部分应该很方便。 我希望它有帮助。

Another option is deadcode Python package. It detects globally defined but unused variable, function and class names. This tool works as a static code checker: it first parses all globally defined names and then counts how many times are they referenced in a whole code base.

You can install it:

pip install deadcode

and run it

deadcode .

I have implemented this tool, since other ones didn't do the job for me. It should be convenient to use deadcode as part of CI/CD pipeline. I hope it helps.

红玫瑰 2024-07-23 04:13:06

我不确定这是否有帮助,但您可以尝试使用覆盖范围figleaf 或其他类似模块,记录源代码的哪些部分在您实际运行脚本/应用程序时使用。

I'm not sure if this is helpful, but you might try using the coverage, figleaf or other similar modules, which record which parts of your source code is used as you actually run your scripts/application.

是你 2024-07-23 04:13:06

由于 Python 代码的呈现方式相当严格,基于正则表达式来查找 def function_name(..) 构建函数列表会那么困难吗?

然后搜索每个名称并计算它在代码中出现的次数。 它自然不会考虑注释,但只要您查看少于两个或三个实例的函数...

这有点斯巴达,但听起来像是一个不错的昏昏欲睡的周末任务 =)

Because of the fairly strict way python code is presented, would it be that hard to build a list of functions based on a regex looking for def function_name(..) ?

And then search for each name and tot up how many times it features in the code. It wouldn't naturally take comments into account but as long as you're having a look at functions with less than two or three instances...

It's a bit Spartan but it sounds like a nice sleepy-weekend task =)

苏辞 2024-07-23 04:13:06

除非你知道你的代码使用反射,正如你所说,我会选择一个简单的 grep 。 不要低估 vim 中星号的力量(对文件中光标下的单词进行搜索),尽管这仅限于您当前正在编辑的文件。

您可以实现的另一个解决方案是拥有一个非常好的测试套件(不幸的是,很少发生),然后用弃用例程包装例程。 如果您得到弃用输出,则意味着该例程已被调用,因此它仍在某处使用。 这甚至适用于反射行为,但当然,您永远无法确定在执行例程调用时是否不会触发这种情况。

unless you know that your code uses reflection, as you said, I would go for a trivial grep. Do not underestimate the power of the asterisk in vim as well (performs a search of the word you have under your cursor in the file), albeit this is limited only to the file you are currently editing.

Another solution you could implement is to have a very good testsuite (seldomly happens, unfortunately) and then wrap the routine with a deprecation routine. if you get the deprecation output, it means that the routine was called, so it's still used somewhere. This works even for reflection behavior, but of course you can never be sure if you don't trigger the situation when your routine call is performed.

快乐很简单 2024-07-23 04:13:06

它不仅搜索函数名称,还搜索所有未使用的导入包。
您需要搜索所有导入的包(包括别名)的代码并搜索使用的函数,然后从每个包创建特定导入的列表(示例而不是 import os,替换为 from os import listdir, getcwd,... ...)

its not only searching function names, but also all the imported packages not in use.
you need to search the code for all the imported packages (including aliases) and search used functions, then create a list of the specific imports from each package (example instead of import os, replace with from os import listdir, getcwd,......)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文