def backport(rootdir="."):
for folder, subs, files in os.walk(rootdir):
for filename in files:
src_filename = os.path.join(folder, filename)
# Skip non python files
if not src_filename.endswith(".py"):
continue
if (__file__ and os.path.basename(src_filename) ==
os.path.basename(__file__)):
continue
print(src_filename)
last_class = ""
for line in fileinput.input(src_filename, inplace=True):
if fileinput.filelineno() == 1:
if line.startswith("#!"):
print(line, end="")
print("from __future__ import unicode_literals")
else:
print("from __future__ import unicode_literals")
print(line, end="")
continue
if line.strip().startswith("class"):
last_class = line.strip().split()[1]
last_class = re.match(r'([a-zA-Z0-9]+)',
last_class).group(1)
if "__str__(" in line:
line = line.replace("__str__(", "__unicode__(")
if "super()" in line:
old_super = "super({}, self)".format(last_class)
line = line.replace("super()", old_super)
print(line, end="")
backport3to2.py 文件源码
python
阅读 27
收藏 0
点赞 0
评论 0
评论列表
文章目录