-1

I have a UI with 8 checkboxes. The idea is that depending on which ones are checked, it will choose what commands to send to telnet and what data files to return.

Currently I just have 8 IF statements. This is causing some of the files to be mixed up when they are written. I think what would solve it is a longer if statement that contains every possible combo, but that is a lot of combinations. Is there a simple way so these statements dont overwrite each other?

Here is some of the code:

        if self.EHbox.isChecked():
            tn.write("geh,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.AHbox.isChecked():
            tn.write("DAT,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()

        if self.DHbox.isChecked():
            tn.write("GDH,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.L1box.isChecked():
            tn.write("gl1,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()           

        if self.L2box.isChecked():
            tn.write("gl2,".encode('ascii') + TS2Dur.encode() + b"\n\r")
            out_file = open(self.linePATH.text() + date + "_001.csv", "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.CMbox.isChecked():
            tn.write("gsf,0".encode('ascii') + b"\n\r")
            out_file = open(self.linePATH.text(), "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.CNbox.isChecked():
            tn.write("gsf,1".encode('ascii') + b"\n\r")
            out_file = open(self.linePATH.text(), "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()            

        if self.FLbox.isChecked():
            tn.write("gsf,2".encode('ascii') + b"\n\r")
            out_file = open(self.linePATH.text(), "wt")
            out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
            out_file.close()
mad5245
  • 394
  • 3
  • 8
  • 20
  • You should try to refactor your blocks - they are all nearly identical... – Stephen May 31 '13 at 19:41
  • @Stephen I am not sure what you mean by 'refactor your blocks'. Could you elaborate to what that is and how I can do it. Also will it solve my issue? – mad5245 Jun 24 '13 at 19:59

1 Answers1

1

This is how I would do it.

First i would have a definition to return which switches are hit. im gonna do a 4 switch example because its the same as an 8 one except less typing.

Create a list of each buttons state (example: [True,False,False,True] = first, fourth switch hit)

def checkSwitches():
     return [EHbox.isChecked(),AHbox.isChecked(),DHbox.isChecked(),L1box.isChecked()]

then some sort of For loop wherever you need to 'do' something .. probably after you click a "Go" button

def clickGo():
    buttons = ['geh,','DAT,','GDH,','gl1,']
    for item in xrange[4]: #<4 is total number of switches
        if checkSwitches()[item]: #checking for Trues ( which im assuming 'isChecked()' returns)
             doButtonJunk(buttons[item])

also your thing can probably be rewritten since you have 8 of PRETTY MUCH the same thing, by doing

def doButtonJunk(THEDIFFERENCEBETWEENYOUR8THINGS)
        tn.write(THEDIFFERENCEBETWEENYOUR8THINGS.encode('ascii') + b"\n\r")
        out_file = open(self.linePATH.text(), "wt")
        out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
        out_file.close()

from here you can modify doButtonJunk() with a lil if statement to see if it needs the `+date+"_001.cvs" part or not ... probably by doing something like this

def doButtonJunk(THEDIFFERENCEBETWEENYOUR8THINGS)
        wt = 'wt' # made new placeholder for it
        if THEDIFFERENCEBETWEENYOUR8THINGS in ['gl2,','GDH,'......]: #i diddnt finish this list, because im lazy.. either way its gonna check to see if it needs the date+ part, and then modify the 'wt' to be what it suppsoed to be.
             wt = date+'_001.csv'+wt 
        tn.write(THEDIFFERENCEBETWEENYOUR8THINGS.encode('ascii') + b"\n\r")
        out_file = open(self.linePATH.text(), wt) #inserted wt instead of 'wt' because we defined it now
        out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode())
        out_file.close()
TehTris
  • 3,139
  • 1
  • 21
  • 33
  • This makes sense. Still a lot to do, I guess I assumed there was a simple way to handle this... – mad5245 Jun 10 '13 at 17:50