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核心。默認情況下,庫將嘗試使用計算機上可用的所有內核。