微信搜索:编程笔记本
微信搜索:编程笔记本
微信搜索:编程笔记本
点击上方蓝字关注我,我们一起学编程
欢迎小伙伴们分享、转载、私信、赞赏
昨天在跟小伙伴聊天,当他谈起自己正在做的项目时,一脸愁容。
他吐槽道:“该项目的 Python 代码库由多个人共同维护。由于每个人使用的编辑器不同,每个人的编码风格也不同,最终导致了代码的缩进千奇百怪:有缩进 2 个空格的,有缩进 4 个空格的,有缩进 8 个空格,有缩进一个 Tab 的,更有缩进随机数量空格的。导致代码的可读性非常差。“
小伙伴还说:“这种长短不一的代码看着非常难受,我就手动将它们全部改成标准四空格缩进了。也考虑过使用自动化脚本,但一时间没什么思路,就搁置了。”
这反映出一个问题:**不同长度的缩进,虽不影响代码解释,但可读性非常差,一律使用标准的四空格缩进是个不错的主意。**于是,我便尝试写一个简单的脚本工具,来解决这个实际问题。
经过简单分析,确定如下思路:
为了统计每一行源代码前的空格数量,我们使用 行的原始长度 - 删除行左侧空白后的长度 表示。
示例代码如下:
file_input = 'source.py' # file to format
file_output = 'dest.py' # formated file
fin = open(file_input, 'r')
fout = open(file_output, 'w')
space = 0
indent_lists = [0]
for line in fin:
# blank line just output
if len(line.lstrip()) == 0:
fout.write(line)
continue
# calc curret indent
indent = len(line) - len(line.lstrip())
# inc space when bigger indent
if indent > indent_lists[-1]:
indent_lists.append(indent)
space = space + 4
# dec space when smaller indent
while indent < indent_lists[-1]:
indent_lists.pop()
space = space - 4
# format the line
line = ' ' * space + line.lstrip()
fout.write(line)
fin.close()
fout.close()
下面我们进行简单的测试。
待格式化的源文件:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for num in range(1, 10):
if num % 2 == 0:
print(str(num) + ' is even')
else:
print(str(num) + ' is odd')
可以看到:line5 缩进了 2 个空格,line6 缩进了 8 个空格,line8缩进了 4 个空格。看起来非常杂乱。
使用本脚本格式化后的源文件:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for num in range(1, 10):
if num % 2 == 0:
print(str(num) + ' is even')
else:
print(str(num) + ' is odd')
可以看到,执行脚本后,所有的缩进都变成了 4 个空格。舒服了…
随后小伙伴又提出:这个脚本每次只能处理一个文件,能不能批量处理呢?当然可以啦!只要对本脚本进行简单的封装,即可实现多文件、多目录处理啦:
def format_file(file_input, file_output):
fin = open(file_input, 'r')
fout = open(file_output, 'w')
space = 0
indent_lists = [0]
for line in fin:
# blank line just output
if len(line.lstrip()) == 0:
fout.write(line)
continue
# calc curret indent
indent = len(line) - len(line.lstrip())
# inc space when bigger indent
if indent > indent_lists[-1]:
indent_lists.append(indent)
space = space + 4
# dec space when smaller indent
while indent < indent_lists[-1]:
indent_lists.pop()
space = space - 4
# format the line
line = ' ' * space + line.lstrip()
fout.write(line)
fin.close()
fout.close()
files_to_format = 3
file_input_lists = ['1.py', '2.py', '3.py'] # file lists to format
file_output_lists = ['11.py', '22.py', '33.py'] # formated file lists
for i in range(files_to_format):
format_file(file_input_lists[i], file_output_lists[i])
上面代码只是一个简单的做法,当然我们可以根据目录或者更多规则进行文件格式化,本例仅做抛砖引玉之意。
微信搜索:编程笔记本
微信搜索:编程笔记本
微信搜索:编程笔记本