Python:代码统计

发布于 2024-11-03 10:05:30 字数 120 浏览 0 评论 0原文

你知道是否有一个可以生成代码统计信息的 Python 库吗?我正在考虑指向一个包并获取类、函数、方法、文档块行等的数量。

它最终可能包含无用的东西,例如 lambda 的数量或其他疯狂的统计数据,只是为了好玩。

Do you know if there's a Python library that generates statistics about code? I'm thinking about pointing to a package and getting number of classes, functions, methods, docblock lines etc.

It could eventually include useless stuff like number of lambdas or other crazy statistics, just for fun.

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

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

发布评论

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

评论(3

(り薆情海 2024-11-10 10:05:31

人们通常不会用十几两行代码就能完成的事情来制作包。下面分析了 all python 语法的用法,并返回一个字典,将 ast 节点映射到该节点在源中出现的次数。显示 defclass 语句数量的示例也在其下方。

import collections
import os
import ast

def analyze(packagedir):
    stats = collections.defaultdict(int)
    for (dirpath, dirnames, filenames) in os.walk(packagedir):
        for filename in filenames:
            if not filename.endswith('.py'):
                continue

            filename = os.path.join(dirpath, filename)

            syntax_tree = ast.parse(open(filename).read(), filename)
            for node in ast.walk(syntax_tree):
                stats[type(node)] += 1   

    return stats

print("Number of def statements:", analyze('.')[ast.FunctionDef])
print("Number of class statements:", analyze('.')[ast.ClassDef])

People don't generally make packages out of things that can be done in a dozen or two lines of code. The following analyzes usage of all python syntax and returns a dictionary mapping ast nodes to how many times that node came up in the source. Examples showing the number of def and class statements are below it as well.

import collections
import os
import ast

def analyze(packagedir):
    stats = collections.defaultdict(int)
    for (dirpath, dirnames, filenames) in os.walk(packagedir):
        for filename in filenames:
            if not filename.endswith('.py'):
                continue

            filename = os.path.join(dirpath, filename)

            syntax_tree = ast.parse(open(filename).read(), filename)
            for node in ast.walk(syntax_tree):
                stats[type(node)] += 1   

    return stats

print("Number of def statements:", analyze('.')[ast.FunctionDef])
print("Number of class statements:", analyze('.')[ast.ClassDef])
七禾 2024-11-10 10:05:31

你可以看看Pymetrics,或者查看其他工具枚举在那里

you can have a look at Pymetrics, or check other tools enumerated there

指尖微凉心微凉 2024-11-10 10:05:31

也许 Tahar 可以提供帮助,它显示每个时间的统计信息函数、方法、类和模块是(以代码行为单位)。但是,由于它使用检查模块,因此如果它分析的模块之一启动 GUI 或类似的东西,它可能会以意外的方式运行。

有一天我会转而使用 AST,尽管我不知道 AST 是否可以提供类似于 inform.getsourcelines() 的服务?

(编辑)

Mergou (使用 tokenize 重写 tahar模块)处于 alpha 阶段,这里有一个实际操作的视频:http://www.youtube.com/watch?v=PI0iBZmInFU&feature=youtu.be

Maybe Tahar can help, it displays statistics about how long each function, method, class and module are (in lines of code). However, since it's using the inspect module, it may run in unexpected ways if one of the module it analyzes launches a GUI or something like that.

I'll switch to using AST someday, although I don't know if AST can provide a service that is similar to inspect.getsourcelines() ?

(EDIT)

Mergou (the rewrite of tahar using the tokenize module) is in alpha, here's a video of it in action : http://www.youtube.com/watch?v=PI0iBZmInFU&feature=youtu.be

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