用于测量python类内聚性的工具。
cohesion的Python项目详细描述
凝聚力
内聚性是度量python类内聚性的工具。
In computer programming, cohesion refers to the degree to which the elements of a module belong together. Thus, cohesion measures the strength of relationship between pieces of functionality within a given module. For example, in highly cohesive systems functionality is strongly related.
When cohesion is high, it means that the methods and variables of the class are co-dependent and hang together as a logical whole.
- Clean Code pg. 140
高内聚性的一些优势,同样来自维基百科:
- 减少了模块复杂度(它们比较简单,操作更少)。
- 提高了系统的可维护性,因为域中的逻辑更改 影响更少的模块,并且因为一个模块中的更改需要更少的 其他模块的更改。
- 提高了模块的可重用性,因为应用程序开发人员会发现 它们需要的组件在内聚的一组操作中更加容易 由模块提供。
安装
$ python -m pip install cohesion
$ cohesion -h
或
$ git clone https://github.com/mschwager/cohesion.git
$ cd cohesion
$ PYTHONPATH=lib/ python -m cohesion -h
使用
内聚性度量方法中类和实例变量的使用 是那个班的。
$ cat example.py
class ExampleClass1(object):
class_variable1 = 5
class_variable2 = 6
def func1(self):
self.instance_variable = 6
def inner_func(b):
return b + 5
local_variable = self.class_variable1
return local_variable
def func2(self):
print(self.class_variable2)
@staticmethod
def func3(variable):
return variable + 7
class ExampleClass2(object):
def func1(self):
self.instance_variable1 = 7
$ cohesion --files example.py --verbose
File: example.py
Class: ExampleClass1 (1:0)
Function: func1 2/3 66.67%
Variable: class_variable1 True
Variable: class_variable2 False
Variable: instance_variable True
Function: func2 1/3 33.33%
Variable: class_variable1 False
Variable: class_variable2 True
Variable: instance_variable False
Function: func3 0/3 0.00%
Variable: class_variable1 False
Variable: class_variable2 False
Variable: instance_variable False
Total: 33.33%
Class: ExampleClass2 (23:0)
Function: func1 1/1 100.00%
Variable: instance_variable1 True
Total: 100.00%
可以将--below
和--above
标志指定为仅显示具有
分别低于或高于指定百分比的内聚值。
薄片8支架
内聚支持由flake8
运行。首先,确保您的安装
注册cohesion
:
$ flake8 --version
3.2.1 (pyflakes: 1.0.0, cohesion: 0.8.0, pycodestyle: 2.2.0, mccabe: 0.5.3) CPython 2.7.12 on Linux
现在使用flake8
对文件进行lint:
$ flake8 example.py
example.py:1:1: H601 class has low cohesion
python版本
如果希望同时在python 2和python 3上运行内聚 代码,然后您将必须为两个版本安装它。即
$ python2 -m pip install cohesion
$ python3 -m pip install cohesion
然后使用相应版本运行模块:
$ python2 -m cohesion --files python2_file.py
$ python3 -m cohesion --files python3_file.py
开发
首先,安装开发包:
$ python -m pip install -r requirements-dev.txt
测试
$ nose2
起毛
$ flake8
覆盖范围
$ nose2 --with-coverage