<p>如果@alex hall的直截了当的猜测和简洁的建议对OP来说不够明确,可能是这样的:</p>
<pre><code>#! /usr/bin/env python
from __future__ import print_function
import re
def open_func_file(args):
functionality_file_context = ''
func_file = ''
for func_file in args.functionality_file:
functionality_file_context = open(func_file).readlines()
return func_file, functionality_file_context
def func_override_parameter_value(
fline, func_file, function_line, value):
"""Mocking the undefined function."""
return 42
args = "some_file_name"
x = 'XXX'
y = 'YYY'
value = ''
name = ''
myDict = {
"Bike": 0,
"Car": 2,
}
commentregex = re.compile('^[^#]') # ignore lines that start with comment (#)
func_file, functionality_file_context = open_func_file(args)
for fline in functionality_file_context:
if fline.strip(): # ignore empty lines or lines with only whitespace
if (commentregex.match(fline)):
# replace tabs with whitespace
replacetab = re.compile(r'\s+')
fline = replacetab.sub(' ', fline)
function_line = fline.split(' ', 2)
if len(function_line) != 3:
exit("Something wrong with this line: "
"" + fline.strip() + "\" in: " + func_file)
name = x + function_line[0].strip() + y
value = func_override_parameter_value(
fline, func_file, function_line, value)
print(name + str(value))
myDict[name] = value
</code></pre>
<p>提示:向下滚动到上面代码的最后一行,其中只说明:</p>
<pre><code>myDict[name] = value
</code></pre>