Package konval :: Module vocabval :: Class Vocab
[hide private]
[frames] | no frames]

Class Vocab

source code

                 object --+    
                          |    
basevalidator.BaseValidator --+
                              |
                             Vocab

Ensure values fall within a fixed set.
Instance Methods [hide private]
 
__init__(self, vocab, canonize=False, allow_other=False)
For example:
source code
 
convert_value(self, value)
Transform a value to the desired form.
source code
 
make_conversion_error_msg(self, bad_val, err)
Generate an meaningful error message for a membership problem.
source code

Inherited from basevalidator.BaseValidator: __call__, convert, make_validation_error_msg, raise_conversion_error, raise_validation_error, validate, validate_value

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, vocab, canonize=False, allow_other=False)
(Constructor)

source code 

For example:

>>> d = ['foo', ['bar', 'baz'], 'quux']
>>> v = Vocab(d)
>>> v('foo')
'foo'
>>> v('bar')
'baz'
>>> v('corge')
Traceback (most recent call last):
...
ValueError: 'corge' is not a member of ['quux', 'foo', 'bar']
>>> v = Vocab(d, allow_other=True)
>>> v('corge')
'corge'
>>> v = Vocab(d, canonize=True, allow_other=True)
>>> v('foo')
'FOO'
>>> v('bar')
'BAZ'
>>> v('corge')
'CORGE'
Parameters:
  • vocab - a sequence of permitted values or value pairs (input and transformation)
  • canonize (bool) - should all values be transformed to a canonical form
  • allow_other (bool) - allow non-listed values
Returns:
the original value, mapped & canonized if supplied and requested
Overrides: object.__init__

convert_value(self, value)

source code 

Transform a value to the desired form.

This is the workhorse method that is called by convert to transform passed values. As such, errors are signalled by throwing a meaningful exception. This is one of the obvious and easiest places to customize behaviour by overriding in a subclass.

Parameters:
  • value - value to be transformed
Returns:
the transformed value
Overrides: basevalidator.BaseValidator.convert_value
(inherited documentation)

make_conversion_error_msg(self, bad_val, err)

source code 
Generate an meaningful error message for a membership problem.
Overrides: basevalidator.BaseValidator.make_conversion_error_msg