Created
June 16, 2017 22:49
-
-
Save enedil/0064978b6ef0c0a50ec267c5ec5955d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
''' | |
Module counts how dense is Python source code in | |
terms of lexical entities per LoC. | |
''' | |
import ast | |
def traverse(node, line_nos): | |
''' | |
go through the AST and count lines | |
''' | |
children = list(ast.iter_child_nodes(node)) | |
try: | |
current_line = node.lineno | |
except AttributeError: | |
current_line = 0 | |
count = 1 | |
line_nos.update([current_line]) | |
if len(children) == 0: | |
return 1 | |
for child in ast.iter_child_nodes(node): | |
count += traverse(child, line_nos) | |
return count | |
def count_code_entities(source): | |
''' | |
parse the source&count | |
''' | |
line_nos = set() | |
tree = ast.parse(source) | |
# -1, to compensate for line 0 which serves as default | |
# for lexical entities not having lineno property | |
return traverse(tree, line_nos), len(line_nos) - 1 | |
def main(): | |
''' | |
parsin&stuff | |
''' | |
from argparse import ArgumentParser, FileType | |
parser = ArgumentParser() | |
parser.add_argument('infile', | |
help='source file to analyze', | |
type=FileType(), | |
default='-', | |
nargs='?',) | |
args = parser.parse_args() | |
with args.infile as file: | |
source = file.read() | |
name = file.name | |
count, number = count_code_entities(source) | |
print(name, ': ', round(count/number, 4), 'entities/line of code') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment