Conventions และ PEP 8

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Adam Spannbauer

Machine Learning Engineer at Eastman

Conventions คืออะไร?

การชนกำปั้น

การโค้งคำนับ

การจับมือ

โลโก้ Python

หลักการวิศวกรรมซอฟต์แวร์ใน Python

แนะนำ PEP 8

PEP 8

"โค้ดถูกอ่านบ่อยกว่าถูกเขียนมาก"

หลักการวิศวกรรมซอฟต์แวร์ใน Python

การละเมิด PEP 8

#define our data
my_dict ={
    'a'  : 10,
'b': 3,
    'c'  :   4,
             'd': 7}
#import needed package
import numpy as np
#helper function
def DictToArray(d):
    """Convert dictionary values to numpy array"""
    #extract values and convert
              x=np.array(d.values())
              return x
print(DictToArray(my_dict))
array([10,  4,  3,  7])
หลักการวิศวกรรมซอฟต์แวร์ใน Python

การทำตาม PEP 8

# Import needed package
import numpy as np

# Define our data
my_dict = {'a': 10, 'b': 3, 'c': 4, 'd': 7}


# Helper function
def dict_to_array(d):
    """Convert dictionary values to numpy array"""
    # Extract values and convert
    x = np.array(d.values())
    return x


print(dict_to_array(my_dict))
array([10,  4,  3,  7])
หลักการวิศวกรรมซอฟต์แวร์ใน Python

เครื่องมือสำหรับ PEP 8

PEP 8 กับ PyCharm

แพ็กเกจ pycodestyle

หลักการวิศวกรรมซอฟต์แวร์ใน Python

การใช้งาน pycodestyle

datacamp@server:~$ pip install pycodestyle
datacamp@server:~$ pycodestyle dict_to_array.py
dict_to_array.py:5:9: E203 whitespace before ':'
dict_to_array.py:6:14: E131 continuation line unaligned for hanging indent
dict_to_array.py:8:1: E265 block comment should start with '# '
dict_to_array.py:9:1: E402 module level import not at top of file
dict_to_array.py:11:1: E302 expected 2 blank lines, found 0
dict_to_array.py:13:15: E111 indentation is not a multiple of four
หลักการวิศวกรรมซอฟต์แวร์ใน Python

ผลลัพธ์จาก pycodestyle

ผลลัพธ์ข้อผิดพลาดจาก pycodestyle

หลักการวิศวกรรมซอฟต์แวร์ใน Python

มาฝึกกันเถอะ!

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Preparing Video For Download...