I'm trying to prevent to use eval
based on an example how-to-avoid-eval-in-python-for-string-conversion using ast
. The challange is that there are a dozen of these self.ch%s_label
's to be made but the variable for it changes based on user input in the GUI.
My code:
import ast ...etc.
....
channel_no += 1
ch_width = eval('self.ch%s_label.frameGeometry().width()' % (channel_no))
When I change it into:
ch_width = ast.literal_eval('self.ch%s_label.frameGeometry().width()' % (channel_no))
I'll get the error:
File "c:\python\anac2\lib\ast.py", line 80, in literal_eval return _convert(node_or_string) File "c:\python\anac2\lib\ast.py", line 79, in _convert raise ValueError('malformed string') ValueError: malformed string
Changing the code (using closing " ") retains the error:
ch_width = ast.literal_eval("'self.ch%s_label.frameGeometry().width()' % (channel_no)")
What other options are there... Any suggestions?