On 06/14/2013 10:48 AM, Matt D wrote:
Hey,
here is a snip of my code.

#logger code----------------------------------------------
                #  first new line
                #self.logfile.write('\n')
                #  date and time
                #self.logfile.write('%s,'%(str(strftime("%Y-%m-%d %H:%M:%S", 
gmtime()))))
                #  blah = [time, 'nac', 'tgid', 'source', 'dest', 'algid'] is 
what we want
                tmplist = []
                tmplist.append(str(strftime("%Y-%m-%d %H:%M:%S", localtime())))
                tmplist.append(field_values["nac"])
                tmplist.append(field_values["tgid"])
                tmplist.append(field_values["source"])
                tmplist.append(field_values["dest"])
                tmplist.append(field_values["algid"])


tmplist is an unnecessary complication. Did you look at my sample loop, which I'll repeat here with a correction:


        for k in FIELD_LIST_NAMES:
            #  get the value of the current TextCtrl field
            f = field_values.get(k, None)
            if not f is None:
                #output the value with trailing comma
                self.logfile.write('%s,'%(str(f)))
            else:
                self.logfile.write(",")
        self.logfile.write("\n")

This code preserves your original feature of not crashing when the C++ program fails to fill in all your expected keys. It also makes sure there will be unadorned commas for missing fields, making it possible for a spreadsheet to read the columns correctly.

If you want to populate a list first, by all means do so, but do it in a loop, using the FIELD_LIST_NAMES as keys.

One thing I didn't handle was the date field. I'd do that by adding it to the field_values dict, or to a copy of it. That way, it's all consistent, even though one field comes from local and the rest from the pickle.





--
DaveA
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to