Variable in a script not working

When I hardcode “.text” ( line 10 that is commented out) when populating the text for a component it works, but using a variable does not. I know I can work around this by testing for the type from my power table, but is there a way to make this assignment without doing this?

def loopPowerTable():
	pyRowData = system.dataset.toPyDataSet(event.source.parent.getComponent('formOnePopulated2').data)
	for row in pyRowData:
		sqlField = row[1]
		dataType = row[2]
		componentName = row[3] + " " + row[1] 
		assign = sqlField+" = event.source.parent.getComponent('"+componentName+"')."+dataType

		try:
		  sqlField = event.source.parent.getComponent(componentName).dataType
		  #sqlField = event.source.parent.getComponent(componentName).text
		  print "OK"+assign
		except AttributeError:
		  print "AttributeError:"+assign

Python has an exec statement and an eval() function that can do these sorts of things, but are rather prone to programmer error. Consider the use of getattr() and a target dictionary.

someDictionary[sqlField] = getattr(event.source.parent.getComponent(componentName), dataType)
3 Likes