Python:代码统计
你知道是否有一个可以生成代码统计信息的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
人们通常不会用十几两行代码就能完成的事情来制作包。下面分析了 all python 语法的用法,并返回一个字典,将 ast 节点映射到该节点在源中出现的次数。显示
def
和class
语句数量的示例也在其下方。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
andclass
statements are below it as well.你可以看看Pymetrics,或者查看其他工具枚举在那里
you can have a look at Pymetrics, or check other tools enumerated there
也许 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