ValueChanged string format breaks

I Have a ValueChanged script on a tag and when string format function is within the code it completely breaks and fails to execute, not even the Gateway logs show any errors.

The following line wil make the ValueChange script unusable.

test = "{0}".format( "abcd" )

Do you get an error in the logs ?
Can we see the whole script ?

You basically cannot use modern python's .format() in tag events, as the curly braces are mis-interpreted. There is no work-around. Use classic % formatting instead.

Phil,

Could you show me a working example since I am not yet familiar with that method.

That's the full code what wil break the working. The entire code is irrelevant to the problem.

There is an error but not shown/visible to the client and the gateway does not log the issue.
I had to find out why the SaveChange did not execute as expected.

"%s" % "abcd"

I should also mention that you can avoid the problem by having your event be a one-liner that delegates to a function in the gateway scripting project's library. (You should do this for all events in Ignition anyways.)

6 Likes

Noo, it's just when using identifiers within the braces that it dies. Get rid of that 0 in the brackets and you're good to go

Ie

test = "{}".format( "abcd" )

Awfully fragile, and sets developers up for failure. Using the % operator is effectively bullet-proof, which is why I use it everywhere.

Not all that is new is not dumb.

1 Like

format is a perfectly good method though, I use it everywhere and have never run into an issue, albeit I don't normally use identifiers in them. The updates to it in python 3 make it much nicer to use with f strings though

The only issue with it within change events is IA's code to parse it which fails for named parameters

Hmmm? My point?

I am currently refactoring everything to create a complete UNS design and all functions are now placed in scripting (also great for testing).

In a very specific case I came across this piece and will migrate to a script, I did wanted to know why it broke.

Thank!