I have a SQL string, for example
SELECT * FROM benchmark WHERE xversion = 1.0
And actually, xversion is aliased variable, and self.alias has all the alias info something like
{'CompilationParameters_Family': 'chip_name',
'xversion': 'CompilationParameters_XilinxVersion', 'opt_param':
....
'chip_name': 'CompilationParameters_Family',
'CompilationParameters_Device': 'device'}
Using this alias, I should change the string into as follows.
SELECT * FROM benchmark WHERE CompilationParameters_XilinxVersion = 1.0
For this change, I came up with the following.
def processAliasString(self, sqlString):
components = sqlString.split(' ')
resList = []
for comp in components:
if comp in self.alias:
resList.append(self.alias[comp])
else:
resList.append(comp)
resString = " ".join(resList)
return resString
But, I expect better code not using for loop. What do you think?
If you could change your input string’s format to make the replacements more clearly visible, e.g.
then
s % self.aliaswould suffice (there are some other alternatives available depending on your favorite formatting syntax and Python level).If the input string format is “nailed down”,
recan help because of the ease it offers to identify word boundaries (so you won’t e.g. unexpectedly miss a replacement if an otherwise insignificant space is missing, for example afterxversion). Consider (withshaving its original form, with substitutables mixed in haphazardly with non-substitutables):These approaches are fast, since
%-formatting andres’subare really well optimized for such tasks.