围绕大规模Python源代码分析的工具。
项目描述
Sourced
围绕大规模Python源代码分析的工具。
用法
目前有两个数据集: pypi-all
和 pypi-popular
,尽管如果您打算保持样本大小较小,我强烈推荐使用 pypi-popular
(与 pypi-all
相比,使用它可以得到更多相关结果的机会更高)。
您可以选择任何数量的具有不同样本大小的数据集
$ sourced datasets create \
--source pypi-popular \
--sample-size 10 \
playground
默认情况下,它将下载所有在 ~/.cache/sourced/0.0.1/<name>
下的源代码,但可能更愿意在您的家目录之外有一个单独的目录
$ sourced datasets create \
--source pypi-popular \
--sample-size 5000 \
--base-data-dir /mnt/my-giant-disk/sourced-datasets \
top-5000-packages
只要这些路径存在,就可以通过CLI访问所有这些数据集
$ sourced datasets list
playground /path/to/.cache/sourced/0.0.1/playground
top-5000-packages /path/to/my-giant-disk/sourced-datasets/top-5000-packages
在源代码上运行分析
一旦您已经检出数据集,您就可以使用本包中提供的工具对该数据集运行任何分析。以下是一个简单的程序,它会解析数据集中的每个文件,以找出最常见的名称
from __future__ import annotations
import ast
import tokenize
from argparse import ArgumentParser
from collections import Counter
from sourced import Sourced
def most_common_name(file: str) -> dict[str, int]:
usage: dict[str, int] = {}
try:
with tokenize.open(file) as stream:
tree = ast.parse(stream.read())
except BaseException as exc:
return usage
for node in ast.walk(tree):
if isinstance(node, ast.Name):
usage.setdefault(node.id, 0)
usage[node.id] += 1
return usage
def main():
parser = ArgumentParser()
parser.add_argument("dataset")
options = parser.parse_args()
sourced = Sourced()
results = Counter()
for result in sourced.run_on(options.dataset, most_common_name):
results.update(result)
for name, count in results.most_common(n=20):
print(f"{name}: {count}")
if __name__ == "__main__":
main()
$ python examples/python_specific_source.py playground
Found 10 sources
Collected 959 files from 10 unique projects.
self: 24489
os: 1821
str: 1735
request: 1157
response: 1064
value: 1029
pytest: 984
mock: 966
name: 837
r: 770
isinstance: 715
len: 705
cmd: 701
client: 674
params: 672
path: 668
key: 659
pool: 623
int: 599
config: 553
项目详情
下载文件
为您的平台下载文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。
源分布
sourced-0.1.0.tar.gz (10.4 kB 查看哈希)
构建分布
sourced-0.1.0-py3-none-any.whl (11.4 kB 查看哈希值)
关闭
sourced-0.1.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8e8def3e7508c916a3d4da441964802e2a303fd134e365cc4dda251609b99db8 |
|
MD5 | ee2124e876328c388b585d2d85a29114 |
|
BLAKE2b-256 | ed4de48027b4a4768336fde04cd3b55230362f59c9a03c3f3e6a251d923b72f8 |
关闭
sourced-0.1.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | c53731bc097d1da6f566ba9a9d164c155912ce1b446d41cc5e8d1563642292d0 |
|
MD5 | 603b3fc447ed540577804a320034d90f |
|
BLAKE2b-256 | 08b14b9b892512e7fdc560ca458ea5c2b90be22338a130e3947a64be6a9affb7 |