发几个自用的代码工具块
###删除UltraEdit的.bak文件
#import os
#for subdir,itsSubdirs,itsFiles in os.walk('.'): #os.walk递归遍历一个目录,返回的元素为(子目录名,该子目录的直接子目录list,该子目录直接拥有的文件list)的迭代器
# for file in itsFiles:
# mainname,extname = os.path.splitext(file)
# main1,main2 = os.path.splitext(mainname)
# if extname == '.bak' and main2 in ['.py','.txt','.java']:
# filepath = os.path.join(subdir,file)
# print 'Delete',filepath
# os.remove(filepath)
可以放到文件头,便于收集代码块,不用的就#掉,用的时候去掉#(ue的块编辑很方便),可以看出哪些代码忘记删掉了
#####打印执行程序块的范围
import re
file = open(__file__)
linenum = start = end = 0
segments = []
rule = re.compile(r'^\s*(#.*)?$') #无效行:空行或者注释行
for line in file:
linenum += 1
if not rule.match(line): #如果是有效行
if start == 0:
start = end = linenum
else:
end += 1
else:
if start != 0:
segments.append((start,end))
start = end = 0
file.close()
spandur = 2 #如果中间跨spandur个无效行,仍然认为是一个程序块
spanseg = []
if len(segments) >= 2:
prestart,preend = segments.pop(0)
while len(segments) > 0:
curstart,curend = segments.pop(0)
if curstart - preend <= spandur:
prestart,preend = prestart,curend
else:
spanseg.append((prestart,preend))
prestart,preend = curstart,curend
spanseg.append((prestart,preend))
print 'lines executed:',spanseg[2:]
print '******************************************************'
###统计代码执行时间
#import time
#print time.time()
#for i in range(1000000):
# pass
#print time.time()
这个好像是cookbook上的
#####返回当前行的信息:源文件 行号 函数名 内容 调用栈
#import traceback
#def lineinfo(*args):
#
# stack = traceback.extract_stack() #tuple表示的栈,元素是(filename, line number, funcofline, linecontext) funcofline指的是line所属的函数名,当属于模块时为常数'<module>'
# #for item in stack:
# # print item
# a, b, c, d = stack[-2] #倒数第二个,因为倒数第一个是本句的内容,应该是调用linenum的内容
#
# if args==():
# return b
# out = []
# for obj in args:
# if obj == 'n' or obj =='number'ut.append(str(b))
# elif obj == 'f' or obj =='function'ut.append(str(c))
# elif obj == 's' or obj =='source'ut.append(str(a))
# elif obj == 'c' or obj =='content'ut.append(str(d))
# elseut.append(str(obj))
# return ' '.join(out)
#def fun1():
# print lineinfo('I', 'can', 'print any thing here',[1,2,3])
#
#fun1()
#
#print lineinfo()
#print lineinfo('n','f','s','c')
#print lineinfo('\nline number:','n','\nsource file:','s','\nfunction is:','f','\nline context:','c')
#a ='sss';print lineinfo('c')
先这些