簡說Python之ipython的pdb調試



系統環境: Ubuntu 18.04.1 LTS

Python使用的是虛擬環境:virutalenv

Python的版本:Python 3.6.9

簡說Python之ipython

1.安裝ipython

pip install ipython

2.ipython的使用

In [1]: run utils.py                                                                          

In [2]: cat utils.py                                                                          
# coding=utf-8
import os
import hashlib
from functools import partial

from config import UPLOAD_FOLDER

HERE = os.path.abspath(os.path.dirname(__file__))


def get_file_md5(f, chunk_size=8192):
    h = hashlib.md5()
    while True:
        chunk = f.read(chunk_size)
        if not chunk:
            break
        h.update(chunk)
    return h.hexdigest()


def humanize_bytes(bytesize, precision=2):
    abbrevs = (
        (1 << 50, 'PB'),
        (1 << 40, 'TB'),
        (1 << 30, 'GB'),
        (1 << 20, 'MB'),
        (1 << 10, 'kB'),
        (1, 'bytes')
    )
    if bytesize == 1:
        return '1 byte'
    for factor, suffix in abbrevs:
        if bytesize >= factor:
            break
    return '%.*f %s' % (precision, bytesize / factor, suffix)


get_file_path = partial(os.path.join, HERE, UPLOAD_FOLDER)

In [3]: humanize_bytes(100)                                                                   
Out[3]: '100.00 bytes'

In [4]: humanize_bytes(430004)                                                                
Out[4]: '419.93 kB'

In [5]: humanize_bytes(430004342342)                                                          
Out[5]: '400.47 GB'

這樣可以不啟動所有的py程序,直接測試某一個函數的功能。通過輸入參數,得到輸出。

函數的性質,把它看成一個黑盒子。測試它的輸入輸出。

humanize_bytes通過測試可以知道,它就是格式化數字,統計為多少字節,KB,MB,GB等。

3.ipython的debug調試。

python寫了一個簡單的遞歸程序

(zsdpy1) $ vi recursive.py 
# coding=utf-8
def recu(n):
    print ("level 1 value :",n);
    if(n>2):
        recu(n-1)
    print ("--level 2 value :",n);
recu(5)

進入調試

In [4]: run -d recursive.py  

      1 # coding=utf-8
----> 2 def recu(n):
      3     print ("level 1 value :",n);
      4     if(n>2):
      5         recu(n-1)
  • b設置斷點命令,進入調試模式輸入b xx(xx為行數)。
  • n命令單步執行,不會進入函數內部
  • s命令單步執行,會進入函數內部
  • a是打印出當前函數的參數值
  • j 是跳轉到某一行執行有點類似b和c指令結合,具體使用j xx(xx為行數)
  • q,退出pdb調試模式

這里我們輸入s命令,單步調試,看看這個遞歸程序是如何運行的。

ipdb> s                                                                                   
## 直接跳入到了recu(5)的過程    
> /home/zsd/web_develop/recursive.py(8)<module>()
      4     if(n>2):
      5         recu(n-1)
      6     print ("--level 2 value :",n);
      7 
----> 8 recu(5)

ipdb> s                                                                                   
## 運行recu(n)的函數
> /home/zsd/web_develop/recursive.py(2)recu()
      1 # coding=utf-8
----> 2 def recu(n):
      3     print ("level 1 value :",n);
      4     if(n>2):
      5         recu(n-1)
## 輸出函數值5    
> /home/zsd/web_develop/recursive.py(3)recu()
      1 # coding=utf-8
      2 def recu(n):
----> 3     print ("level 1 value :",n);
      4     if(n>2):
      5         recu(n-1)

ipdb> s                                                                                       
level 1 value : 5
> /home/zsd/web_develop/recursive.py(4)recu()
      2 def recu(n):
      3     print ("level 1 value :",n);
----> 4     if(n>2):
      5         recu(n-1)
      6     print ("--level 2 value :",n);
然后就會一直循環,到2的時候,在循環出來。有興趣的可以s繼續單步調試

其輸出結果是:

level 1 value : 5
level 1 value : 4
level 1 value : 3
level 1 value : 2
--level 2 value : 2
--level 2 value : 3
--level 2 value : 4
--level 2 value : 5


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM