第17课 调试程序
一、调试程序
1、为什么要调试
1)程序往往不像我们设想的那样运行
2)调试可以定位问题的根源所在,从而解决问题。
2、调试方法
1)断点+跟踪,举例如下:
''' 现有一个数据库记录文件(见附件0005_1.txt),保存了学生课程签到的数据库记录。 内容格式如下 , ('2017-03-13 11:50:09', 271, 131), ('2017-03-14 10:52:19', 273, 131), ('2017-03-13 11:50:19', 271, 126), 每一行记录保存了学生的一次签到信息。 每一次签到信息的记录,分为三个部分, 分别是签到时间、签到课程的id号、签到学生的id号 要求大家实现下面的函数。其中参数fileName 为 数据库记录文件路径, 输出结果是将数据库记录文件中的学生签到信息保存在一个字典对象中,并作为返回值返回。 def putInfoToDict(fileName): 要求返回的字典对象的格式是这样的: key 是各个学生的id号, value是 该学生的签到信息 其中value,里面保存着该学生所有签到的信息 其中每个签到的信息是字典对象,有两个元素: key 是lessonid的 记录课程id,key是checkintime的 记录签到时间 比如,对于上面的示例中的3条记录,相应的返回结果如下: { 131: [ {'lessonid': 271,'checkintime':'2017-03-13 11:50:09'}, {'lessonid': 273,'checkintime':'2017-03-14 10:52:19'}, ], 126: [ {'lessonid': 271,'checkintime':'2017-03-13 11:50:19'}, ], }
需求分解:
1、学生课程签到信息
1)时间、课程ID、学生ID
2)格式:有特定要求
2、要求:
1)定义一个函数:def putInfoToDict(fileName)
2)return ----dict
3)格式输出:
{
学生ID:
[
{课程ID:XXXXX, 签到时间:XXXX}
{课程ID:XXXX, 签到时间:XXXX}
]
}
思路分解
1、获取信息
1)1、userId 2、lessonId 3、checkTime:从数据库文件获取
读取文件:'/' '\\' r' '
with open('fileDir') as rFile == open ('fileDir')
2)lines = rFile.read().splitlines()---读取每行内容
3)组织dict,格式打印
a、from pprint import pprint----美化打印
pprint(filedir)
2、debug知识点汇总
1)打完断点用debug模式运行
2)断点停留的行,并没有执行
3)如果需要直接从第一个断点跳转到第二个断点,方法如下:
a、当程序停在第一个断点的时候,点击debug按钮
b、然后点击菜单栏的Run—>Resume Programs,直接跳转到第二个断点
4)在调试的时候,经常遇到以下几个按钮,这里给出说明
a、Step Over:在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个执行完再停止,也就是把子函数整个作为一步。有一点,经过我们简单的调试,在不存在子函数的情况下是和Step Into效果一样的(简而言之,越过子函数,但子函数会执行)。
b、Step Into:单步执行,遇到子函数就进入并且继续单步执行(简而言之,进入子函数)。
c、Step Into My Code:进入自己编写的函数,不进入系统函数,很少用到。
d、Force Step Into:强制进入,在调试的时候能进入任何方法
e、Step Out:当单步执行到子函数内时,用Step Out就可以执行完子函数余下部分,并返回到上一层函数。
f、Run to Cursor:一直执行,到光标处停止,用在循环内部时,点击一次就执行一个循环。
作者:polyhedronx
来源:CSDN
原文:https://blog.csdn.net/polyhedronx/article/details/81514437
g、 重新进行debug
h、viewbreakpoints:显示所有断点
并且可以选择或者取消
参考代码如下:
import pprint fileDir = '0005_1.txt' def putInfoToDict(fileName): outInfoDict = {} with open(fileDir) as rFile: lines = rFile.read().splitlines() for line in lines: line = line.replace('(', '').replace(')', '').replace("'", '').replace('\t', '').replace(';', '') temp = line.split(',') userId = int(temp[2].strip()) lessonId = int(temp[1].strip()) checkTime = temp[0].strip() toDict = {'lessonid': lessonId, 'checktime': checkTime} if userId not in outInfoDict: # 如果userId(key)没在这个字典中 outInfoDict[userId] = [] # 则先添加这个key的value为空列表 outInfoDict[userId].append(toDict) # 然后再添加key和value到outInfoDict return outInfoDict pprint.pprint(putInfoToDict(fileDir))
执行结果如下:
{34: [{'checktime': '2017-03-13 12:00:18', 'lessonid': 271}], 35: [{'checktime': '2017-03-13 11:52:07', 'lessonid': 271}, {'checktime': '2017-03-14 00:53:26', 'lessonid': 272}, {'checktime': '2017-03-14 11:50:59', 'lessonid': 273}], 37: [{'checktime': '2017-03-13 11:52:44', 'lessonid': 271}, {'checktime': '2017-03-14 11:56:43', 'lessonid': 273}, {'checktime': '2017-03-16 00:53:36', 'lessonid': 274}], 38: [{'checktime': '2017-03-13 11:51:48', 'lessonid': 271}, {'checktime': '2017-03-14 00:50:07', 'lessonid': 272}, {'checktime': '2017-03-14 11:51:54', 'lessonid': 273}, {'checktime': '2017-03-16 00:50:06', 'lessonid': 274}], 39: [{'checktime': '2017-03-14 12:06:28', 'lessonid': 273}], 40: [{'checktime': '2017-03-13 11:52:01', 'lessonid': 271}, {'checktime': '2017-03-14 12:06:04', 'lessonid': 273}], 43: [{'checktime': '2017-03-13 11:52:46', 'lessonid': 271}, {'checktime': '2017-03-14 11:51:00', 'lessonid': 273}], 45: [{'checktime': '2017-03-13 12:02:27', 'lessonid': 271}, {'checktime': '2017-03-14 00:55:49', 'lessonid': 272}, {'checktime': '2017-03-14 11:58:01', 'lessonid': 273}], 46: [{'checktime': '2017-03-13 11:59:18', 'lessonid': 271}, {'checktime': '2017-03-14 11:57:26', 'lessonid': 273}], 51: [{'checktime': '2017-03-13 11:51:41', 'lessonid': 271}, {'checktime': '2017-03-14 00:51:12', 'lessonid': 272}, {'checktime': '2017-03-14 11:55:04', 'lessonid': 273}, {'checktime': '2017-03-16 00:54:52', 'lessonid': 274}], 53: [{'checktime': '2017-03-13 11:51:41', 'lessonid': 271}, {'checktime': '2017-03-14 11:56:59', 'lessonid': 273}], 54: [{'checktime': '2017-03-14 00:56:23', 'lessonid': 272}, {'checktime': '2017-03-14 12:03:42', 'lessonid': 273}], 55: [{'checktime': '2017-03-13 11:56:44', 'lessonid': 271}, {'checktime': '2017-03-14 11:53:56', 'lessonid': 273}, {'checktime': '2017-03-16 00:58:57', 'lessonid': 274}], 56: [{'checktime': '2017-03-13 11:53:39', 'lessonid': 271}, {'checktime': '2017-03-14 00:52:16', 'lessonid': 272}, {'checktime': '2017-03-14 11:52:24', 'lessonid': 273}, {'checktime': '2017-03-16 01:07:59', 'lessonid': 274}], 57: [{'checktime': '2017-03-16 00:50:40', 'lessonid': 274}], 59: [{'checktime': '2017-03-14 11:51:32', 'lessonid': 273}], 60: [{'checktime': '2017-03-13 12:05:52', 'lessonid': 271}, {'checktime': '2017-03-14 11:57:14', 'lessonid': 273}, {'checktime': '2017-03-16 01:03:04', 'lessonid': 274}], 61: [{'checktime': '2017-03-13 11:58:18', 'lessonid': 271}, {'checktime': '2017-03-14 01:07:16', 'lessonid': 272}, {'checktime': '2017-03-14 11:57:50', 'lessonid': 273}], 62: [{'checktime': '2017-03-16 00:58:14', 'lessonid': 274}], 64: [{'checktime': '2017-03-13 11:51:32', 'lessonid': 271}, {'checktime': '2017-03-14 00:54:01', 'lessonid': 272}, {'checktime': '2017-03-14 11:52:50', 'lessonid': 273}, {'checktime': '2017-03-16 00:51:37', 'lessonid': 274}], 66: [{'checktime': '2017-03-13 11:59:01', 'lessonid': 271}], 67: [{'checktime': '2017-03-13 11:51:48', 'lessonid': 271}, {'checktime': '2017-03-14 00:51:51', 'lessonid': 272}, {'checktime': '2017-03-14 11:50:13', 'lessonid': 273}, {'checktime': '2017-03-16 00:51:09', 'lessonid': 274}], 68: [{'checktime': '2017-03-13 11:52:21', 'lessonid': 271}, {'checktime': '2017-03-14 11:59:06', 'lessonid': 273}, {'checktime': '2017-03-16 00:50:13', 'lessonid': 274}], 69: [{'checktime': '2017-03-13 11:58:01', 'lessonid': 271}, {'checktime': '2017-03-14 11:55:56', 'lessonid': 273}], 70: [{'checktime': '2017-03-16 00:56:55', 'lessonid': 274}], 71: [{'checktime': '2017-03-13 11:58:03', 'lessonid': 271}], 74: [{'checktime': '2017-03-13 11:58:29', 'lessonid': 271}, {'checktime': '2017-03-14 00:57:44', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:24', 'lessonid': 273}], 75: [{'checktime': '2017-03-13 12:00:14', 'lessonid': 271}, {'checktime': '2017-03-14 00:55:50', 'lessonid': 272}, {'checktime': '2017-03-14 11:52:45', 'lessonid': 273}, {'checktime': '2017-03-16 00:51:58', 'lessonid': 274}], 76: [{'checktime': '2017-03-13 11:58:30', 'lessonid': 271}, {'checktime': '2017-03-14 11:57:51', 'lessonid': 273}], 77: [{'checktime': '2017-03-13 11:52:14', 'lessonid': 271}, {'checktime': '2017-03-14 11:55:11', 'lessonid': 273}, {'checktime': '2017-03-16 00:50:37', 'lessonid': 274}], 78: [{'checktime': '2017-03-13 11:54:17', 'lessonid': 271}, {'checktime': '2017-03-14 00:58:36', 'lessonid': 272}, {'checktime': '2017-03-14 12:01:29', 'lessonid': 273}, {'checktime': '2017-03-16 00:52:33', 'lessonid': 274}], 79: [{'checktime': '2017-03-13 11:50:55', 'lessonid': 271}, {'checktime': '2017-03-16 00:50:15', 'lessonid': 274}], 81: [{'checktime': '2017-03-13 11:52:30', 'lessonid': 271}, {'checktime': '2017-03-14 11:51:18', 'lessonid': 273}], 82: [{'checktime': '2017-03-13 11:51:41', 'lessonid': 271}, {'checktime': '2017-03-14 11:58:11', 'lessonid': 273}], 83: [{'checktime': '2017-03-13 11:59:14', 'lessonid': 271}, {'checktime': '2017-03-14 01:04:52', 'lessonid': 272}, {'checktime': '2017-03-14 11:56:55', 'lessonid': 273}, {'checktime': '2017-03-16 01:03:06', 'lessonid': 274}], 85: [{'checktime': '2017-03-13 11:50:25', 'lessonid': 271}, {'checktime': '2017-03-14 00:50:34', 'lessonid': 272}, {'checktime': '2017-03-14 11:51:07', 'lessonid': 273}, {'checktime': '2017-03-16 00:53:45', 'lessonid': 274}], 87: [{'checktime': '2017-03-13 12:05:23', 'lessonid': 271}, {'checktime': '2017-03-14 00:51:25', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:44', 'lessonid': 273}], 88: [{'checktime': '2017-03-13 11:56:58', 'lessonid': 271}], 90: [{'checktime': '2017-03-13 11:54:27', 'lessonid': 271}, {'checktime': '2017-03-14 00:56:13', 'lessonid': 272}], 91: [{'checktime': '2017-03-13 11:58:53', 'lessonid': 271}, {'checktime': '2017-03-14 11:53:37', 'lessonid': 273}], 92: [{'checktime': '2017-03-14 11:51:52', 'lessonid': 273}], 93: [{'checktime': '2017-03-13 12:00:39', 'lessonid': 271}], 94: [{'checktime': '2017-03-13 12:03:30', 'lessonid': 271}, {'checktime': '2017-03-14 11:52:08', 'lessonid': 273}], 95: [{'checktime': '2017-03-13 12:07:28', 'lessonid': 271}, {'checktime': '2017-03-14 01:02:38', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:02', 'lessonid': 273}, {'checktime': '2017-03-16 01:02:47', 'lessonid': 274}], 96: [{'checktime': '2017-03-13 11:56:04', 'lessonid': 271}, {'checktime': '2017-03-14 00:56:02', 'lessonid': 272}, {'checktime': '2017-03-14 12:02:44', 'lessonid': 273}, {'checktime': '2017-03-16 00:59:30', 'lessonid': 274}], 98: [{'checktime': '2017-03-13 11:51:43', 'lessonid': 271}, {'checktime': '2017-03-14 00:54:50', 'lessonid': 272}, {'checktime': '2017-03-14 11:56:55', 'lessonid': 273}, {'checktime': '2017-03-16 00:50:25', 'lessonid': 274}], 99: [{'checktime': '2017-03-13 11:54:24', 'lessonid': 271}, {'checktime': '2017-03-14 11:58:07', 'lessonid': 273}], 101: [{'checktime': '2017-03-13 11:57:14', 'lessonid': 271}], 102: [{'checktime': '2017-03-13 12:05:30', 'lessonid': 271}, {'checktime': '2017-03-14 00:56:35', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:24', 'lessonid': 273}, {'checktime': '2017-03-16 00:54:26', 'lessonid': 274}], 103: [{'checktime': '2017-03-13 11:55:22', 'lessonid': 271}, {'checktime': '2017-03-14 11:51:10', 'lessonid': 273}, {'checktime': '2017-03-16 00:54:48', 'lessonid': 274}], 104: [{'checktime': '2017-03-13 11:55:49', 'lessonid': 271}, {'checktime': '2017-03-14 00:54:22', 'lessonid': 272}, {'checktime': '2017-03-14 11:54:40', 'lessonid': 273}, {'checktime': '2017-03-16 00:51:11', 'lessonid': 274}], 105: [{'checktime': '2017-03-14 11:51:17', 'lessonid': 273}], 106: [{'checktime': '2017-03-13 11:55:28', 'lessonid': 271}, {'checktime': '2017-03-14 11:51:11', 'lessonid': 273}], 107: [{'checktime': '2017-03-13 11:57:33', 'lessonid': 271}, {'checktime': '2017-03-14 01:05:49', 'lessonid': 272}, {'checktime': '2017-03-14 12:02:18', 'lessonid': 273}, {'checktime': '2017-03-16 00:50:49', 'lessonid': 274}], 108: [{'checktime': '2017-03-13 11:52:13', 'lessonid': 271}, {'checktime': '2017-03-14 00:51:34', 'lessonid': 272}, {'checktime': '2017-03-14 11:50:46', 'lessonid': 273}], 109: [{'checktime': '2017-03-13 11:50:44', 'lessonid': 271}, {'checktime': '2017-03-14 11:56:23', 'lessonid': 273}], 110: [{'checktime': '2017-03-13 11:51:58', 'lessonid': 271}, {'checktime': '2017-03-14 00:53:51', 'lessonid': 272}, {'checktime': '2017-03-14 11:52:20', 'lessonid': 273}, {'checktime': '2017-03-16 00:53:03', 'lessonid': 274}], 111: [{'checktime': '2017-03-13 11:56:32', 'lessonid': 271}, {'checktime': '2017-03-14 11:57:24', 'lessonid': 273}, {'checktime': '2017-03-16 00:52:15', 'lessonid': 274}], 113: [{'checktime': '2017-03-13 12:00:08', 'lessonid': 271}, {'checktime': '2017-03-14 00:59:01', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:40', 'lessonid': 273}, {'checktime': '2017-03-16 00:59:30', 'lessonid': 274}], 114: [{'checktime': '2017-03-13 11:54:47', 'lessonid': 271}, {'checktime': '2017-03-14 00:51:29', 'lessonid': 272}, {'checktime': '2017-03-14 11:51:28', 'lessonid': 273}, {'checktime': '2017-03-16 00:51:31', 'lessonid': 274}], 115: [{'checktime': '2017-03-13 11:52:18', 'lessonid': 271}, {'checktime': '2017-03-14 00:50:13', 'lessonid': 272}, {'checktime': '2017-03-14 11:51:19', 'lessonid': 273}, {'checktime': '2017-03-16 00:50:17', 'lessonid': 274}], 116: [{'checktime': '2017-03-13 11:51:02', 'lessonid': 271}, {'checktime': '2017-03-14 00:52:11', 'lessonid': 272}, {'checktime': '2017-03-14 11:52:56', 'lessonid': 273}, {'checktime': '2017-03-16 00:50:04', 'lessonid': 274}], 118: [{'checktime': '2017-03-13 11:50:31', 'lessonid': 271}], 119: [{'checktime': '2017-03-13 11:50:34', 'lessonid': 271}, {'checktime': '2017-03-14 11:55:13', 'lessonid': 273}], 120: [{'checktime': '2017-03-14 11:50:31', 'lessonid': 273}, {'checktime': '2017-03-16 00:58:28', 'lessonid': 274}], 121: [{'checktime': '2017-03-13 11:50:58', 'lessonid': 271}, {'checktime': '2017-03-14 00:55:18', 'lessonid': 272}, {'checktime': '2017-03-14 11:50:25', 'lessonid': 273}, {'checktime': '2017-03-16 00:56:13', 'lessonid': 274}], 122: [{'checktime': '2017-03-13 11:51:39', 'lessonid': 271}, {'checktime': '2017-03-14 00:57:06', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:33', 'lessonid': 273}, {'checktime': '2017-03-16 00:53:23', 'lessonid': 274}], 123: [{'checktime': '2017-03-13 11:51:38', 'lessonid': 271}, {'checktime': '2017-03-14 00:57:08', 'lessonid': 272}, {'checktime': '2017-03-16 00:54:19', 'lessonid': 274}], 124: [{'checktime': '2017-03-16 00:56:58', 'lessonid': 274}], 125: [{'checktime': '2017-03-13 11:51:05', 'lessonid': 271}, {'checktime': '2017-03-14 00:54:55', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:04', 'lessonid': 273}, {'checktime': '2017-03-16 01:00:03', 'lessonid': 274}], 126: [{'checktime': '2017-03-13 11:50:19', 'lessonid': 271}, {'checktime': '2017-03-14 00:50:53', 'lessonid': 272}, {'checktime': '2017-03-14 11:50:49', 'lessonid': 273}, {'checktime': '2017-03-16 00:57:22', 'lessonid': 274}], 127: [{'checktime': '2017-03-13 11:52:27', 'lessonid': 271}, {'checktime': '2017-03-14 11:58:47', 'lessonid': 273}], 128: [{'checktime': '2017-03-13 11:51:44', 'lessonid': 271}], 129: [{'checktime': '2017-03-13 11:52:22', 'lessonid': 271}, {'checktime': '2017-03-14 00:52:11', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:31', 'lessonid': 273}, {'checktime': '2017-03-16 00:52:54', 'lessonid': 274}], 130: [{'checktime': '2017-03-13 11:54:19', 'lessonid': 271}, {'checktime': '2017-03-14 01:04:26', 'lessonid': 272}, {'checktime': '2017-03-14 11:53:39', 'lessonid': 273}, {'checktime': '2017-03-16 01:02:29', 'lessonid': 274}], 131: [{'checktime': '2017-03-13 11:50:09', 'lessonid': 271}, {'checktime': '2017-03-14 00:51:38', 'lessonid': 272}, {'checktime': '2017-03-14 11:52:02', 'lessonid': 273}, {'checktime': '2017-03-16 00:55:41', 'lessonid': 274}], 132: [{'checktime': '2017-03-13 12:06:41', 'lessonid': 271}], 133: [{'checktime': '2017-03-13 11:53:10', 'lessonid': 271}, {'checktime': '2017-03-14 00:53:31', 'lessonid': 272}, {'checktime': '2017-03-14 11:52:52', 'lessonid': 273}, {'checktime': '2017-03-16 00:58:27', 'lessonid': 274}], 134: [{'checktime': '2017-03-13 12:06:31', 'lessonid': 271}], 135: [{'checktime': '2017-03-13 11:59:40', 'lessonid': 271}, {'checktime': '2017-03-14 00:58:14', 'lessonid': 272}, {'checktime': '2017-03-14 12:00:07', 'lessonid': 273}, {'checktime': '2017-03-16 00:51:30', 'lessonid': 274}], 136: [{'checktime': '2017-03-14 11:56:54', 'lessonid': 273}], 138: [{'checktime': '2017-03-14 11:53:33', 'lessonid': 273}], 139: [{'checktime': '2017-03-14 11:58:32', 'lessonid': 273}], 141: [{'checktime': '2017-03-14 11:50:15', 'lessonid': 273}], 142: [{'checktime': '2017-03-14 11:55:46', 'lessonid': 273}, {'checktime': '2017-03-16 00:51:19', 'lessonid': 274}]}
二、问题分析
1、方法
1)debug常见于开发环境
2)查看关键变量的值
3)print信息或者log,常见于生产环境
2、两种方式的比较
1)print方式
常见于没有IDE的时候:
a、临时任务
b、生产环境:bug不方便重现;用log方法,tail、tailf查看日志文件
2)IDE debug
a、常用于开发过程中
b、更加方便、快捷
c、可以查看变量、表达式的值