Problem with an if statement in a script

I have a simple script bound to the text of a label.

This is the result that I am getting:

image

1 Like

What type of value is {self.view.params.ShowManualUpDown} ?

I would try to explicitly check the value of the param

if self.view.params.ShowManualUpDown==True:

image

    text = 'ShowManualUpDown is '
    text = text + str(self.view.params.ShowManualUpDown) + '. '
    if self.view.params.ShowManualUpDown == True:
        text = text + 'It took the true path.'
    else:
        text = 'It took the false path.'
    return text

Gives the same result.
self.view.params.ShowManualUpDown) + '. '
    if self.view.params.ShowManualUpDown

You're reading the property two times. Something else is acting on it in between. This is a data race.
If you want the value to be constant, bind it to a local variable:

text = 'ShowManualUpDown is '
manualValue = self.view.params.ShowManualUpDown
text = text + str(manualValue) + '. '
if manualValue == True:
    text = text + 'It took the true path.'
else:
    text = 'It took the false path.'
return text
def transform(self, value, quality, timestamp):
    tmp = ( self.view.params.ShowManualUpDown)
    text = 'ShowManualUpDown is '
    text = text + str( tmp) + '. '
    if tmp:
        #text = UP_SP_LIFT_Status_Text.State(value, False, False)    
        text = text + 'It took the true path.'
    else:
        #text = UP_SP_STD_Status_Text.State(value, False, False)
        text = 'It took the false path.'
    return text


image

Same result.

Are you actually passing a boolean to the parameter? Perhaps a string is being passed and then coerced into a boolean during the check.

If ShowManualUpDown was an actual boolean, str(ShowManualUpDown) would be “False” not “false.” If it’s the string “false” then it would evaluate as True in a boolean expression.

3 Likes

That’s the problem. It is being passed in as string and not a Boolean.

I am invoking it with:

That is passing it as a string.

Thank you everyone for your help and suggestions on this.

You're over cookng this one. You shouldn't be using a script transform at all here to do a simple, what turns out to be, text concatenation. Script transforms are a last resort if you can't do it another way as they require more resources and time to process.

This should use an expression binding with:

'ShowManualUpDown is ' + toStr({self.view.params.ShowManualUpDown}) + '. It took the' +({self.view.params.ShowManualUpDown} = True) + ' path' 

Edit: Oh wait I think I missed the point haha. It was obviously a test, but I didn't appreciate that it was a test checking the data type of that param.

However, my comment still stands about using expressions over script transforms.