learn python 2 - real world case

47
Learn Python in 30 min - 2 cmj 1

Upload: chia-hao-tsai

Post on 13-Aug-2015

231 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Learn python 2 - Real World Case

Learn Python in 30 min - 2cmj

1

Page 2: Learn python 2 - Real World Case

2

Page 3: Learn python 2 - Real World Case

真實的世界

‧ 你需要會:

‧ 複雜的操作邏輯

‧ 很有機會讀/寫檔案

‧ 還可能執行外部指令

‧ 或許還來個操作系統

3

Page 4: Learn python 2 - Real World Case

真實的世界

‧ 你需要會:

‧ 複雜的操作邏輯

‧ 很有機會讀/寫檔案

‧ 還可能執行外部指令

‧ 或許還來個操作系統 下次會提到!

4

Page 5: Learn python 2 - Real World Case

來點會用到的狀況∼

5

Page 6: Learn python 2 - Real World Case

1. 跑你要的程式 (e.g. 測試某個功能)

1. 控制系統,執行某個邏輯

2. 回傳結果

2. 記錄結果

1. 決定測試記錄的格式,開檔案

2. 記錄結果,關檔案

6

Page 7: Learn python 2 - Real World Case

‧ 來點真實的 case :確定版號

‧ 思考一下:取得系統/程式版本

‧ 思考一下:記錄當下的版本

7

Page 8: Learn python 2 - Real World Case

‧ 來點真實的 case :確定版號

‧ 思考一下:取得系統/程式版本

‧ 執行外部指令:uname -a

‧ 思考一下:記錄當下的版本

8

Page 9: Learn python 2 - Real World Case

‧ 來點真實的 case :確定版號

‧ 思考一下:取得系統/程式版本

‧ 執行外部指令:uname -a

‧ 思考一下:記錄當下的版本

‧ 處理字串,萃取版本資訊

9

Page 10: Learn python 2 - Real World Case

執行外部指令:需要外部函式庫

10

Page 11: Learn python 2 - Real World Case

11

Page 12: Learn python 2 - Real World Case

‧ import os

‧ 引用外部函式庫

‧ os 函式庫提供的所有功能,現在可以用了∼

‧ 理論上,可以用 popen 執行所有外部指令

12

Page 13: Learn python 2 - Real World Case

‧ 根據不同的平台,當然會得到不同的結果

‧ uname 是平台相關 (POSIX-Like command)

‧ -a 表示顯示所有資訊

‧ 你需要處理這個字串

13

Page 14: Learn python 2 - Real World Case

14

Page 15: Learn python 2 - Real World Case

‧ 字串處理常用的

‧ split - 分割

‧ find - 找第一個子字串的起始位子

‧ count - 尋找子字串出現的次數

‧ join - 將多個字串合成一個

15

Page 16: Learn python 2 - Real World Case

這些都比不上...

16

Page 17: Learn python 2 - Real World Case

‧ 字串/串列的基本操作

‧ LIST[idx] - 第 idx 個元素

‧ LIST[-idx] - 反向第 idx 個元素

‧ LIST[Start:End] - 從 Start 到 End-1 的子XX

‧ LIST[S:E:Step] - 從 S 到 E-1 每 Step 的子XX

17

Page 18: Learn python 2 - Real World Case

18

Page 19: Learn python 2 - Real World Case

Q:誰會看著程式,一邊跑一邊記錄哪個步驟錯誤

19

Page 20: Learn python 2 - Real World Case

記在檔案不是很棒嗎

20

Page 21: Learn python 2 - Real World Case

‧ 如果可以把所有測試結果記錄下來

‧ 完整的測試結果

‧ 只有沒有成功的測試

‧ 測試失敗的詳細步驟

‧ 這樣不是很棒嗎∼

21

Page 22: Learn python 2 - Real World Case

‧ 如果可以把所有測試結果記錄下來

‧ 完整的測試結果

‧ 只有沒有成功的測試

‧ 測試失敗的詳細步驟

‧ 這樣不是很棒嗎∼

‧ 還可以跟隔壁的 泡茶聊天 討論公事

22

Page 23: Learn python 2 - Real World Case

23

Page 24: Learn python 2 - Real World Case

‧ 簡單一點:要不讀檔、要不寫檔

‧ 看你 open 帶的參數:預設是讀檔

‧ 根據你開檔的方式:要不 read、要不 write

‧ 死都要記得關檔

24

Page 25: Learn python 2 - Real World Case

‧ 簡單一點:要不讀檔、要不寫檔

‧ 看你 open 帶的參數:預設是讀檔

‧ 根據你開檔的方式:要不 read、要不 write

‧ 死都要記得關檔:我會忘記怎麼辦!

25

Page 26: Learn python 2 - Real World Case

26

Page 27: Learn python 2 - Real World Case

‧ 關鍵字 with

‧ 把開檔的 fd 放在 as 之後

‧ 跟 function 一樣有 scope

‧ 在 scope 結束後幫你自動關檔

27

Page 28: Learn python 2 - Real World Case

所以合併兩個 case

28

Page 29: Learn python 2 - Real World Case

29

Page 30: Learn python 2 - Real World Case

回到類別

30

Page 31: Learn python 2 - Real World Case

‧ 如果把一切變成類別/函式庫

‧ 你只需要拼命的加額外的邏輯

‧ 不需要管

‧ 流程紀錄

‧ 執行參數

31

Page 32: Learn python 2 - Real World Case

套用到一開始的例子∼

32

Page 33: Learn python 2 - Real World Case

‧ 設計一個框架

‧ 執行不同的測試邏輯,紀錄測試結果

‧ 根據測試邏輯,執行不同的參數

‧ 需要增加測試 == 增加一個新的 method

33

Page 34: Learn python 2 - Real World Case

框架

34

Page 35: Learn python 2 - Real World Case

35

決定測試項⽬目

Page 36: Learn python 2 - Real World Case

36

執⾏行:紀錄

Page 37: Learn python 2 - Real World Case

測試邏輯

37

Page 38: Learn python 2 - Real World Case

38

單項測試

Page 39: Learn python 2 - Real World Case

39

多項測試

Page 40: Learn python 2 - Real World Case

40

控制測試項⽬目

Page 41: Learn python 2 - Real World Case

41

抽象的測試邏輯

Page 42: Learn python 2 - Real World Case

看 log 的時間

42

Page 43: Learn python 2 - Real World Case

不免俗的,回家作業

43

Page 44: Learn python 2 - Real World Case

回家作業

‧ 熟讀 string 可以用的技巧可以幫你處理字串

‧ 替換所有特定字元

‧ 我不要第3~5行

‧ 每一行開頭都幫我加上 #

‧ ⋯ etc

44

Page 45: Learn python 2 - Real World Case

處理 ps 指令

‧ 寫一個完整的程式,可以用 Python 執行

‧ 最後的結果輸出成檔案:result

‧ 有多少 非 root 身份 的 process

‧ 最大 PID 的 process 名稱

‧ 如果 result 已經存在

‧ 把舊的 result 變成 result.old

‧ 原本的 result.old 就 忘了 刪了它吧

45

Page 46: Learn python 2 - Real World Case

進階:互動程式

‧ 簡單的計算機程式

‧ 接受外面 User 輸入得值,送進 python eval 裡

‧ e.g. User 輸入 1+2 你就執行 eval(“1+2”)

‧ 如果 User 亂打,你需要處理例外狀況

‧ 1/0 => 告訴他:你數學沒學好

‧ a+b => 告訴他:這不是數學運算

46

Page 47: Learn python 2 - Real World Case

Thanks for your attention~

47