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
CodePudding user response:
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())
CodePudding user response:
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
CodePudding user response:
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"]
]