一、实验环境
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语句的做法!