I have a very simple GUI that accepts two parameters and then calls three other classes whose names are DigitalFilter(), BeatByBeatVariables(), and GetSummaryOfWholeTest(). This is my first time writing up classes, and I need help with the syntax.
Specifically, ca开发者_Go百科n you help me with inheritance? I want the GUI's class MainWindow(wx.Frame) to be called when the application launches. Then, when the user clicks on self.button in the GUI, I want the application to first run myFilter=DigitalFilter(TestID,FilterTimePeriod), then second run myBeatByBeat = BeatByBeatVariables(arg1,arg2), and finally third run myTestSummary = GetSummaryOfWholeTest(argA,argB)
Note that myBeatByBeat cannot begin to run until myFilter has finished being created, because instantiation of myFilter creates csv files that are required as inputs to myBeatByBeat. Similarly, myTest Summary cannot begin to run until myBeatByBeat has finished being created because instantiation of myBeatByBeat creates csv files which are required as input to myTestSummary.
Can anyone show me the proper syntax for writing/instantiating these classes with the appropriate inheritance so that the work of each class will be done in a sequence that respects their input/output relationships?
I am assuming that inheritance should be employed here, but I do not know what should inherit from what. I also do not know if I am failing to see other concepts that are necessary to give the code the necessary inter-relationships.
Here is a synopsis of the relevant code:
class DigitalFilter():
def __init__(self,TestID,FilterTimePeriod):
# All the code for the digital filter goes here.
# I am omitting this class' code for simplicity.
class BeatByBeatVariables():
def __init__(self,arg1,arg2):
# I am omitting this class' code for simplicity
class GetSummaryOfWholeTest():
def __init__(self,argA,ArgB):
# This class' code is omitted for simplicity
class MainWindow(wx.Frame):
def __init__(self, parent,id,title):
wx.Frame.__init__(self,parent,wx.ID_ANY,title, size = (500,500), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
# Create self.editname and self.edithear in code that I am omitting for simplicity
self.button =wx.Button(self, label="Click here to filter the data", pos=(200, 125))
self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
def OnClick(self,event):
FilterTimePeriod = self.editname.GetValue()
TestID = self.edithear.GetValue()
myFilter=DigitalFilter(TestID,FilterTimePeriod)
myBeatByBeat = BeatByBeatVariables(arg1,arg2)
myTestSummary = GetSummaryOfWholeTest(argA,argB)
app = wx.PySimpleApp()
frame = MainWindow(None,-1,"Filtering Software. Version 1.0")
app.MainLoop()
Note: I am using Python 2.6 because I am also using numpy and sciepy
It sounds like you're a bit confused about what inheritance means. Classes which inherit from a base class add extra functionality to that base class, it doesn't imply anything about instantiation order or data passing. If all you want to do is enforce the sequence of object creation, you should be using wx.lib.pubsub
to know when each step is over.
You can send a message from DigitalFilter when the filtering is done and either receive that in an already-instantiated BeatByBeatVariables instance or receive it in your main class and create the BeatByBeatVariables instance at that time. Both work. If I knew I would need all of these instances, I would probably create them ahead of time, like so:
from wx.lib.pubsub import setupkwargs # needed only in version 2.8.11
from wx.lib.pubsub import pub
class DigitalFilter():
def __init__(self,TestID,FilterTimePeriod):
pass
def do_something(self, data):
# do something
pub.sendMessage('filter.done', data=some_data)
class BeatByBeatVariables():
def __init__(self,arg1,arg2):
pub.subscribe(self.do_something, 'filter.done')
def do_something(self, data):
# do something
pub.sendMessage('beatbybeat.done', data=some_data)
class GetSummaryOfWholeTest():
def __init__(self,argA,ArgB):
pub.subscribe(self.do_something, 'beatbybeat.done')
def do_something(self, data):
# do something
pub.sendMessage('summary.done', data=some_data)
Then in your main class, you can have:
def OnClick(self,event):
FilterTimePeriod = self.editname.GetValue()
TestID = self.edithear.GetValue()
myFilter=DigitalFilter(TestID,FilterTimePeriod)
myBeatByBeat = BeatByBeatVariables(arg1,arg2)
myTestSummary = GetSummaryOfWholeTest(argA,argB)
myFilter.do_something()
# all the other do_somethings are triggered by message passing
精彩评论