def entab(temp, tab_width=4, all=0):
#if all is true, every time tab_width number of spaces are found next
#to each other, they are converted to a tab. If false, only those at
#the beginning of the line are converted. Default is false.
if all:
temp = re.sub(r" {" + `tab_width` + r"}", r"\t", temp)
else:
patt = re.compile(r"^ {" + `tab_width` + r"}", re.M)
temp, count = patt.subn(r"\t", temp)
i = 1
while count > 0:
#this only loops a few times, at most six or seven times on
#heavily indented code
subpatt = re.compile(r"^\t{" + `i` + r"} {" + `tab_width` + r"}", re.M)
temp, count = subpatt.subn("\t"*(i+1), temp)
i += 1
return temp
评论列表
文章目录