Filter Alarm Status Table

Okay, first, posting only screen shots of code can be frustrating, you should post the formatted code. If you want to provide a screen shot to give more context that's great but you should always post any code in question as preformatted text.

See:

Second, you don't need to use the get() function, you can just reference the property directly.

Third, the comment above your conditional does not match what the logic is doing. It says that you want to display alarms without a matching display path, but the logic is doing the opposite of that.

Finally, your conditional can be cleaned up and optimized so you're minimizing the amount of time it takes to filter the table.

def filterAlarm(self, alarmEvent):

	# Retrieve the Grupo property from the alarm event
	grupo = alarmEvent.displayPath
	
	# Correctly retrieve the checkbox selections from the view's custom properties
	nodo1Selected = self.custom.Nodo1
	nodo3Selected = self.custom.Nodo3
	nodo5Selected = self.custom.Nodo5
	
	possibleGrupo = ['Nodo 1','Nodo 3','Nodo 5']
	# If no checkboxes are selected, show alarms with no Grupo value
	if not (any([nodo1Selected,nodo3Selected,nodo5Selected])) and grupo not in possibleGrupo:
		return True
	# Show alarms that match the selected nodes
	elif any([grupo == 'Nodo 1' and nodo1Selected,grupo == 'Nodo 3' and nodo3Selected,grupo == 'Nodo 5' and nodo5Selected]):
		return True
	return False

The use of the any() built-in allows for short-circuiting the evaluation, so as soon as a checkbox is seen as being true it will return instead of evaluating all options.

Once a return is evaluated, no code beyond that point will be processed, I have factored out all of your else return False statements to the end of the conditional. In python an else if condition is signaled by the elif keyword, so I have used here as well. These changes I believe make the code much more readable.

1 Like