#define
行创建常量。只需在 python 中为这些创建变量:
STDIN_BASEADDRESS = 0x40600000
STDOUT_BASEADDRESS = 0x40600000
# ******************************************************************
# Definitions for driver UARTLITE
XPAR_XUARTLITE_NUM_INSTANCES = 1
因此,您需要做的就是删除该#define
部分,然后插入一个=
等号。一组正则表达式可以完成这项工作:
import re
from functools import partial
replacements = (
partial(re.compile(r'\s*#define ([A-Z_]+) (.*)').sub, r'\1 = \2'),
partial(re.compile(r'\s*/\*\s*(.*?)\s*\*/').sub, r'# \1'),
)
for line in inputsequence:
for transform in replacements:
line = transform(line)
print line
演示:
>>> import re
>>> from functools import partial
>>> replacements = (
... partial(re.compile(r'\s*#define ([A-Z_]+) (.*)').sub, r'\1 = \2'),
... partial(re.compile(r'\s*/\*\s*(.*?)\s*\*/').sub, r'# \1'),
... )
>>> for line in inputsequence:
... for transform in replacements:
... line = transform(line)
... print line
...
STDIN_BASEADDRESS = 0x40600000
STDOUT_BASEADDRESS = 0x40600000
# ****************************************************************
# Definitions for driver UARTLITE
XPAR_XUARTLITE_NUM_INSTANCES = 1
如果您仍想将定义转换为函数,只需调整替换模式:
partial(re.compile(r'\s*#define ([A-Z_]+) (.*)').sub, r'def \1():\n return \2'),
这导致:
def STDIN_BASEADDRESS():
return 0x40600000
def STDOUT_BASEADDRESS():
return 0x40600000
# ****************************************************************
# Definitions for driver UARTLITE
def XPAR_XUARTLITE_NUM_INSTANCES():
return 1