Variables from variables

paul at nlhas.no paul at nlhas.no
Fri Aug 17 05:38:15 EDT 2001


I've come to a dead end with this one, and would be grateful for
suggestions. Scenario - a tuple with field names (self.description) to
be mapped to class variables and assigned values from self.record.

I've got this far, and get the partially successful result shown
below. I can't fathom the reasons for the exceptions.

Is there an easier way to do this?

pm



y = 0
r=self.record[0]
for x in self.description : 
	k = x[0]
	c = r[y]
	y += 1
	v = 'self.' + k
	exceptiontype = ''
	try :
	   cde = compile( "%s = %s" % (v , c) , '<string>' , 'exec')
           exec cde
           print 'OK ' +  v + ' ' + `type(v)`
	except StandardError , exceptiontype:
	   print `exceptiontype` + v + ' ' + `type(v)`

Result:

OK self.message_id <type 'string'>
<exceptions.SyntaxError instance at 0x81279e4>self.message_subject
<type 'string'>
<exceptions.NameError instance at 0x80f4a04>self.message_responsible
<type 'string'>
<exceptions.SyntaxError instance at 0x8127394>self.message_modified
<type 'string'>
<exceptions.SyntaxError instance at 0x81277bc>self.message_registered
<type 'string'>
OK self.message_alarm <type 'string'>
OK self.message_external <type 'string'>
<exceptions.NameError instance at 0x80f263c>self.message_status <type
'string'>
OK self.message_headers <type 'string'>
OK self.message_thread <type 'string'>
OK self.message_backup <type 'string'>
OK self.message_body <type 'string'>
OK self.message_comment <type 'string'>
<exceptions.SyntaxError instance at 0x81255cc>self.message_inout <type
'string'>
OK self.message_headersig <type 'string'>





More information about the Python-list mailing list