我必须将遗留代码(~60K LOC)从 Python 2 移植到 3,它有几千个结构,如下所示:
class Sample(object):
__slots__ = ('both', 'advertise')
class __metaclass__(type):
__instancecheck__ = classmethod(
lambda cls, inst: inst in ('both', 'advertise'))
both = 'both'
advertise = 'advertise'
此代码适用于 Python 2,但不能使用 Python 3 编译,要解决它,我需要将其更改为
class Sample(object):
__slots__ = ('both', 'advertise')
class __metaclass__(type):
__instancecheck__ = classmethod(
lambda cls, inst: inst in ('both', 'advertise'))
def __init__(self):
both = 'both'
advertise = 'advertise'
鉴于必须对如此大的文件多次执行此更改,什么是处理此更改的有效方法?
我们必须考虑到该类可能已经有也可能没有__init__
函数定义,并且也可以有嵌套的类定义。
这是我到目前为止所尝试的。
2to3
不认为这是一个问题,因此不会改变它。- 一种可能的方法是使用
ast
模块来修改内存中的解析树,然后用它unparse
来写回修改后的代码。但这并不简单。 - 如果没有其他工作,我会考虑编写一个简单的 Shell/Python 脚本,它将读取源文件、进行更改并将其写回。
是否有另一种快速简单的方法来处理此更改。