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

Source Code for Module konval.containerval

  1  """ 
  2  Validators that check value size or length. 
  3   
  4  """ 
  5   
  6  __docformat__ = "restructuredtext en" 
  7   
  8   
  9  ### IMPORTS 
 10   
 11  from basevalidator import BaseValidator 
 12   
 13   
 14  ### CONSTANTS & DEFINES 
 15   
 16  ### IMPLEMENTATION ### 
 17   
18 -class ToLength (BaseValidator):
19 """ 20 Convert a sequence to its length. 21 22 For example:: 23 24 >>> v = ToLength() 25 >>> v("abc") 26 3 27 >>> v([1, 2]) 28 2 29 30 """ 31
32 - def convert_value (self, value):
33 return len (value)
34 35
36 -class CheckLength (BaseValidator):
37 """ 38 Only allow values of a certain sizes. 39 40 Length limitations are expressed as (inclusive) minimum and maximum sizes. 41 This is most useful for strings, but could be used for lists. 42 43 For example:: 44 45 >>> v = CheckLength(min=2, max=4) 46 >>> v("abc") 47 'abc' 48 >>> v("abcde") #doctest: +ELLIPSIS 49 Traceback (most recent call last): 50 ... 51 ValueError: 'abcde' is longer than 4 52 >>> v("a") 53 Traceback (most recent call last): 54 ... 55 ValueError: 'a' is shorter than 2 56 >>> v = CheckLength(max=4) 57 >>> v("abc") 58 'abc' 59 >>> v("abcde") 60 Traceback (most recent call last): 61 ... 62 ValueError: 'abcde' is longer than 4 63 >>> v("a") 64 'a' 65 >>> v = CheckLength(min=2) 66 >>> v("abc") 67 'abc' 68 >>> v("abcde") 69 'abcde' 70 >>> v("a") 71 Traceback (most recent call last): 72 ... 73 ValueError: 'a' is shorter than 2 74 75 """
76 - def __init__ (self, min=None, max=None):
77 self.min = min 78 self.max = max
79
80 - def make_validation_error_msg (self, bad_val, err):
81 """ 82 Generate an meaningful error message for a length problem. 83 """ 84 if err: 85 return str (err) 86 else: 87 return BaseValidator.make_validation_error_msg (self, bad_val, err)
88 89
90 - def validate_value (self, value):
91 if self.min is not None: 92 assert self.min <= len (value), "'%s' is shorter than %s" % (value, self.min) 93 if self.max is not None: 94 assert len (value) <= self.max, "'%s' is longer than %s" % (value, self.max) 95 return True
96 97
98 -class IsEmpty(CheckLength):
99 """ 100 Checks the value is empty (an empty string, list, etc.) 101 102 For example:: 103 104 >>> v = IsEmpty() 105 >>> v("abc") 106 Traceback (most recent call last): 107 ... 108 ValueError: 'abc' is not empty 109 >>> v([]) 110 [] 111 112 """
113 - def __init__ (self):
114 CheckLength.__init__ (self, max=0)
115
116 - def make_validation_error_msg (self, bad_val, err):
117 """ 118 Generate an meaningful error message for an empty value. 119 """ 120 return "'%s' is not empty" % (bad_val)
121 122
123 -class IsNotEmpty(CheckLength):
124 """ 125 Checks the value is not empty (a nonblank string, list with items, etc.) 126 127 For example:: 128 129 >>> v = IsNotEmpty() 130 >>> v("abc") 131 'abc' 132 >>> v([]) 133 Traceback (most recent call last): 134 ... 135 ValueError: '[]' is empty 136 137 """
138 - def __init__ (self):
139 CheckLength.__init__ (self, min=1)
140
141 - def make_validation_error_msg (self, bad_val, err):
142 """ 143 Generate an meaningful error message for an empty value. 144 """ 145 return "'%s' is empty" % (bad_val)
146 147
148 -class IsMember (BaseValidator):
149 """ 150 Only allow values of a particular set. 151 152 Length limitations are expressed as (inclusive) minimum and maximum sizes. 153 This is most useful for strings, but could be used for lists. 154 155 For example:: 156 157 >>> v = IsMember([1, 2, 3]) 158 >>> v(1) 159 1 160 >>> v(4) 161 Traceback (most recent call last): 162 ... 163 ValueError: '4' is not a member of [1, 2, 3] 164 165 """
166 - def __init__ (self, vocab):
167 self.vocab = vocab
168
169 - def make_validation_error_msg (self, bad_val, err):
170 """ 171 Generate an meaningful error message for a membership problem. 172 """ 173 return "'%s' is not a member of %s" % (bad_val, self.vocab)
174
175 - def validate_value (self, value):
176 return value in self.vocab
177 178
179 -class ToIndex (BaseValidator):
180 """ 181 Convert to the index of the 182 183 Length limitations are expressed as (inclusive) minimum and maximum sizes. 184 This is most useful for strings, but could be used for lists. 185 186 For example:: 187 188 >>> v = ToIndex(['a', 'b', 'c']) 189 >>> v('a') 190 0 191 >>> v('d') 192 Traceback (most recent call last): 193 ... 194 ValueError: 'd' is not a member of ['a', 'b', 'c'] 195 196 """
197 - def __init__ (self, vocab):
198 self.vocab = vocab
199
200 - def make_conversion_error_msg (self, bad_val, err):
201 """ 202 Generate an meaningful error message for a membership problem. 203 """ 204 return "'%s' is not a member of %s" % (bad_val, self.vocab)
205
206 - def convert_value (self, value):
207 return self.vocab.index (value)
208 209 210 ## DEBUG & TEST ### 211 212 if __name__ == "__main__": 213 import doctest 214 doctest.testmod() 215 216 217 ### END ####################################################################### 218