Package konval :: Module stringval :: Class ToCanonical
[hide private]
[frames] | no frames]

Class ToCanonical

source code

                 object --+    
                          |    
basevalidator.BaseValidator --+
                              |
                             ToCanonical

Reduce strings to a canonical form.

A common problem in cleaning user input is to catch trivial variants, e.g. how to recognise 'foo-bar', 'Foo-bar', ' foo-bar ' and 'foo_bar' as being the same value. This function achieves that by stripping flanking spaces, converting letters to uppercase and converting internal stretches of spaces, underscores and hyphens to a single underscore. Thus, all of the previous values would be converted to 'FOO_BAR'.

For example:

>>> v = ToCanonical()
>>> v ('aBcD')
'ABCD'
>>> v ('  ab cd_')
'AB_CD_'
>>> v ('AB-_ CD')
'AB_CD'
Instance Methods [hide private]
 
convert_value(self, value)
Transform a value to the desired form.
source code

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

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

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

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)