跳转到主要内容

围绕大规模Python源代码分析的工具。

项目描述

Sourced

围绕大规模Python源代码分析的工具。

用法

目前有两个数据集: pypi-allpypi-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 查看哈希值)

上传时间 Python 3

支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面