Package qanda :: Module validators
[hide private]
[frames] | no frames]

Module validators

source code

Prompting the users for, and validating, answers.

In qanda, answers from a user may be processed through a list of validators. This follows the idiom of Ian Bicking & FormEncode where validation and conversion are one and the same: raw values are passed into a converter and the results are passed into the next. Should an exception be raised, conversion is halted, an error message printed (based on the exception message) and the question posed again.

Additional validators are easy to construct. The minimum interface they need is to be callable with a value and to return a (possibly transformed) value, optionally throwing an exception if the value is not valid. Thus, type constructors can be used as validators:

prompt.string ("Give me a float", converters=[float])

More complex validators can be derived from a supplied base class. BaseValidator supplies three methods for overriding and customising validator behaviour: __call__, convert and validate. Custom validators should only need subclass one of these methods and perhaps supply a c'tor.

Classes [hide private]
  BaseValidator
A base class for custom validators.
  Clean
Normalize values by stripping flanking space and converting to lower case.
  Synonyms
Map values to other values.
  Vocab
Ensure values fall within a fixed set.
  Nonblank
Only allow non-blank strings (i.e. those with a length more than 0).
  Regex
Only allow values that match a certain regular expression.
  Range
Only allow values between certain inclusive bounds.
  ToInt
Convert a value to an integer.
  ToFloat
Convert a value to a float.
  Length
Only allow values of a certain sizes.
Variables [hide private]
  __package__ = 'qanda'