0

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

1
  • 1
    Please update your question with the non-working code you have tried and the full error traceback. Commented Oct 19, 2022 at 12:21

3 Answers 3

2

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())
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

ja, that was very useful but the returned values still came as 2 or 0 instead of pass or fail. but thank you it was nice
1

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"]
]

2 Comments

It would be slightly less gratuitous as a two-liner with for attrib in ["WR_rot", "WR_grun", "1", "2", "3"]: on top rather than using a list-comprehension for side-effects only. Also it would display as one less line.
true, although this code could be displayed as one line. but I would never use it in real life! stripping away the common prefixes and suffixes is a crime against readability

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.