I have a function that looks if a value is bigger, smaller or equal zero and depending on the result returns one of three colours. Also if a certain attribute is present it doesn't return one of the three colours but a separate fourth on. I solved it like this:
def set_colour(x, trigger=False):
if x > 0.0:
colour = 'green'
elif x < 0.0:
colour = 'red'
else:
colour = 'black'
if trigger:
colour = 'blue'
return colour
But I don't like it. Is there a better way that is more elegant, efficient and pythonic? I found this post very interesting but can't use dictionaries since I am not checking for a static value but comparing the value.
Is there a possibility to define a variable for all numbers greater zero to test against in order to use the dictionary solution? Maybe with lambda? I tried some things but didn't get anywhere.
At the moment this solution that I have might be working okay but in future additional colours might be added for values smaller than -1.0, -2.0 or bigger 1.0 or 2.0 etc. and at that point the code will just get longer and longer for something that I feel can be solved more elegantly.
I think I check all relevant posts here so I hope this is not a duplicate.