Try to print a value of list by readblocking ,found some interesting

Can you help exlplain this ? if you already got A[0].value is Good ,then this code should print True , but I got False.

path=["default]Folder/Tag_A"]

A=system.tag.readBlocking(path)
print A[0].value  #this is Good
if A[0].value == 'Good':
	print True
else:
	print False

Try print type(A[0].value).

You'll find that Ignition scripting uses a lot of strange Jython class like types, in unexpected areas.

If I'm checking something like a string I always typecast it first just in case.

I wouldn't do that, that's how you end up with very hard to diagnose unexpected behaviors.
Type casting is something you should do with a purpose, when you know what type your variable is and what type you want it to be, and how casting it will affect it. Type casting "just in case" sounds like something that might come back later with a vengeance.

2 Likes

Stringification is also expensive, computationally. Only use stringification to show things to users.

If you have a quality code, perhaps from a qualified value, check for "good" with its .good native property. (A boolean, no equals comparison required.)

3 Likes

The print statement asks whatever expression you give it for a string representation. Qualified values happen to string represent in ways that look like they're regular strings, but they are not. Always print someVar, type(someVar) if you're not absolutely sure it's a string.

This would work, but to Phil's point:

Don't do this!

if str(A[0].value) == 'Good':

Instead, you should examine the quality attribute of the Qualified Value object you receive from readBlocking.

Do this!

if A[0].quality.good:

In the rare case you actually need to compare between two explicit quality codes, you should do it with the is function, e.g.

from com.inductiveautomation.ignition.common.model.values import QualityCode

uncertain = QualityCode.Uncertain_InitialValue

if A[0].quality.is(uncertain):
9 Likes

To add to what Phil and Paul have said, if you were trying to compare string values then if those values matched, you would have gotten what you expected. So obviously, something was not the value you expected it to be.

system.tag.writeBlocking(["[default]testString"],["Good"])
value = system.tag.readBlocking(["[default]testString"])[0].value

print value, type(value)
print value == "Good":

if value == "Good":
    print True
else:
    print False

system.tag.writeBlocking(["[default]testString"],["good"])
value = system.tag.readBlocking(["[default]testString")[0].value

print value, type(value)
print value == "good"

if value == "Good":
    print True
else:
    print False

Output:

>>> 
[Good]
Good <type 'unicode'>
True
True
[Good]
good <type 'unicode'>
True
False
>>> 

The good in square brackets is the Quality Code returned by system.tag.writeBlocking()

1 Like