Package konval :: Module typeval :: Class StrToBool
[hide private]
[frames] | no frames]

Class StrToBool

source code

                 object --+        
                          |        
basevalidator.BaseValidator --+    
                              |    
              vocabval.Synonyms --+
                                  |
                                 StrToBool

Converts common abbreviations for true-false to boolean values.

This converts common abbreviations for true/false to actual Booleans. Case and flanking spaces are ignored. The allowed values are defined in defs. For example:

>>> v = StrToBool()
>>> v("True")
True
>>> v("f")
False
>>> v(" on ")
True
>>> v("0")
False
>>> v("yEs")
True
>>> v("maybe")
Traceback (most recent call last):
...
ValueError: can't recognise MAYBE' as true or false
Instance Methods [hide private]
 
__init__(self)
For example:
source code
 
convert_value(self, value)
Transform a value to the desired form.
source code
 
validate_value(self, value)
Check a value is of the desired form.
source code
 
make_validation_error_msg(self, bad_val, err)
Generate an error message for a validation problem.
source code

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

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)
(Constructor)

source code 

For example:

>>> d = {'foo': 1, 'bar': 2}
>>> v = Synonyms(d)
>>> v('foo')
1
>>> v('quux')
'quux'
Parameters:
  • d - a dictionary mapping input values to output values
Returns:
the mapped value or the original is no mapping available
Overrides: object.__init__
(inherited documentation)

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)

validate_value(self, value)

source code 

Check a value is of the desired form.

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

Parameters:
  • value - value to be checked
Returns:
success of validation.
Overrides: basevalidator.BaseValidator.validate_value
(inherited documentation)

make_validation_error_msg(self, bad_val, err)

source code 

Generate an error message for a validation problem.

Parameters as per raise_validation_error. Override in subclass if need be, for more specific and meaningful messages.

Overrides: basevalidator.BaseValidator.make_validation_error_msg
(inherited documentation)