从 python 脚本运行 vulture [英] Running vulture from a python script

查看:31
本文介绍了从 python 脚本运行 vulture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种在 python 脚本中运行 vulture(它在 python 项目中找到未使用的代码)的方法.
秃鹫文档可以在这里找到:https://pypi.org/project/vulture/

有人知道怎么做吗?

我知道使用 vulture 的唯一方法是通过 shell 命令.我尝试使用模块子进程调整脚本中的 shell 命令,如下所示:

process = subprocess.run(['vulture', '.'], check=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)

我认为这与运行 shell 命令vulture"具有相同的效果.

但它不起作用.

有人可以帮忙吗?谢谢

解决方案

Vulture dev here.

Vulture 包公开了一个 API,名为 noreferrnof">code>scavenge - 它在解析命令行参数后在内部用于运行分析(此处位于 vulture.main).

它接受一个 Python 文件/目录列表.对于每个目录,Vulture 会分析所有包含的 *.py 文件.

分析当前目录:

导入秃鹫v = vulture.Vulture()v.scavenge(['.'])

如果只想将结果打印到标准输出,可以调用:

v.report()

但是,也可以对 Vulture 的结果执行自定义分析/过滤器.方法 used_code> 返回 vulture.Item 的列表 对象 - 保存未使用代码的名称、类型和位置.

为了这个答案,我将打印所有未使用对象的名称:

 用于 v.get_unused_code() 中的项目:打印(项目名称)

有关详细信息,请参阅 - https://github.com/jendrikseipp/vulture

I'm trying to find a way to run vulture (which finds unused code in python projects) inside a python script.
vulture documentation can be found here: https://pypi.org/project/vulture/

Does anyone know how to do it?

The only way I know to use vulture is by shell commands. I tried to tun the shell commands from the script, using module subprocess, something like this:

process = subprocess.run(['vulture', '.'], check=True, 
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)

which I though would have the same effect as running the shell command "vulture ."

but it doesn't work.

Can anyone help? Thanks

解决方案

Vulture dev here.

The Vulture package exposes an API, called scavenge - which it uses internally for running the analysis after parsing command line arguments (here in vulture.main).

It takes in a list of Python files/directories. For each directory, Vulture analyzes all contained *.py files.

To analyze the current directory:

import vulture
v = vulture.Vulture()
v.scavenge(['.'])

If you just want to print the results to stdout, you can call:

v.report()

However, it's also possible to perform custom analysis/filters over Vulture's results. The method vulture.get_unused_code returns a list of vulture.Item objects - which hold the name, type and location of unused code.

For the sake of this answer, I'm just gonna print the name of all unused objects:

for item in v.get_unused_code():
     print(item.name)

For more info, see - https://github.com/jendrikseipp/vulture

这篇关于从 python 脚本运行 vulture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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