想法&需求
1>在看代码的时候, 总会想: 这个api很好, 这段代码用法很好. 总结下来. 但当看完书的时候就忘了.
2>有时候会无聊, 无所事事. 浪费了一段时间后才想起, 其实可以做XX, YY.
解决办法
1>在看代码的时候用注释方式做标记, 记录下来.
2>用小程序遍历这些代码, 找出这些标记.
3>将这些项记录到excel里. 无聊的时候就打开这个excel看看有什么东西做.
todo的例子(用一些ide可以方便生成大部分注释)
------------------------------
-------------------------- todo sample begin
#todo_begin:
#item: 配置一个方便的调试 spring3 + hibernate 3 的教程
#estimate: 4-8h
#file: /media/dev/utopism2008/projects/python/dpytools/src/findtodo/FindToDo.py
#todo_end:
#todo_begin:
#item: 总结xlrt 和 xlwt 用法. 特别是 nrows 和 写已有的excel
#estimate: 4h
#file: /media/dev/utopism2008/projects/python/dpytools/src/findtodo/FindToDo.py
#todo_end:
-------------------------------------------------------- todo sample end
程序源码:
1 #! /usr/bin/python
2 '''
3 Created on Feb 6, 2011
4
5 1>Find todo string in every file read.
6 2>Set this content into excel file. Every weekend I can check this file and make next plan based on these items.
7
8 @author: yunshichen@gamil.com
9 '''
10 # -*- coding: utf8 -*-
11
12 import os
13 import ConfigParser
14
15 import xlrd
16 import xlutils.copy
17 import xlwt
18
19 ITEM_KEY = "#item:"
20 EST_KEY = "#estimate:"
21 FILE_PATH_KEY = "#file:"
22
23 '''
24 A typical sample of todo item in file:
25
26 #todo_begin:
27 #item: To summarize usage of Qt4 widget
28 #estimate: 5m
29 #file: /media/dev/utopism2008/projects/python/dpytools/src/findtodo/FindToDo.py
30 #todo_end:
31
32 '''
33 def findTodo(fpath,alist):
34 todo_begin = "todo_begin"
35 todo_end = "todo_end"
36 isFinding = False
37 fread = open(fpath,"r")
38 aitem = None
39 for line in fread:
40
41 if todo_begin in line:
42 isFinding = True
43 aitem = {}
44 continue
45
46 if todo_end in line:
47 isFinding = False
48 continue
49
50 if( isFinding == False):
51 continue
52
53 if mapKeyValue(aitem,ITEM_KEY,line):
54 continue
55
56 if mapKeyValue(aitem,EST_KEY,line):
57 continue
58
59 if mapKeyValue(aitem,FILE_PATH_KEY,line):
60 if FILE_PATH_KEY in line:
61 alist.append(aitem)
62 aitem = None
63 print "--Find todo item in " + fpath + "\n"
64 continue
65
66
67 fread.close()
68
69
70 def mapKeyValue(item,key,line):
71 if key in line:
72 #test
73 print "key is:" + key + "\n"
74 item[key] = line[len(key):]
75 return True
76
77 return False
78
79
80 def findTodoFromFolder(alist,folder):
81 for parent, dirnames, filenames in os.walk(folder):
82 for fname in filenames:
83 findTodo(os.path.join(parent, fname),alist)
84
85
86 def mainLogic():
87 configFileName = "config.txt"
88 cf = ConfigParser.ConfigParser();
89 cf.read(configFileName)
90
91 flist = cf.get("path", "folder_list").split(",")
92 itemlist = []
93 for folder in flist:
94 findTodoFromFolder(itemlist,folder)
95
96 excelPath = cf.get("path", "excel_path")
97
98 mapItemIntoExcel(itemlist,excelPath)
99
100 print "\n---- OK -------- \n"
101
102 def createCellStype():
103 font = xlwt.Font()
104 font.wrap = xlwt.Alignment.WRAP_AT_RIGHT
105 style = xlwt.XFStyle()
106 style.font = font
107 return style
108
109
110 def mapItemIntoExcel(itemlist,excelPath):
111 try:
112
113 rb = xlrd.open_workbook(excelPath,formatting_info=True,on_demand=True)
114 wb = xlutils.copy.copy(rb)
115 sheet = wb.get_sheet(2)
116
117 row_index = len(sheet.rows)
118 row_index += 1
119
120 cellStyle = createCellStype()
121 for item in itemlist:
122 sheet.row(row_index).write(xlwt.Utils.col_by_name("A"),unicode(item[ITEM_KEY]),cellStyle )
123 sheet.row(row_index).write(xlwt.Utils.col_by_name("D"),item[EST_KEY] ,cellStyle)
124 sheet.row(row_index).write(xlwt.Utils.col_by_name("F"),"N" ,cellStyle)
125 sheet.row(row_index).write(xlwt.Utils.col_by_name("H"),item[FILE_PATH_KEY] ,cellStyle)
126 row_index += 1
127
128 wb.save(excelPath)
129
130 finally:
131 rb.release_resources
132
133 if __name__ =="__main__":
134 mainLogic();
135
136
程序和配置文件请在这里下载: http://www.uudisc.com/user/diegoyun/file/3880743
如何运行
1> sudo apt-get install python-xlrt python-xlwt
2> 到这个页面下载 xlutis :
http://pypi.python.org/pypi/xlutils#downloads .
然后 sudo easy_install xlutils 该功能需要安装python 的 setuptools 功能. 如果没装,
ubuntu 会提示你安装.
3> 配置config.txt, 指定目录和excel路径
4> 运行 FindToDo.py