Class Validator
source code
Validator is an object that allows you to register a set of 'checks'.
These checks take input and test that it conforms to the check.
This can also involve converting the value from a string into
the correct datatype.
The check method takes an input string which configures which
check is to be used and applies that check to a supplied value.
An example input string would be:
'int_range(param1, param2)'
You would then provide something like:
>>> def int_range_check(value, min, max):
...
... min = int(min)
... max = int(max)
...
...
...
... if not isinstance(value, (int, long, StringTypes)):
... raise VdtTypeError(value)
... elif isinstance(value, StringTypes):
...
...
... try:
... value = int(value)
... except ValueError:
... raise VdtValueError(value)
...
... if not min <= value:
... raise VdtValueTooSmallError(value)
... if not value <= max:
... raise VdtValueTooBigError(value)
... return value
>>> fdict = {'int_range': int_range_check}
>>> vtr1 = Validator(fdict)
>>> vtr1.check('int_range(20, 40)', '30')
30
>>> vtr1.check('int_range(20, 40)', '60')
Traceback (most recent call last):
VdtValueTooBigError: the value "60" is too big.
New functions can be added with :
>>> vtr2 = Validator()
>>> vtr2.functions['int_range'] = int_range_check
Or by passing in a dictionary of functions when Validator
is instantiated.
Your functions can use keyword arguments,
but the first argument should always be 'value'.
If the function doesn't take additional arguments,
the parentheses are optional in the check.
It can be written with either of :
keyword = function_name
keyword = function_name()
The first program to utilise Validator() was Michael Foord's
ConfigObj, an alternative to ConfigParser which supports lists and
can validate a config file using a config schema.
For more details on using Validator with ConfigObj see:
http://www.voidspace.org.uk/python/configobj.html
|
__init__(self,
functions=' frontend ' )
x.__init__(...) initializes x; see x.__class__.__doc__ for
signature |
source code
|
|
|
|
|
_unquote(self,
val)
Unquote a value if necessary. |
source code
|
|
|
_list_handle(self,
listmatch)
Take apart a keyword=list('val, 'val') type string. |
source code
|
|
|
_pass(self,
value)
Dummy check that always passes |
source code
|
|
Inherited from object :
__delattr__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__str__
|
|
_func_re = re.compile(r'( .+? ) \(( .* ) \)')
|
|
_key_arg = re.compile(r'^( [ a- zA- Z_] [ a- zA- Z0- 9_] * ) \s* =\s* ( .* ) $')
|
|
_list_arg = re.compile(r'(?x) (?: ( [ a- zA- Z_] [ a- zA- Z0- 9_] * ) \s* =\s...
|
|
_list_members = re.compile(r'(?x) ( (?: ".*? ") | (?: \'.*? \') | (?: [ ^ \...
|
|
_paramfinder = re.compile(r'(?x) (?: ( (?: [ a- zA- Z_] [ a- zA- Z0- 9_] * \...
|
|
_matchfinder = re.compile(r'(?x) ^(?: ( (?: [ a- zA- Z_] [ a- zA- Z0- 9_] * ...
|
Inherited from object :
__class__
|
__init__(self,
functions=' frontend ' )
(Constructor)
| source code
|
>>> vtri = Validator()
- Overrides:
object.__init__
|
Usage: check(check, value)
- Arguments:
- check: string representing check to apply (including arguments)
value: object to be checked
Returns value, converted to correct type if necessary
If the check fails, raises a ValidateError subclass.
>>> vtor.check('yoda', '')
Traceback (most recent call last):
VdtUnknownCheckError: the check "yoda" is unknown.
>>> vtor.check('yoda()', '')
Traceback (most recent call last):
VdtUnknownCheckError: the check "yoda" is unknown.
|
_list_arg
- Value:
re.compile(r'(?x) (?: ( [ a- zA- Z_] [ a- zA- Z0- 9_] * ) \s* =\s* list\(( (?: \s* (?: (?:
".*? ") | (?: \'.*? \') | (?: [ ^ \'",\s\)] [ ^ ,\)] *? ) ) \s* ,\s* ) * (?: (?: ".*? ") | (?: \'
.*? \') | (?: [ ^ \'",\s\)] [ ^ ,\)] *? ) ) ? ) \)) ')
|
|
_list_members
- Value:
re.compile(r'(?x) ( (?: ".*? ") | (?: \'.*? \') | (?: [ ^ \'",\s=] [ ^ ,=] *? ) ) (?: (?: \s
* ,\s* ) | (?: \s* $) ) ')
|
|
_paramfinder
- Value:
re.compile(r'(?x) (?: ( (?: [ a- zA- Z_] [ a- zA- Z0- 9_] * \s* =\s* list\((?: \s* (?: (?
: ".*? ") | (?: \'.*? \') | (?: [ ^ \'",\s\)] [ ^ ,\)] *? ) ) \s* ,\s* ) * (?: (?: ".*? ") | (?: \
'.*? \') | (?: [ ^ \'",\s\)] [ ^ ,\)] *? ) ) ? \)) | (?: (?: ".*? ") | (?: \'.*? \') | (?: [ ^ \'"
,\s=] [ ^ ,=] *? ) | (?: [ a- zA- Z_] [ a- zA- Z0- 9_] * \s* =\s* (?: (?: ".*? ") | (?: \'.*? \')
| (?: [ ^ \'",\s=] [ ^ ,=] *? ) ) ) ) ) (?: (?: \s* ,\s* ) | (?: \s* $) ) ) ')
|
|
_matchfinder
- Value:
re.compile(r'(?x) ^(?: ( (?: [ a- zA- Z_] [ a- zA- Z0- 9_] * \s* =\s* list\((?: \s* (?: (
?: ".*? ") | (?: \'.*? \') | (?: [ ^ \'",\s\)] [ ^ ,\)] *? ) ) \s* ,\s* ) * (?: (?: ".*? ") | (?:
\'.*? \') | (?: [ ^ \'",\s\)] [ ^ ,\)] *? ) ) ? \)) | (?: (?: ".*? ") | (?: \'.*? \') | (?: [ ^ \'
",\s=] [ ^ ,=] *? ) | (?: [ a- zA- Z_] [ a- zA- Z0- 9_] * \s* =\s* (?: (?: ".*? ") | (?: \'.*? \'
) | (?: [ ^ \'",\s=] [ ^ ,=] *? ) ) ) ) ) (?: (?: \s* ,\s* ) | (?: \s* $) ) ) * ')
|
|