【发布时间】:2022-01-22 11:13:33
【问题描述】:
我正在对一些 python 库进行静态分析,这些库的源代码可在 Github 上找到。有没有办法找出某个库在其他应用程序中使用了多少次? GitHub 洞察力仅提供一个月的信息,在我的情况下,这不足以比较库的受欢迎程度。
提前致谢。
【问题讨论】:
我正在对一些 python 库进行静态分析,这些库的源代码可在 Github 上找到。有没有办法找出某个库在其他应用程序中使用了多少次? GitHub 洞察力仅提供一个月的信息,在我的情况下,这不足以比较库的受欢迎程度。
提前致谢。
【问题讨论】:
是的,有。我最近对这个主题进行了研究。首先,我会推荐https://sourcegraph.com/search。 Sourcegraph 拥有数百万个存储库,并允许在这些存储库中进行非常强大的搜索。通过这个网站,您可以搜索例如content:"import my_module" language:Python 在实践中找到my_module 的大量使用。该工具允许使用许多不同的过滤器,并且非常有用。 (我与 Sourcegraph 没有任何关系。)
我还想在这里添加我上述研究的结果。我制作了一个名为module_dependencies 的模块,它可以完全用于这个任务。它依赖于 sourcegraph,可以这样使用:
from module_dependencies import Module
from pprint import pprint
# Attempt to find 1000 imports of the "nltk" module
# in both Python files and Jupyter Notebooks each
module = Module("nltk", count="1000")
# Frequency of use of objects within the module
pprint(module.usage()[:15])
# How frequently the module was used (not particularly useful unless count="all")
print("NLTK was used", module.nested_usage()["nltk"]['occurrences'], "times")
# Show an interactive plot
module.plot()
这个程序输出:
[2022-01-03 14:14:39,127] [module_dependencies.module.session] [INFO ] - Fetching Python source code containing imports of `nltk`...
[2022-01-03 14:14:42,824] [module_dependencies.module.session] [INFO ] - Fetched Python source code containing imports of `nltk` (status code 200)
[2022-01-03 14:14:42,825] [module_dependencies.module.session] [INFO ] - Parsing 6,830,859 bytes of Python source code as JSON...
[2022-01-03 14:14:42,865] [module_dependencies.module.session] [INFO ] - Parsed 6,830,859 bytes of Python source code as JSON...
[2022-01-03 14:14:42,866] [module_dependencies.module.session] [INFO ] - Extracting dependencies of 725 files of Python source code...
Parsing Files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 725/725 [00:02<00:00, 258.48files/s]
[2022-01-03 14:14:45,702] [module_dependencies.module.session] [INFO ] - Extracted dependencies of 725 files of Python source code.
[2022-01-03 14:14:45,703] [module_dependencies.module.session] [INFO ] - Fetching Jupyter Notebook source code containing imports of `nltk`...
[2022-01-03 14:14:48,726] [module_dependencies.module.session] [INFO ] - Fetched Jupyter Notebook source code containing imports of `nltk` (status code 200)
[2022-01-03 14:14:48,726] [module_dependencies.module.session] [INFO ] - Parsing 25,713,281 bytes of Jupyter Notebook source code as JSON...
[2022-01-03 14:14:48,886] [module_dependencies.module.session] [INFO ] - Parsed 25,713,281 bytes of Jupyter Notebook source code as JSON...
[2022-01-03 14:14:48,888] [module_dependencies.module.session] [INFO ] - Extracting dependencies of 495 files of Jupyter Notebook source code...
Parsing Files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 495/495 [00:02<00:00, 167.09files/s]
[2022-01-03 14:14:51,851] [module_dependencies.module.session] [INFO ] - Extracted dependencies of 495 files of Jupyter Notebook source code.
[('nltk.tokenize.word_tokenize', 327),
('nltk.download', 298),
('nltk.corpus.stopwords.words', 257),
('nltk.tokenize.sent_tokenize', 126),
('nltk.stem.porter.PorterStemmer', 115),
('nltk.stem.wordnet.WordNetLemmatizer', 99),
('nltk.tag.pos_tag', 75),
('nltk.stem.snowball.SnowballStemmer', 48),
('nltk.data.path.append', 42),
('nltk.probability.FreqDist', 42),
('nltk.tokenize.RegexpTokenizer', 42),
('nltk.tokenize.TweetTokenizer', 35),
('nltk.corpus.wordnet.synsets', 33),
('nltk.data.load', 32),
('nltk.translate.bleu_score.corpus_bleu', 29)]
NLTK was used 2487 times
这个情节可以interacted with查看每个部分被使用的次数,包括根本身。
要非常简洁地回答您的问题,您可以使用以下内容:
from module_dependencies import Module
mod_name = "mymodule"
module = Module(mod_name , count="all")
print(f"{mod_name} was used {module.nested_usage()[mod_name]['occurrences']} times")
这在托管在 GitHub(或 Gitlab)上的实际项目中提供了清晰、可验证的使用数量。 module_dependencies 还会提取到那些使用您感兴趣的模块的存储库和文件的链接,并跟踪每个存储库有多少星,以防您对分析感兴趣。
有关module_dependencies 的文档,请参阅https://tomaarsen.github.io/module_dependencies/。再说一遍:我是这个模块的作者。
【讨论】:
import jwt,因此它会找到其中任何一个的用途。但是,如果 PyJWT 使用例如jwt.foo 和 python-jwt 使用 jwt.bar,然后您可以手动对来自 these methods 的输出进行后处理以区分它们。例如,绘制jwt 的用法,并使用领域知识来识别哪个是哪个。
【讨论】: