LoginSignup
1
0

More than 3 years have passed since last update.

pythonをストレスなく使う!(Pylintに寄り添う)

Last updated at Posted at 2019-06-01

目的

 pythonをストレスなく使う!
そのためには、少しでも、理解のレベルを上げる必要あり。
なんでも、こだわって、、、、理解を深める。

 → Pylintに寄り添ってみようと考えました。。。

 :pencil: この記事で、多少(わずかに?)役立つかも?と思う記載項目:
    - if not a is b の件

素材として、使った書籍

入門 Python3
著者 Bill Lubanovic
発行所 株式会社オライリー・ジャパン

Pylintを使ってみる

UPPER_CASEの件

p99の以下のソースを、各々、rabbit1.pyとrabbit2.pyとして保存し、
pylintにかけてみた。
ソースは、以下。

rabbit1.py
rabbits = ['Flopsy', 'Mopsy', 'Cottontail', 'Peter']
current = 0
while current < len(rabbits):
    print(rabbits[current])
    current += 1
rabbit2.py
rabbits = ['Flopsy', 'Mopsy', 'Cottontail', 'Peter']
for rabbit in rabbits:
    print(rabbit)

期待するpylintのアウトプットは、

rabbit1.pyに関しては、
何か、pythonらしくないことを、直接、間接に指摘され、
rabbit2.pyに関しては、指摘なし。

でした。

しかし、現実は、
rabbit1.pyに対して、

rabbit1.py:1:0: C0111: Missing module docstring (missing-docstring)
rabbit1.py:1:0: C0103: Constant name "rabbits" doesn't conform to UPPER_CASE naming style (invalid-name)
rabbit1.py:2:0: C0103: Constant name "current" doesn't conform to UPPER_CASE naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 4.00/10 (previous run: 5.00/10, -1.00)

rabbit2.pyに対して、

rabbit2.py:1:0: C0111: Missing module docstring (missing-docstring
rabbit2.py:1:0: C0103: Constant name "rabbits" doesn't conform to UPPER_CASE naming style (invalid-name)
------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

という、本質的に同じ指摘。。。。

どうも

(出典: https://stackoverflow.com/questions/41204932/python-pylint-c0103invalid-constant-name )
とかによると、

As explained by Kundor, PEP 8 states that:
Constants are usually defined on a module level and written in all capital letters with underscores separating words.
The point is that "constants" in python don't really exist. Pylint, as per PEP 8, expects module level variables to be "constants."
That being said you've several options:
you don't want this "constant" thing, then change pylint's const-rgx regular expression to be the same as e.g. variable-rgx,
you may deactivate those warnings for this file, or even locally in the file, using # pylint: disable=invalid-name,
avoid module level variables, by wrapping them into a function.
In your case I would go with the third option, by creating a build_app function or something similar, that would return the application (and maybe the 'db' object as well but you have several choices there). Then you could add a salt of the second option to get something like:
app = build_app() # pylint: disable=invalid-name

らしい

if a is not b の件

PEP8において、
『式全体を否定(if not a is b)するのではなく、否定判定演算子(if a is not b)を使う。』
というのがある。

すばらしいことに、pylintでは、これがチェックできる。

チェックさせたコードは、以下。

kaijyu1kai.py
'pylint test'
ookisa = "small"
if not ookisa == "very large":
    print("kaijyu ya nai!!!")

チェック結果は、以下。

************* Module kaijyu1kai
kaijyu1kai.py:2:0: C0103: Constant name "ookisa" doesn't conform to UPPER_CASE naming style (invalid-name)
kaijyu1kai.py:3:3: C0113: Consider changing "not ookisa == 'very large'" to "ookisa != 'very large'" (unneeded-not)

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

Consider changing ・・・』 にて、指摘し、更に、直し方まで書いてある。

ただ、もともと、これを示そうと思ったモトモトのサンプルコードは、以下。

kaijyu1.py
'pylint test'
def check_kaijyu(ookisa):
    'do check'
    if not ookisa == "very large":
        print("kaijyu ya nai!!!")

check_kaijyu("small")

それに対するpylintの結果は、以下。

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

なんと、、、 10点満点で、、、『Consider changing ・・・』 が出ない!
なぜ??(『なぜ』終わりで、すみません。)

まとめ

Pylintがいきなり、constantsじゃない(ものに対してだ)けど、contantsという切り口で、厳しい、動作をした。
また、if not a is b の件は、すごいような、疑問が残るような。。。

それは、それとして、
なんらかの対処をして、pylintに馴染むのがいいと思う。

関連(本人)

pythonをストレスなく使う!(ExpressionとStatement)
pythonをストレスなく使う!(Pythonでは、すべてがオブジェクトとして実装されている)

今後

コメントなどあれば、お願いします。:candy:
このスピード感での習熟で、、、、いいレベルに届くかは、悲観的です。。。。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0