Hi all, I've got a problem evaluating a variable which I don't know how to go about solving... Consider the following code: Counter=0 for GetDoc in context.DisplayCustomerSQL(SelectCID=TestCID): print "Customer name is %s %s" % (GetDoc.FirstName, GetDoc.LastName) for FieldCount in FieldName: ExecField = "GetDoc."+FieldName[Counter] print "value of field %s is %s" % (FieldCount, ExecField) Counter=Counter+1 (yes I realise there are probably tidier ways to do this - especially the counter bit! - but hey I am learning!! albeit bad habits.... :) Prior to this the python script scans a database record, and stores a string of field names (represented with the record as [Field1], [Field2], etc). So, by the time the script above gets to the second-from-last line, the var ExecField contains something like GetDoc.Field1 or GetDoc.Field2. As you've probably guessed by now, I want to display the literal value of ExecField, instead than the word "ExecField" at this stage.... I'm not python programmer, but having scanned my copy of "learing Python" thoroughly, I reckon I'd use the "exec" command to do this. Except I'm not in Python, I'm in Zope. And using 'exec ExecField' doesn't work. Nor does "_.exec" which has helped me before in these cases!! I seem to remember reading somewhere that exec and eval were considered too powerful and had been removed in Zope's python - now it seems that is definately the case!! So, does anybody have any idea how I might solve my dilemma...?? (which, by the way, I've arrived at by giving up on trying to Mail Merge Zope with Word, and instead I'm now trying to build the entire docs to be Mail Merged in Zope!!!) many thanks for any help! Regards, Greg Conway. This electronic transmission and any files attached to it are strictly confidential and intended solely for the addressee. If you are not the intended addressee, you must not disclose, copy or take any action in reliance of this transmission. If you have received this transmission in error, please notify us by return and delete the same. Further enquiries/returns can be posted to postmaster@gmlnt.com Thank you.
Greg Conway wrote:
Counter=0 for GetDoc in context.DisplayCustomerSQL(SelectCID=TestCID): print "Customer name is %s %s" % (GetDoc.FirstName, GetDoc.LastName) for FieldCount in FieldName: ExecField = "GetDoc."+FieldName[Counter] print "value of field %s is %s" % (FieldCount, ExecField) Counter=Counter+1
(yes I realise there are probably tidier ways to do this - especially the counter bit! - but hey I am learning!! albeit bad habits.... :)
What's FieldName?
So, by the time the script above gets to the second-from-last line, the var ExecField contains something like GetDoc.Field1 or GetDoc.Field2.
As you've probably guessed by now, I want to display the literal value of ExecField, instead than the word "ExecField" at this stage.... I'm not python programmer, but having scanned my copy of "learing Python" thoroughly, I reckon I'd use the "exec" command to do this.
I don't. exec is bad, never use exec. Can you spell 'security hole'? ;-)
So, does anybody have any idea how I might solve my dilemma...??
you're specifically looking for: getattr(object,variable_containing_attribute_name) Here's a neater version of your code: for GetDoc in context.DisplayCustomerSQL(SelectCID=TestCID): print "Customer name is %s %s" % (GetDoc.FirstName, GetDoc.LastName) for name in FieldName: print "value of field %s is %s" % (name, getattr(GetDoc,name))
(which, by the way, I've arrived at by giving up on trying to Mail Merge Zope with Word,
What were the problems there? cheers, Chris
participants (2)
-
Chris Withers -
Greg Conway