How to iterate through a list of objects without repeating the same line of code

Question:

Would you know how i can iterate through the self.checkxxx values so that I do not have to repeat the same if else clause over and over again.
I am not sure how to do it if someone could give me an example that could be very useful
Maybe like a for loop but the for loops i have tried have just not worked.
I just get this error: TypeError: cannot unpack non-iterable CheckState object

def checkbox_ledVA(self):
        self.LED = 'PASS'
        self.LED_fail = 'FAIL'
        self.checkPWR_rot_va = self.ui.PWR_rot_VA.checkState()
        self.checkPWR_grun_va = self.ui.PWR_grun_VA.checkState()
        self.checkP1_va = self.ui.Port1_VA.checkState()
        self.checkP2_va = self.ui.port2_VA.checkState()
        self.checkP3_va = self.ui.Port3_VA.checkState()
     
        if self.checkPWR_rot_va == 2:
            self.checkPWR_rot_va = self.LED_pass
        else:
            self.checkPWR_rot_va = self.LED_fail
        
        if self.checkPWR_grun_va == 2:
            self.checkPWR_grun_va = self.LED_pass
        else:
            self.checkPWR_grun_va = self.LED_fail

        if self.checkP1_va == 2:
            self.checkP1_va = self.LED_pass
        else:
            self.checkP1_va = self.LED_fail
        
        if self.checkP2_va == 2:
            self.checkP2_va = self.LED_pass
        else:
            self.checkP2_va = self.LED_fail
        
        if self.checkP3_va == 2:
            self.checkP3_va = self.LED_pass
        else:
            self.checkP3_va = self.LED_fail

Please give me an example so i can learn and apply it to my program

Asked By: Ramas

||

Answers:

I would probably put the values in a dictionary or list and do something like

for i, val in enumerate(self.values):
    if val == 2:
        self.values[i] = self.LED_pass
    else:
        self.values[i] = self.LED_fail
Answered By: Byron

Gratuitious one-liner:

[
    setattr(
        self,
        "checkP" + attrib + "_va",
        self.LED_pass
        if getattr(self, "checkP" + attrib + "_va") == 2
        else self.LED_fail,
    )
    for attrib in ["WR_rot", "WR_grun", "1", "2", "3"]
]
Answered By: Josh Friedlander

Dispense with the entire if-else section and use a helper function when originally assigning attributes:

def checkbox_ledVA(self):
    self.LED = 'PASS'
    self.LED_fail = 'FAIL'

    pass_fail = lambda state: self.LED if state == 2 else self.LED_fail

    self.checkPWR_rot_va = pass_fail(self.ui.PWR_rot_VA.checkState())
    self.checkPWR_grun_va = pass_fail(self.ui.PWR_grun_VA.checkState())
    self.checkP1_va = pass_fail(self.ui.Port1_VA.checkState())
    self.checkP2_va = pass_fail(self.ui.port2_VA.checkState())
    self.checkP3_va = pass_fail(self.ui.Port3_VA.checkState())
Answered By: Steven Rumbalski
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.