25 พฤศจิกายน 2557

Published พฤศจิกายน 25, 2557 by with 0 comment

ตรวจสอบคุณภาพโค้ด Python ด้วย Pylint

Pylint เป็นเครื่องมือหนึ่งใน Python ใช้สำหรับตรวจสอบคุณภาพและประสิทธิภาพของโค้ด Python ที่เขียน ว่ามีคุณภาพอยู่ในระดับใด โดยอ้างอิงตามหลัก PEP 8 ของ Python  ซึ่งเป็นหลักที่เกี่ยวข้องกับรูปแบบของ Python Code และมีการให้คะแนนการทำงานของโค้ด Python อีกด้วยครับ
เครื่องมือ Pylint นี้ รองรับทั้ง Python 2 และ Python 3 สามาติดตั้งได้ง่าย ๆ โดยใช้ pip ใช้คำสั่งต่อไปนี้
pip install pylint
การใช้งาน ทำได้ง่าย ๆ แค่ใช้คำสั่งบนคอมมานด์ไลน์
pylint ไฟล์โค้ด.py
ตัวอย่างการตรวจสอบโค้ด Python ด้วย Pylint
ผมใช้โค้ดจากบทความ จัดการและประมวลผลรูปภาพกราฟิกใน Python ด้วย Pillow มาลองตรวจสอบคุณภาพโค้ดครับ
โค้ด 1.py
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
img = Image.open("60_1.jpg") #ดึงรูปภาพเข้ามา
draw = ImageDraw.Draw(img)
#ดึงไฟล์ Font เข้ามาและกำหนดขนาด
font = ImageFont.truetype("CSPraKasFD.otf", 50)
#ใส่ข้อความ โดยมีรูปแบบการใช้งานดังนี้ draw.text((x, y),"Sample Text",(r,g,b))
draw.text((10, 10), "Hello!",font=font)
#บันทึกรูปภาพเป็น sample-out.jpg
img.save('sample-out.jpg')

ใช้คำสั่ง
pylint 1.py
ผลลัพธ์
D:\งาน python3\>pylint 1.py
No config file found, using default configuration
************* Module 1
C:  8, 0: Exactly one space required after comma
draw.text((10, 10), "Hello!",font=font)
                            ^ (bad-whitespace)
C: 11, 0: Final newline missing (missing-final-newline)
C:  1, 0: Invalid module name "1" (invalid-name)
C:  1, 0: Missing module docstring (missing-docstring)
C:  4, 0: Invalid constant name "img" (invalid-name)
C:  5, 0: Invalid constant name "draw" (invalid-name)
C:  7, 0: Invalid constant name "font" (invalid-name)


Report
======
9 statements analysed.

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

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



External dependencies
---------------------
::

    PIL
      \-Image (1)
      \-ImageDraw (1)
      \-ImageFont (1)



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

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |9      |81.82 |11       |-2.00      |
+----------+-------+------+---------+-----------+
|docstring |0      |0.00  |0        |=          |
+----------+-------+------+---------+-----------+
|comment   |1      |9.09  |0        |+1.00      |
+----------+-------+------+---------+-----------+
|empty     |1      |9.09  |1        |=          |
+----------+-------+------+---------+-----------+



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

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



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

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |7      |3        |+4.00      |
+-----------+-------+---------+-----------+
|refactor   |0      |0        |=          |
+-----------+-------+---------+-----------+
|warning    |0      |116      |-116.00    |
+-----------+-------+---------+-----------+
|error      |0      |0        |=          |
+-----------+-------+---------+-----------+



Messages
--------

+----------------------+------------+
|message id            |occurrences |
+======================+============+
|invalid-name          |4           |
+----------------------+------------+
|missing-final-newline |1           |
+----------------------+------------+
|missing-docstring     |1           |
+----------------------+------------+
|bad-whitespace        |1           |
+----------------------+------------+



Global evaluation
-----------------
Your code has been rated at 2.22/10 (previous run: -98.18/10, +100.40)
ถือว่าโค้ดที่ผมเขียน ถ้าต้องการประสิทธิภาพสูง ๆ ต้องนำไปปรับปรุงตามคำแนะนำของ Pylint ครับ
ติดตามบทความต่อไปนะครับ
ขอบคุณครับ

0 ความคิดเห็น:

แสดงความคิดเห็น

แสดงความคิดเห็นได้ครับ :)