[Tutor] Need Help Modifying a wxPython GUI (scrolling display and logging)

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue Jun 18 17:57:39 CEST 2013


Matt D wrote:
> > There are other ways a script might change the current directory.  For
> > example, some naive scripts use os.chdir()
> >
> > But how is it you don't know what the current directory was when the
> > code ran?   A simply pwd can tell you, if your prompt doesn't already
> > reveal it.
> >
> >
> hey i found the logfile.  just took a few minutes of looking round.  the
> file is logged all out of order so i have some work to do on that
> formatting issue.  if you have a sec can you take a look at my code please?
> 
> def update(self, field_values):
> 
>     	#  logger code---------------
>         #  first write the CURRENT date/time
> 		self.logfile.write('%s,'%(str(strftime("%Y-%m-%d %H:%M:%S", gmtime()))))
> 		#  loop through each of the TextCtrl objects
> 		for k,v in self.fields.items():
>             #  get the value of the current TextCtrl field
> 			f = field_values.get(k, None)
> 			if f:
>                 #output the value with trailing comma
> 				self.logfile.write('%s,'%(str(f)))
> 				self.logfile.write('\n')
> 		#end logger code ----------------
> 
>         #if the field 'duid' == 'hdu', then clear all the fields
> 		if field_values['duid'] == 'hdu':
> 			self.clear()
> 		#loop through all TextCtrl fields storing the key/value pairs in k, v
> 		for k,v in self.fields.items():
> 			# get the pickle value for this text control
> 			f = field_values.get(k, None)
> 			# if the value is empty then set the new value
> 			if f:
> 				v.SetValue(f)
> 
> 
> When i open the .csv file the fields are all out of order.  what i want
> is have them all in one row beginning with the date/time.  and idea?
> Thanks!

Everything Dave Angel said applies.

You can sort the keys by doing and sorting the keys and then logging.
That should ensure field order.

for k in sorted(self.fields):
    v = self.fields[k]


Also note, that unless you do self.logfile.close() it is not guaranteed
that the data is being written to file. I prefer to use the following
idiom for Python 2.6+ (might be in 2.5, but not sure offhand when it was added).

with open('filename.txt', 'a') as f:
    # write data


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list