Creating Parameters¶
If the built in parameters
don’t suite your needs, you can create your own, custom
parameters. Creating a parameter involves subclassing Parameter
or another parameter class,
and overriding the clean()
method to validate and (if necessary) transform the values
passed to an argument of that type. If the incoming value is invalid, the clean
method should raise an exception
(see mpilot.exceptions
).
from mpilot.params import StringParameter
class FooBarParameter(StringParameter):
def clean(self, value, program=None, lineno=None):
if value.lower() not in ('foo', 'bar'):
raise ParameterNotValid(value, "FooBar", lineno=lineno)
return value
This parameter extends StringParameter
, restricts it to accept only “foo” or “bar” as
a valid value, and returns valid, incoming values unmodified.
Parameter Options¶
You can add extra configuration options to your parameter by implementing __init__
. Let’s modify the above example
to accept a list of valid values, rather than the hard-coded “foo” and “bar”.
class FooBarParameter(StringParameter):
def __init__(self, valid=('foo', 'bar'), **kwargs):
super(StringParameter, self).__init__(**kwargs)
self.valid = valid
def clean(self, value, program=None, lineno=None):
if value.lower() not in self.valid:
raise ParameterNotValid(value, "FooBar", lineno=lineno)
return value
When using this parameter in a command, now we could now provide different valid values.
class MyCommand(Command):
inputs = {
Speed = FooBarParameter(valid=('fast', 'slow'))
}