\(\renewcommand\AA{\unicode{x212B}}\)

Validating InputΒΆ

By default there is no validation performed for input properties so any default value will be accepted by the algorithm.

Property validation can be added using the validator keyword within the declareProperty method. The validation is performed by an object and due to the restrictions placed on us by C++ we must know the type and pick the appropriate class for the job.

For the basic python types (int, float, string) the following validators are defined:

To use a validator, create it with the appropriate code as discussed above and pass it as the validator argument of declareProperty

from mantid.kernel import FloatBoundedValidator, StringListValidator, StringMandatoryValidator

...

def PyInit(self):
    # Force the value to be positive or zero
    self.declareProperty("Parameter", -1.0, FloatBoundedValidator(lower=0))

    # Require the user to provide a non-empty string as input
    self.declareProperty("Prefix", "", StringMandatoryValidator())

    # Require the property to have one of the listed values
    self.declareProperty("ProcessOption","Full",
                         StringListValidator(["Full", "QuickEstimate"]))

Validation can also be done when executing the algorithm by raising an exception if the value is invalid.

It should be preferred to validate in PyInit if possible as:

  1. The execution section can assume the values are valid

  2. The GUI can flag up errors sooner before the execute phase of the algorithm occurs