用于测量python类内聚性的工具。

cohesion的Python项目详细描述


凝聚力

Build StatusBuild StatusCoverage StatusPython VersionsPyPI Version

内聚性是度量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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
空间计数器在Java中不起作用   json在java中表示XPath列表的最佳方式   java报警服务接收器安卓   java注入bean在自定义的all存储库中   java从迁移到Spring MVC 4+Hibernate5   JavaEclipseIDE透视图被缓存,更改没有任何影响   java Hibernate:在将对象插入Derby嵌入式数据库时引发SQLGrammerException   适用于mp4或mp3文件的java Exoplayer自动流媒体质量   安卓如何在java的静态方法中使用这个关键字?   SSL服务器端的spring主机名验证+Tomcat和Java 8   java Eclipse强制刷新IDs   java有可能返回Mono。只是从GetMapping(“/”)处理程序中获取(“索引”)吗?   arraylist当我用java编程时,我遇到了这个错误,有人能告诉我到底出了什么问题吗?   java如何更改Apache CXF web服务中的日期时间格式   Jfoenix ChipView中的java多线程   java任务在Spark上不可序列化