simudaru's blog

Python, Rなどのメモを残していこうと思います。  よろしくお願いいたします。

【python】コードの品質管理について その2 pylint

下記の続き。simudaru.hatenablog.com

下記を参考にした。
Python Project Howto (日本語訳) — Python Project Howto 日本語訳

pylint

pipでインストール可能
コードの多くの潜在的な問題を警告してくれる。
参考URLによると、8点以上というのが一つの基準のようだ。


前回に引き続き、fizzbuzzの例を示す。

fizzbuzz.py
# coding: utf-8
# ---------------------------------------------------------
# FizzBuzz問題
# ---------------------------------------------------------


def fizzbuzz(x):
    if x % 15 == 0:
        ans = 'FizzBuzz'
    elif x % 3 == 0:
        ans = 'Fizz'
    else:
        ans = 'Buzz'
    return ans

$ pylint fizzbuzz.py

No config file found, using default configuration
************* Module fizzbuzz
W:  3, 0: Cannot decode using encoding "utf-8", unexpected byte at position 10 (
invalid-encoded-data)
C:  1, 0: Missing module docstring (missing-docstring)
C:  7, 0: Invalid argument name "x" (invalid-name)
C:  7, 0: Missing function docstring (missing-docstring)


Report
======
8 statements analysed.

Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type     |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module   |1      |NC         |NC         |0.00        |0.00     |
+---------+-------+-----------+-----------+------------+---------+
|class    |0      |NC         |NC         |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|method   |0      |NC         |NC         |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|function |1      |NC         |NC         |0.00        |0.00     |
+---------+-------+-----------+-----------+------------+---------+



Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |8      |53.33 |NC       |NC         |
+----------+-------+------+---------+-----------+
|docstring |0      |0.00  |NC       |NC         |
+----------+-------+------+---------+-----------+
|comment   |4      |26.67 |NC       |NC         |
+----------+-------+------+---------+-----------+
|empty     |3      |20.00 |NC       |NC         |
+----------+-------+------+---------+-----------+



Duplication
-----------

+-------------------------+----+---------+-----------+
|                         |now |previous |difference |
+=========================+====+=========+===========+
|nb duplicated lines      |0   |NC       |NC         |
+-------------------------+----+---------+-----------+
|percent duplicated lines |0   |NC       |NC         |
+-------------------------+----+---------+-----------+



Messages by category
--------------------

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |3      |NC       |NC         |
+-----------+-------+---------+-----------+
|refactor   |0      |NC       |NC         |
+-----------+-------+---------+-----------+
|warning    |1      |NC       |NC         |
+-----------+-------+---------+-----------+
|error      |0      |NC       |NC         |
+-----------+-------+---------+-----------+



Messages
--------

+---------------------+------------+
|message id           |occurrences |
+=====================+============+
|missing-docstring    |2           |
+---------------------+------------+
|invalid-name         |1           |
+---------------------+------------+
|invalid-encoded-data |1           |
+---------------------+------------+



Global evaluation
-----------------
Your code has been rated at 5.00/10

色々出力された。最後に点数が出力される。
指摘事項を直していくと、最終的に以下のようになった。

fizzbuzz.py
# coding: utf-8
""" FizzBuzz問題 """


def fizzbuzz(x):
    """ FizzBuzz関数

    引数xが3で割り切れる場合には'Fizz'を、
    引数xが5で割り切れる場合には'Buzz'を、
    引数xが3でも5でも割り切れる場合には'FizzBuzz'を、
    それ以外の場合は引数xを返す
    """
    if x % 15 == 0:
        ans = 'FizzBuzz'
    elif x % 3 == 0:
        ans = 'Fizz'
    else:
        ans = 'Buzz'
    return ans

$ pylint fizzbuzz.py

No config file found, using default configuration
************* Module fizzbuzz
C:  5, 0: Invalid argument name "x" (invalid-name)


Report
======
8 statements analysed.

Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type     |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module   |1      |1          |=          |100.00      |0.00     |
+---------+-------+-----------+-----------+------------+---------+
|class    |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|method   |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|function |1      |1          |=          |100.00      |0.00     |
+---------+-------+-----------+-----------+------------+---------+



Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |8      |40.00 |8        |=          |
+----------+-------+------+---------+-----------+
|docstring |8      |40.00 |8        |=          |
+----------+-------+------+---------+-----------+
|comment   |1      |5.00  |1        |=          |
+----------+-------+------+---------+-----------+
|empty     |3      |15.00 |2        |+1.00      |
+----------+-------+------+---------+-----------+



Duplication
-----------

+-------------------------+------+---------+-----------+
|                         |now   |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines      |0     |0        |=          |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |0.000    |=          |
+-------------------------+------+---------+-----------+



Messages by category
--------------------

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |1      |2        |-1.00      |
+-----------+-------+---------+-----------+
|refactor   |0      |0        |=          |
+-----------+-------+---------+-----------+
|warning    |0      |0        |=          |
+-----------+-------+---------+-----------+
|error      |0      |0        |=          |
+-----------+-------+---------+-----------+



Messages
--------

+-------------+------------+
|message id   |occurrences |
+=============+============+
|invalid-name |1           |
+-------------+------------+



Global evaluation
-----------------
Your code has been rated at 8.75/10 (previous run: 7.50/10, +1.25)

引数名が短すぎるというメッセージが残っているが、
この程度の関数ならxで十分だろう。

次回はUnittestについて。