Package konval :: Module fxn
[hide private]
[frames] | no frames]

Source Code for Module konval.fxn

  1  """ 
  2  Various functions for using validators. 
  3   
  4  """ 
  5  # TODO: "or" validator 
  6   
  7   
  8  __docformat__ = "restructuredtext en" 
  9   
 10   
 11  ### IMPORTS 
 12   
 13  import exceptions 
 14   
 15  import impl 
 16   
 17  __all__ = [ 
 18          'sanitize', 
 19          'select', 
 20          'reject', 
 21          'assert_sanity', 
 22  ] 
 23   
 24   
 25  ### CONSTANTS & DEFINES 
 26   
 27  ### IMPLEMENTATION ### 
 28   
29 -def sanitize (val, validators):
30 """ 31 Check and/or convert a value, throwing an exception on failure. 32 33 :Parameters: 34 val 35 the original value to be validated and/or converted 36 validators 37 a validator or sequence of validators or suitable objects 38 39 :Returns: 40 the converted value, or original one if only validation has occurred 41 42 The core service method for konval, this can be used to check and convert 43 data. Note that this accepts a single value - if you want to sanitize a 44 whole list in the same way, use a list comprehension. 45 46 For example:: 47 48 >>> sanitize (1, int) 49 1 50 >>> from konval import IsEqualOrMore, ToLength 51 >>> sanitize ('2', [float, IsEqualOrMore(1)]) 52 2.0 53 >>> x = sanitize (['a', 'b'], [ToLength(), float, IsEqualOrMore(1)]) 54 >>> x 55 2.0 56 >>> sanitize (['a', 'b'], [ToLength(), float, IsEqualOrMore(3)]) 57 Traceback (most recent call last): 58 ... 59 ValueError: 2.0 is lower than 3 60 61 62 63 """ 64 for c in impl.make_list (validators): 65 val = c(val) 66 return val
67 68
69 -def select (vals, validators):
70 """ 71 Return those values that pass validation. 72 73 Note that the converted values are returned. 74 """ 75 selected = [] 76 for v in impl.make_list (vals): 77 try: 78 selected.append (sanitize (v, validators)) 79 except: 80 pass 81 return selected
82 83
84 -def reject (vals, validators):
85 """ 86 Return those values that fail validation. 87 88 Note that non-converted values are returned. 89 """ 90 rejected = [] 91 for v in impl.make_list (vals): 92 try: 93 sanitize (v, validators) 94 except: 95 rejected.append (v) 96 return rejected
97 98
99 -def assert_sanity (val, validators):
100 """ 101 Use validators for assertion. 102 103 This actually works much like `sanitize` other than converting errors to 104 AssertionErrors and serving as a signal of intent in code. Note that this 105 accepts a single value - If you want to sanitize a whole list in the same 106 way, use a list comprehension. 107 108 For example:: 109 110 >>> assert_sanity (1, int) 111 1 112 >>> from konval import IsEqualOrMore, ToLength 113 >>> x = assert_sanity ('2', [float, IsEqualOrMore(1)]) 114 >>> x 115 2.0 116 >>> assert_sanity (['a', 'b'], [ToLength(), float, IsEqualOrMore(3)]) 117 Traceback (most recent call last): 118 ... 119 AssertionError: 2.0 is lower than 3 120 121 """ 122 try: 123 return sanitize (val, validators) 124 except exceptions.Exception, err: 125 raise exceptions.AssertionError (str (err)) 126 except: 127 raise exceptions.AssertionError ("an error occurred when sanitizing '%s'" % val)
128 129 130 131 ## DEBUG & TEST ### 132 133 if __name__ == "__main__": 134 import doctest 135 doctest.testmod() 136 137 138 139 ### END ####################################################################### 140