You can go one step further in this case, and remove the brackets in
netTotal = sum([i.value for i in system.tag.readBlocking(pathsNet) if i.quality.good])
sum
works just fine with generator expressions, so
netTotal = sum(i.value for i in system.tag.readBlocking(pathsNet) if i.quality.good)
Will work on the expression without having to create the list.
The quality
attribute of qualified values has an isGood()
method that returns true if the quality is good. As @pturmel taught me yesterday, there's some kind of netbeans black magic that allows you to use .good
instead of .isGood()
, and that is apparently an optimization.
It's exactly the same thing as using tag.value
instead of tag.getValue()
or tag.quality
instead of tag.getQuality()
.
You can use .isGood()
if you find the meaning to be clearer (I do). I find it conveys its meaning best when written as tag.quality.isGood()
.
I'm not sure I understand, so let's go back to the code snippet:
[tag.value for tag in system.tag.readBlocking(paths)]
This will iterate through the list of tags returned by readBlocking
, get the value of each tag, and make a list out of them. You can add a condition to a comprehension, using this syntax:
[element for element in list if condition]
In your case, if you want the values of only the qualified values that have a good quality, then that what you check:
[qval.value for qval in qval_list if 'Good' in str(qval.quality)]
which can be simplified with
[qval.value for qval in qval_list if qval.quality.isGood()]
Edit:
Wait, I think I got it. No, the resulting list won't have the names of the tags. But the qualified values are still available WHILE iterating through them. If you 'unroll' the comprehension, it might be a bit clearer:
for tag in system.tag.readBlocking(paths):
if tag.quality.isGood():
values.append(tag.value)
If you want your list to retain the paths, use the other snippet I posted:
tags = [
{
'path': path,
'value': tag.value
} for path, tag in zip(paths, system.tag.readBlocking(paths)) if tag.quality.good
]
This will return a list of dictionaries containing two keys, one for the path, and the other for the value.