Ipython的 Debug 函數

  一直以來,很喜歡方便而強大的ipython調試python,但始終沒有機會仔細看高版本的手冊,今天偶然在這裏 看到一個調試單獨函數的小技巧,順便瀏覽了下ipython主站的文檔,記錄以下。   一般我有個函數要調試,我會在想要的位置插入一個 ipdb的斷點。python

def oauth():
    '''
        測試 oauth 功能
    '''

    import ipdb; ipdb.set_trace()
    
    appid = u"abc"
    r_url = u"http://www.baidu.com/"
    scope = u"snsapi_base"
    url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s'
    'APPID&redirect_uri=%s&response_type=code&scope=%s&'
    'state=STATE#wechat_redirect' % (appid, r_url, scope)如

  能夠經過設置一個自定義的函數api

def debug(f, *args, **kwargs):  
    from IPython.core.debugger import Pdb  
    pdb = Pdb(color_scheme='Linux')  
    return pdb.runcall(f, *args, **kwargs)

  實現 debug(oauth) 的效果app

1.創建一個py文件

$cd `ipython locate profile`/startup/

創建一個tool.py文件,內容以下:函數

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

def set_trace():
    from IPython.core.debugger import Pdb
    Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)

def debug(f, *args, **kwargs):
    from IPython.core.debugger import Pdb
    pdb = Pdb(color_scheme='Linux')
    return pdb.runcall(f, *args, **kwargs)

簡單解釋一下測試

  • ipython locate profile 命令可以返回 ipython 的配置目錄
  • 其下的startup目錄下的文件會在 ipython啓動前運行。
相關文章
相關標籤/搜索