[Tutor] Changing the Attribute of a Variable
Wayne Watson
sierra_mtnview at sbcglobal.net
Tue Feb 17 13:44:18 CET 2009
Note that the diagnostic output in the image shows attributeError: 'str'
object has no attribute 'strftime'.
Let me see if I clarify what's really going on by including some of the
code.
In Sentinel_GUI, the mainloop, the code shows (hard coded default values):
...
self.slowdown = 1
self.stop_time = datetime.time(6,0,0) <<---
self.start_time = datetime.time(18,0,0)
The code for OpenConfigFile is below, which reads the text file
containing the configuration variables and values.
===========Sample of config file==========
Sentinel NC Configuration File Sentinel User 3 - 1/3/2009 (Meteor Software)
config_file_name=Initial.sen
mask_file_name=*none*
gray_scale=True
post_event_stack=False
post_event_format=Tiff 2
show_real_time=False
hourly_rate=12
slowdown=1
start_time=22:00:00
stop_time=06:00:00
lat=40.0
...
===========end of sample==============
Note below that I'm trying to pick off "user" variables in the file that
are dates. self.config_var_list contains DAT (user) type for stop_time.
Note the use of setattr at the bottom. I may have gone wrong here, when
the code handles the DAT differently than the other user variables. See
the code after this, where the error is raised when the dialog begins to
be activated. There are a few more comments below this.
==============OpenConfigFile==========
def OpenConfigFile(self):
def time_tuple(tstring):
t = tstring.split(':')
print 'here is t', t
tnum = ()
for j in range(0,len(t)):
tnum = tnum+(string.atoi(t[j]),)
return tnum
print "OCFile entered"
print
config_file_name = askopenfilename( title="Open Configuration File",
filetypes=CEN_FILE_TYPES )
config_file=open(config_file_name,'r')
first_line = config_file.readline() # skip first line
for (j,newline) in enumerate(config_file):
aline = newline[:-1]
aline.rstrip()
(config_var, config_value) = aline.split('=')
config_type = self.config_var_list[j][1][1]
self_var = "self." + config_var
print "ocf: ",config_var,config_value,"self_var=",self_var
if config_var == DAT: # Date type, expecting hh:mm:ss
t_ntup = time_tuple(config_value)
stime = datetime.time(t_ntup[0],t_ntup[1],t_ntup[2])
print "type stime: ",type(stime)
# Date vars should be type datetime.time
config_value = str(stime)
else:
self_var_assignment = self_var +'='+ config_value
print "self_var_assignment", self_var_assignment
abc = self
# self_var_assignment
setattr(self, config_var, config_value)
config_file.close()
=============End of OpenConfigFile===========
When the program brings up the dialog, this piece of code gets me into
trouble, as marked.
===========OperationalSettings===============
def OperationalSettings(self):
print "OSett self = ", self, "type =", type(self)
print
set_loc_dict = {}
set_loc_dict[ "ok" ] = False
set_loc_dict[ "color" ] = 2
if self.gray_scale:
set_loc_dict[ "color"] = 1
print "gray scale now--wtw: ", self.gray_scale
set_loc_dict[ "hourly_rate" ] = self.hourly_rate
print "wtw self.stop_time", self.stop_time, type(self.stop_time)
# set in GUI as datetime.time(6,0,0)
# HEY wtw self.stop_time.strftime("%H:%M:%S")
set_loc_dict[ "stop_time" ] =
self.stop_time.strftime("%H:%M:%S") <<----problem, see image in first post
set_loc_dict[ "start_time" ] = self.start_time.strftime("%H:%M:%S")
set_loc_dict[ "slowdown" ] = self.slowdown
...
===============End of OperationalSettings======
I've defined several user types, DAT (date-time format, time
really--hh:mm:ss), BOO (boolean), STR (string), FLT (float), ... In the
OpenConfigFile, I've deliberately singled out DAT, and just relegated
all others to STR. I figured I'd have trouble here, so put DAT on stage
first. Note though that gray_scale is boolean, and "seems" to have been
set without a fuss.
More information about the Tutor
mailing list