python abc
1.0.0
ABC软件指标的Python实施:
杰里·菲茨帕特里克(Jerry Fitzpatrick)于1997年引入了ABC软件指标,以克服LOC的缺点。该度量标准将ABC分数定义为代表一组源代码语句的大小的值三联。通过计算程序中的任务(a),分支(b)的数量(a),有条件的数量(c)来计算ABC分数。 ABC分数可以应用于程序中的单个方法,功能,类,模块或文件。
菲茨帕特里克(Fitzpatrick)的原始论文在撰写本文时仅通过Wayback Machine提供,因此此存储库也包含了它的副本。
本文列出了C,C ++和Java的计数规则,因此这是该回购用于Python的规则:
else
, elif
, except
。assert
语句。 在选择的虚拟环境中安装要求,然后您可以看到可用的命令行参数:
$ python -m python_abc --help
usage: python_abc [-h] [--debug DEBUG] [--sort SORT] [--verbose VERBOSE] path
A python implementation of the ABC Software metric: https://en.wikipedia.org/wiki/ABC_Software_Metric
positional arguments:
path path to directory or file
optional arguments:
-h, --help show this help message and exit
--debug DEBUG display AST output for each element in the parsed tree
--sort SORT sort files from highest to lowest magnitude
--verbose VERBOSE display marked-up file
给定的file.py
包含以下文本:
if a and b :
print ( a )
else :
print ( b )
a = sum ( i for i in range ( 1000 ) if i % 3 == 0 and i % 5 == 0 )
def f ( n ):
def inner ( n ):
return n ** 2
if n == 0 :
return 1
elif n == 1 :
return n
elif n < 5 :
return ( n - 1 ) ** 2
return n * pow ( inner ( n ), f ( n - 1 ), n - 3 )
您可以按照以下方式获取准会输出:
$ python -m python_abc /path/to/file.py
/path/to/file.py < 1, 7, 10> (12.2)
传递verbose
标志将提供更多细节:
$ python -m python_abc file.py --verbose=true
cc | if a and b:
b | print(a)
c | else:
b | print(b)
|
abbcc | a = sum(i for i in range(1000) if i % 3 == 0 and i % 5 == 0)
|
| def f(n):
| def inner(n):
| return n ** 2
c | if n == 0:
| return 1
cc | elif n == 1:
| return n
cc | elif n < 5:
| return (n - 1) ** 2
bbb | return n * pow(inner(n), f(n - 1), n - 3)
file.py < 1, 7, 10> (12.2)
如果要检查抽象语法树对文件进行检查,则可以传递debug
标志,该文件将从树和由其产生的向量中打印出每个节点。
path
参数也可以是通往目录的路径,在这种情况下,将扫描该目录中的所有python文件(及其子目录),此时,通过ABC通过ABC对文件进行sort
来对文件进行排名很有用震级:
$ python -m python_abc . --sort
./calculate.py < 18, 56, 23> (63.2)
./vector.py < 12, 23, 11> (28.2)
./main.py < 10, 23, 8> (26.3)
./tests/test_vector.py < 4, 19, 10> (21.8)
./tests/__init__.py < 4, 12, 1> (12.7)
./tests/test_radon_test_cases.py < 1, 2, 1> (2.4)
./tests/test_calculate_condition.py < 1, 2, 1> (2.4)
./tests/test_calculate_empty.py < 1, 2, 1> (2.4)
./tests/test_calculate_assignment.py < 1, 2, 1> (2.4)
./tests/test_calculate_branch.py < 1, 2, 1> (2.4)
最后,您可以通过cores
参数告诉图书馆使用多少个CPU核心。默认情况下,库将尝试使用计算机上可用的所有内核。