前言 ........................................................................................... xix
第1 部分 起步
第1 章 選擇一個解釋器 ........................................................................ 3
Python 2 與Python 3 的狀況對比 ..........................................................................3
建議 .......................................................................................................................3
那就選擇Python 3 嗎 .............................................................................................4
Python 的不同實現 ................................................................................................4
CPython ...........................................................................................................5
Stackless ..........................................................................................................5
PyPy ................................................................................................................5
Jython ..............................................................................................................5
IronPython .......................................................................................................6
PythonNet ........................................................................................................6
Skulpt ..............................................................................................................6
MicroPython ....................................................................................................7
第2 章 恰當地安裝Python ................................................................... 9
在Mac OS X 上安裝Python ..................................................................................9
Setuptools 和pip ........................................................................................... 11
virtualenv ...................................................................................................... 11
在Linux 上安裝Python .......................................................................................12
Setuptools 和pip ...........................................................................................12
開發(fā)工具 .......................................................................................................13
virtualenv ......................................................................................................14
在Windows 上安裝Python ..................................................................................15
Setuptools 和pip ...........................................................................................17
virtualenv ......................................................................................................17
商業(yè)化Python 二次發(fā)行版 ..................................................................................18
第3 章 搭建開發(fā)環(huán)境 ......................................................................... 21
文本編輯器 ..........................................................................................................21
Sublime Text ..................................................................................................22
Vim ................................................................................................................23
Emacs ............................................................................................................25
TextMate........................................................................................................26
Atom .............................................................................................................26
Code ..............................................................................................................26
IDE.......................................................................................................................27
PyCharm/IntelliJ IDEA ..................................................................................28
Aptana Studio 3/Eclipse+LiClipse+PyDev .....................................................29
WingIDE........................................................................................................29
Spyder ...........................................................................................................30
NINJA-IDE ....................................................................................................30
Komodo IDE..................................................................................................30
Eric(Eric Python IDE) .................................................................................31
Visual Studio .................................................................................................31
增強型交互式工具 ...............................................................................................32
IDLE .............................................................................................................32
IPython ..........................................................................................................32
bpython ..........................................................................................................33
環(huán)境隔離工具 ......................................................................................................33
虛擬環(huán)境 .......................................................................................................33
pyenv .............................................................................................................35
Autoenv .........................................................................................................36
virtualenvwrapper ..........................................................................................36
Buildout .........................................................................................................37
Conda ............................................................................................................38
Docker ...........................................................................................................39
第2 部分 步入正題
第4 章 編寫高質量的代碼 ................................................................... 43
代碼風格 ..............................................................................................................43
PEP 8 .............................................................................................................43
PEP 20(又名Python 之禪) ..........................................................................44
一般性建議 ...................................................................................................45
約定 ...............................................................................................................52
習語 ...............................................................................................................55
常見陷阱 .......................................................................................................58
組織好項目的結構 ...............................................................................................61
模塊 ...............................................................................................................61
包 ..................................................................................................................65
面向對象編程 ................................................................................................66
裝飾器 ...........................................................................................................67
動態(tài)類型 .......................................................................................................68
可變類型和不可變類型 .................................................................................69
管理依賴 .......................................................................................................71
測試代碼 ..............................................................................................................72
測試的基礎知識 ............................................................................................73
舉例說明 .......................................................................................................76
其他流行工具 ................................................................................................80
文檔 .....................................................................................................................82
項目文檔 .......................................................................................................82
項目配套發(fā)行文檔 ........................................................................................83
文檔字符串與塊注釋 .....................................................................................84
日志 .....................................................................................................................84
在庫中使用logging .......................................................................................85
在應用中使用logging ...................................................................................86
選擇許可證 ..........................................................................................................88
上游許可證 ...................................................................................................88
許可證選項 ...................................................................................................89
軟件許可相關的學習資源 .............................................................................90
第5 章 閱讀高質量的代碼 ................................................................... 91
共同特征 ..............................................................................................................92
HowDoI ................................................................................................................92
閱讀單文件腳本 ............................................................................................93
取自HowDoI 的結構示例 .............................................................................96
取自HowDoI 的風格示例 .............................................................................97
Diamond ...............................................................................................................99
閱讀一個更大的應用程序 .............................................................................99
取自Diamond 的結構示例 .......................................................................... 105
取自Diamond 的風格示例 .......................................................................... 109
Tablib ................................................................................................................. 111
閱讀一個小型庫 .......................................................................................... 112
取自Tablib 的結構示例 .............................................................................. 115
取自Tablib 的風格示例 .............................................................................. 123
Requests ............................................................................................................. 126
閱讀一個更大的庫 ...................................................................................... 126
取自Requests 的結構示例 .......................................................................... 130
取自Requests 的風格示例 .......................................................................... 134
Werkzeug ........................................................................................................... 139
閱讀一個工具包的代碼 ............................................................................... 140
取自Werkzeug 的風格示例 ......................................................................... 148
取自Werkzeug 的結構示例 ......................................................................... 149
Flask................................................................................................................... 156
閱讀一個框架的代碼 ................................................................................... 156
取自Flask 的風格示例 ................................................................................ 163
取自Flask 的結構示例 ................................................................................ 164
第6 章 交付高質量的代碼 ................................................................. 169
有用的詞匯和概念 ............................................................................................. 170
打包你的代碼 .................................................................................................... 171
Conda .......................................................................................................... 171
PyPI ............................................................................................................. 171
凍結你的代碼 .................................................................................................... 174
PyInstaller ................................................................................................... 176
cx_Freeze .................................................................................................... 178
py2app ......................................................................................................... 179
py2exe ......................................................................................................... 180
bbFreeze ...................................................................................................... 181
Linux 已構建分發(fā)包的打包技術 ........................................................................ 181
可執(zhí)行的ZIP 文件 ............................................................................................. 183
第3 部分 場景化指南
第7 章 用戶交互 .............................................................................. 187
Jupyter Notebooks 項目 ...................................................................................... 187
命令行應用 ........................................................................................................ 188
圖形化用戶界面應用 ......................................................................................... 196
窗口部件庫 ................................................................................................. 196
游戲開發(fā) ..................................................................................................... 202
Web 應用 ............................................................................................................ 203
Web 框架/ 微框架 ....................................................................................... 203
Web 模板引擎 ............................................................................................. 206
Web 部署 ..................................................................................................... 212
第8 章 代碼管理和改進 .................................................................... 215
持續(xù)集成 ............................................................................................................ 215
系統(tǒng)管理 ............................................................................................................ 216
服務器自動化 .............................................................................................. 218
系統(tǒng)和任務監(jiān)控 .......................................................................................... 222
加速 ................................................................................................................... 225
與C/C++/FORTRAN 庫進行交互 ............................................................... 235
第9 章 軟件接口 .............................................................................. 239
Web 客戶端庫 .................................................................................................... 240
Web API ...................................................................................................... 240
數據序列化 ........................................................................................................ 245
分布式系統(tǒng) ........................................................................................................ 248
網絡編程 ..................................................................................................... 248
密碼技術 ............................................................................................................ 254
第10 章 數據操作 ............................................................................ 261
科學應用 ............................................................................................................ 262
文本操作和文本挖掘 ......................................................................................... 266
Python 標準庫中的字符串工具 ................................................................... 266
圖像操作 ..................................................................................................... 269
第11 章 數據持久化 ......................................................................... 273
結構化文件 ........................................................................................................ 273
數據庫接口庫 .................................................................................................... 274
附錄A 補充說明 ............................................................................... 289