3

How can I stop QGIS from printing True twice in python console for each updated record? This is slowing down the calculations.

Example:

layer = iface.activeLayer()
with edit(layer):
    for feature in layer.getFeatures():
        if feature['Randint'] % 2 == 0:
            new_value =  'Even'
        else:
            new_value = 'Odd'
        feature.setAttribute(feature.fieldNameIndex('Parity'), new_value)
        layer.updateFeature(feature)

True*10000: enter image description here

1
  • 1
    simple workaround, change last line to _ = layer.updateFeature(feature) and the same for setAttribute _ = feature.setAttribute(feature.fieldNameIndex('Parity'), new_value)
    – xlDias
    Sep 6, 2018 at 17:52

1 Answer 1

1

The functions setAttribute and updateFeature both return a boolean (indicating whether or not the operation was successful). Since you are executing from the terminal, if you do not use this returned value, the value will return to the console, that is why assigning it to a variable result in it not being printed anymore.

For example:

_ = feature.setAttribute(feature.fieldNameIndex('Parity'), new_value)
_ = layer.updateFeature(feature)

Note that the variable name does not need to be an underscore, I used it because I believe it makes clear that it is a variable that has no use for the script itself.

And, as the OP mentioned in the comment, print statements make things a lot slower

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.