2018年1月6日 星期六

好書 : Python 專家實踐指南

周五去圖書館拿預約到館的 "Python 專家實踐指南" :

Source : 天瓏

稍微翻閱了一下, 才發現這不是一本程式技術的書, 而是 "如何寫好 Python" 的書, 例如怎樣撰寫符合 Python 常見風格的程式碼如 PEP8, PEP20 等; 如何測試與打包發布 Python 程式等等, 範圍其實很雜很大, 不只是 coding 而已, 真的是專家等級, 我自認還是屬於羽量級, 有很多還用不到.

在樹莓派中安裝 pep8 必須用 sudo 以系統管理員身分 :

pi@raspberrypi:~ $ sudo pip3 install pep8 
Downloading/unpacking pep8
  Downloading pep8-1.7.1-py2.py3-none-any.whl (41kB): 41kB downloaded
Installing collected packages: pep8
Successfully installed pep8
Cleaning up...

這樣就可以用 pep8 指令來檢查 Python 程式是否符合 PEP8 風格 :

pi@raspberrypi:~ $ pep8 reportip2.py   
/usr/local/lib/python3.4/dist-packages/pep8.py:2124: UserWarning:

pep8 has been renamed to pycodestyle (GitHub issue #466)
Use of the pep8 tool will be removed in a future release.
Please install and use `pycodestyle` instead.

原來 pep8 已有繼任者 pycodestyle, 那就用新的吧!

$ pip install pycodestyle  
$ pycodestyle ...

  '\n\n'
reportip2.py:2:1: E265 block comment should start with '# '
reportip2.py:6:1: E402 module level import not at top of file
reportip2.py:7:1: E402 module level import not at top of file
reportip2.py:8:1: E402 module level import not at top of file
reportip2.py:9:1: E402 module level import not at top of file
reportip2.py:10:1: E402 module level import not at top of file
reportip2.py:11:1: E402 module level import not at top of file
reportip2.py:12:1: E402 module level import not at top of file
reportip2.py:13:1: E402 module level import not at top of file
reportip2.py:14:1: E402 module level import not at top of file
reportip2.py:15:1: E402 module level import not at top of file
reportip2.py:23:33: E231 missing whitespace after ','
reportip2.py:29:1: E302 expected 2 blank lines, found 1
reportip2.py:33:25: E222 multiple spaces after operator
reportip2.py:34:31: E231 missing whitespace after ','
reportip2.py:34:38: E231 missing whitespace after ','
reportip2.py:48:25: E203 whitespace before ','
reportip2.py:49:12: E111 indentation is not a multiple of four
reportip2.py:50:12: E111 indentation is not a multiple of four
reportip2.py:51:12: E111 indentation is not a multiple of four
reportip2.py:54:1: E302 expected 2 blank lines, found 1
reportip2.py:54:18: W291 trailing whitespace
reportip2.py:56:25: E231 missing whitespace after ','
reportip2.py:56:30: W291 trailing whitespace
reportip2.py:57:11: E225 missing whitespace around operator
reportip2.py:61:1: E302 expected 2 blank lines, found 1
reportip2.py:78:5: E301 expected 1 blank line, found 0
reportip2.py:78:19: E231 missing whitespace after ','
reportip2.py:79:37: E231 missing whitespace after ','
reportip2.py:82:40: E231 missing whitespace after ','
reportip2.py:83:46: E231 missing whitespace after ','
reportip2.py:85:1: E302 expected 2 blank lines, found 1
reportip2.py:93:11: E225 missing whitespace around operator
reportip2.py:94:10: E225 missing whitespace around operator
reportip2.py:95:12: E225 missing whitespace around operator
reportip2.py:103:33: E231 missing whitespace after ','

原來 PEP8 有新版的模組稱為 pycodestyle :

pi@raspberrypi:~ $ sudo pip3 install pycodestyle   
Downloading/unpacking pycodestyle
  Downloading pycodestyle-2.3.1-py2.py3-none-any.whl (45kB): 45kB downloaded
Installing collected packages: pycodestyle
Successfully installed pycodestyle
Cleaning up...

pi@raspberrypi:~ $ pycodestyle reportip2.py   
reportip2.py:2:1: E265 block comment should start with '# '
reportip2.py:23:33: E231 missing whitespace after ','
reportip2.py:29:1: E302 expected 2 blank lines, found 1
reportip2.py:33:25: E222 multiple spaces after operator
reportip2.py:34:31: E231 missing whitespace after ','
reportip2.py:34:38: E231 missing whitespace after ','
reportip2.py:48:25: E203 whitespace before ','
reportip2.py:49:12: E111 indentation is not a multiple of four
reportip2.py:50:12: E111 indentation is not a multiple of four
reportip2.py:51:12: E111 indentation is not a multiple of four
reportip2.py:54:1: E302 expected 2 blank lines, found 1
reportip2.py:54:18: W291 trailing whitespace
reportip2.py:56:25: E231 missing whitespace after ','
reportip2.py:56:30: W291 trailing whitespace
reportip2.py:57:11: E225 missing whitespace around operator
reportip2.py:61:1: E302 expected 2 blank lines, found 1
reportip2.py:65:9: E722 do not use bare except'
reportip2.py:68:13: E722 do not use bare except'
reportip2.py:73:17: E722 do not use bare except'
reportip2.py:78:5: E301 expected 1 blank line, found 0
reportip2.py:78:19: E231 missing whitespace after ','
reportip2.py:79:37: E231 missing whitespace after ','
reportip2.py:82:40: E231 missing whitespace after ','
reportip2.py:83:46: E231 missing whitespace after ','
reportip2.py:85:1: E302 expected 2 blank lines, found 1
reportip2.py:93:11: E225 missing whitespace around operator
reportip2.py:94:10: E225 missing whitespace around operator
reportip2.py:95:12: E225 missing whitespace around operator
reportip2.py:103:33: E231 missing whitespace after ','

第六章介紹如何打包 (或凍結 freeze) Python 程式, 五種工具如下表 :

 Pyinstaller cx_Freeze py2app py2exe bbFreeze
 Python3 YES YES YES YES -
 Licence GPL PSF MIT MIT Zlib
 Windows YES YES - YES YES
 Linux YES YES - - YES
 OS X YES YES YES - -

其中 Pyinstaller 與 cx_Freeze 頗受好評. Pyinstaller 用法參考 :

將Python腳本打包成可執行文件

其安裝指令為

$ pip install pyinstaller

打包程式 :

$ pyinstaller script.py

這樣會產生一個 .spec 檔與 build (日誌), dist (可執行檔與相依函式庫) 兩個目錄, 接著需編輯 .spec 檔以自訂建構內容.

安裝 cx_Freeze  :

$ pip install cx_Freeze

建構腳本 :

$ cxfreeze-quickstart

打包 :

$ python setup.py build_exe  (Windows 可執行檔)
$ python setup.py build_msi  (Windows 可執行檔與相依檔案)
$ python setup.py build_rpm  (Linux)

另外在第十章資料處理舉出了 Python 常用的資料工具函式庫 :

  1. Numpy :
    提供多維陣列與線性代數工具, 速度最佳化, 是 SciPy 的一部分, 從 Scipy 中獨立出來.
  2. SciPy :
    提供工程與科學相關函式與工具, 例如線性代數, 信號處理, 積分, 求根, 與統計等.
  3. Matplotlib :
    提供科學繪圖函式庫.
  4. Pandas :
    提供可儲存, 合併, 分群, 迴歸, 索引, 視窗, 與子集的序列與 DataFrame 物件.
  5. Scikit-Learn :
    提供機器學習演算法.
  6. Nltk :
    提供自然語言工具組, 可用多種語言進行資料建模與訓練. 
  7. Rpy2 :
    提供 R 語言統計套件介面, 可從 Python 執行 R 語言, 並在兩個環境之間傳遞資料.
  8. CV2 (OpenCV) :
    提供電腦視覺即時影像分析函數庫, 例如人臉辨識演算法.
  9. Scikit-image :
    提供影像處理函式庫. 
原來 Python 還能透過 Rpy2 與 R 語言協同作業哩!

沒有留言:

張貼留言