critique my code, please

Brian Blais bblais at bryant.edu
Mon Feb 6 08:28:23 EST 2006


Hello,

I am including at the end of this document (is it better as an attachment?) some code
for a small gui dialog.  Since I am quite new to this, if anyone has any suggestions
for improvements to the code, bad coding practices, poor gui design, etc... I'd love
to hear it.  This list has been very helpful to me so far, and I hope to be able to
return the favor someday when I get good enough to take the training wheels off.  :)

The code makes a simple class, with some parameters, some of which are numbers, some
boolean, and one which is either a string or a number depending on context.  There is
a dialog class which allows you to edit/change the values, and a wrapper function of
the form:  new_params <== wrapper(old_params)  which calls the dialog, and returns
the updated params instance.

		thanks,

			Brian Blais

-- 
-----------------

             bblais at bryant.edu
             http://web.bryant.edu/~bblais

import wx
import copy

class SimulationParams(object):

     def __init__(self):

         self.epoch_number=500
         self.iter_per_epoch=500
         self.epoch_per_display=1
         self.random_seed='clock'
         self.keep_every_epoch=0
         self.save_input_vectors=0

     def __repr__(self):

         yesno={0:"No",1:"Yes",True:"Yes",False:"No"}

         s="Epoch Number: %d\n" % self.epoch_number
         s=s+"Iter Per Epoch: %d\n" % self.iter_per_epoch
         s=s+"Epoch Per Display: %d\n" % self.epoch_per_display
         if (isinstance(self.random_seed,str)):
             s=s+"Random Seed: %s\n" % self.random_seed
         else:
             s=s+"Random Seed: %d\n" % self.random_seed

         s=s+"Keep Every Epoch: %s\n" % yesno[self.keep_every_epoch]
         s=s+"Save Input Vectors: %s\n" % yesno[self.save_input_vectors]
         return(s)


class SimulationParamsDialog(wx.Dialog):

     def __init__(self,params,parent=None):

         self.params=params


         wx.Dialog.__init__(self, parent, -1, "Simulation Parameters")

         sizer = wx.BoxSizer(wx.VERTICAL)
         box = wx.BoxSizer(wx.HORIZONTAL)

         label = wx.StaticText(self, -1, "Epoch_Number:")
         box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

         self.text1 = wx.TextCtrl(self, -1, str(params.epoch_number), size=(80,-1))
         box.Add(self.text1, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

         sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

         box = wx.BoxSizer(wx.HORIZONTAL)
         label = wx.StaticText(self, -1, "Iterations Per Epoch:")
         box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

         self.text2 = wx.TextCtrl(self, -1, str(params.iter_per_epoch), size=(80,-1))
         box.Add(self.text2, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

         sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

         box = wx.BoxSizer(wx.HORIZONTAL)
         label = wx.StaticText(self, -1, "Epoch Per Display:")
         box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

         self.text3 = wx.TextCtrl(self, -1, str(params.epoch_per_display), size=(80,-1))
         box.Add(self.text3, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

         sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

         box = wx.BoxSizer(wx.HORIZONTAL)
         label = wx.StaticText(self, -1, "Random Seed:")
         box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

         self.text4 = wx.TextCtrl(self, -1, str(params.random_seed), size=(80,-1))
         box.Add(self.text4, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

         sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

         self.cb1 = wx.CheckBox(self, -1, "Keep Every Epoch")
         self.cb1.SetValue(params.keep_every_epoch)
         sizer.Add(self.cb1, 1, wx.GROW|wx.ALIGN_CENTRE|wx.ALL, 5)
         self.cb2 = wx.CheckBox(self, -1, "Save Input Vectors")
         self.cb2.SetValue(params.save_input_vectors)
         sizer.Add(self.cb2, 1, wx.GROW|wx.ALIGN_CENTRE|wx.ALL, 5)




         btnsizer = wx.StdDialogButtonSizer()

         btn = wx.Button(self, wx.ID_OK)
         btn.SetHelpText("The OK button completes the dialog")
         btn.SetDefault()
         btnsizer.AddButton(btn)

         btn = wx.Button(self, wx.ID_CANCEL)
         btn.SetHelpText("The Cancel button cnacels the dialog. (Cool, huh?)")
         btnsizer.AddButton(btn)
         btnsizer.Realize()


         sizer.Add(btnsizer, 1, wx.GROW|wx.ALIGN_CENTRE|wx.ALL, 5)
         self.SetSizer(sizer)
         sizer.Fit(self)


def SetSimParams(params):

     new_params=copy.copy(params)

     dlg=SimulationParamsDialog(params)
     val=dlg.ShowModal()

     if val == wx.ID_OK:
         new_params.epoch_number=eval(dlg.text1.GetValue())
         new_params.iter_per_epoch=eval(dlg.text2.GetValue())
         new_params.epoch_per_display=eval(dlg.text3.GetValue())

         if (dlg.text4.GetValue()=='clock'):
             new_params.random_seed='clock'
         else:
             new_params.random_seed=eval(dlg.text4.GetValue())

         new_params.keep_every_epoch=dlg.cb1.GetValue()
         new_params.save_input_vectors=dlg.cb2.GetValue()

         print "ok"
     else:
         print "cancel"

     dlg.Destroy()

     return(new_params)

if __name__ == '__main__':
     app = wx.PySimpleApp(0)

     params=SimulationParams()
     new_params=SetSimParams(params);

     print params
     print new_params
     app.MainLoop()





More information about the Python-list mailing list