PyCharm Debug 調試


一、實驗環境

1.Windows7x64_SP1

2.anaconda2.5.0 + python2.7(anaconda集成,不需單獨安裝)

3.pyinstaller3.0

二、調試代碼

get_config.py代碼如下

 

# -*- coding: utf-8 -*-
import os
from configparser import ConfigParser,NoSectionError

debug = False


def debug_print(str):
    if debug:
        print(str)


class GetConfig(ConfigParser):
    def __init__(self,defaults=None):
        """
        初始化函數
        :param defaults:
        """
        ConfigParser.__init__(self,defaults=None)

    def optionxform(self, optionstr):
        """
        解決ConfigParser將鍵值對強制轉換為小寫問題
        :param optionstr:
        :return:
        """
        return optionstr

    def open_file(self,file_name):
        """
        打開config文件夾
        :param file_name: config文件路徑
        :return:
        """
        if os.path.exists(file_name):
            debug_print('%s exists' %(file_name))
            self.read(file_name)
            return True
        else:
            debug_print('%s do not exists' % (file_name))
            return False

    def get_sections(self):
        """
        獲取config中所有section
        :return: section列表
        """
        section = self.sections()
        debug_print(section)
        return section

    def check_section(self,*args,**kwargs):
        def check_section_type(*args,**kwargs):
            if isinstance(args[0], str):
                print('The first parameter (section) must be a string')
                return
        return check_section_type

    @check_section
    def get_options(self,section):
        """
        返回section中所有option
        :param section: 指定section
        :return: option列表
        """
        try:
            options_list = self.options(section)
            return options_list
        except NoSectionError as e:
            debug_print(e)
            return

    @check_section
    def get_dicts(self,section):
        """
        返回section中鍵值對
        :param section: 指定section
        :return: 鍵值對
        """
        try:
            list_str = self.items(section)
            dict_str = dict(list_str)
            return dict_str
        except NoSectionError as e:
            debug_print(e)
            return

    @check_section
    def get_options_match_str(self,section,match_str):
        """
        使用指定字符串匹配option列表
        :param section: 指定section
        :param match_str: 需要匹配的字符串
        :return: 匹配列表
        """
        options_list = self.get_options(section)
        match_list = []
        for each in options_list:
            if match_str in each:
                match_list.append(each)
        return match_list

    @check_section
    def get_para(self,section,key):
        """
        使用section和key索引value
        :param section: 指定section
        :param key: 指定key
        :return: 索引值
        """
        dicts = self.get_dicts(section)
        if dicts:
            result = dicts.get(key,None)
            return result
        else:
            return


if __name__ == '__main__':
    debug = True
    config = GetConfig()
    config.open_file('g:\gejunping\extract_bin\Tool\config_block.cfg')
    sections = config.get_sections()
    ret = config.get_para('AM620','AM620_128G')
    print(ret)
    options = config.get_options('AM620')
    print(options)
    match_list = config.get_options_match_str('AM620','PREFIX')
    print(match_list)
    for each_match in match_list:
        print(config.get_para('AM620',each_match))
    dicts = config.get_dicts('AM620')
    print(dicts)

  

config_block.cfg內容如下

[AM620]
AM620_128G = 16000
AM620_256G = 16000
AM620_512G = 16000
AM620_1TB = 16000

[AH640]
AH640_128G = 16000
AH640_256G = 16000
AH640_512G = 16000
AH640_1T = 16000

三、實驗步驟

1、設置斷點、取消斷點

  • 行號之后、左側邊界之前單擊,設置斷點
  • 行號之后、左側邊界之前再次單擊,取消斷點

 

 

 2、進入調試界面

  • 代碼區域內,右擊鼠標,選擇其中的Debug ‘xx.py’文件

  • 使用快捷鍵Alt + shift + F9,選擇需要debug的腳本

 

 

 3、從斷點處單步執行 F8

  • Debug運行后,調試器停止在第一個斷點處,每按一次F8,程序單步執行一次。
  • pycharm在代碼后自動添加變量值,如下圖黃色框框,格式為:變量名:變量類型:變量值

 

 

 4、進入函數內部 F7

  • 多次按F8,調試器運行至ret = config.get_para()函數,如下圖黃色框框

  • 此時按F7,調試器將進入get_para函數,調試器會打印調用該函數的參數值,如下圖黃色框框

 

 5、只在斷點處執行 F9

  程序中設置三處斷點,調試器運行后停留在第一個斷點處,按一次F9跳至第二個斷點,再按一次F9跳至第三個斷點。

 

 綜上所述,pycharm調試功能及其強大,可以拋棄在程序中不停添加print語句的做法!

 

 

 

 

 

  


免責聲明!

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



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