1 """
2 Validators that check value size or length.
3
4 """
5
6 __docformat__ = "restructuredtext en"
7
8
9
10
11 from basevalidator import BaseValidator
12
13
14
15
16
17
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
34
35
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
88
89
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
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 """
115
117 """
118 Generate an meaningful error message for an empty value.
119 """
120 return "'%s' is not empty" % (bad_val)
121
122
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 """
140
142 """
143 Generate an meaningful error message for an empty value.
144 """
145 return "'%s' is empty" % (bad_val)
146
147
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 """
168
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
176 return value in self.vocab
177
178
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 """
199
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
207 return self.vocab.index (value)
208
209
210
211
212 if __name__ == "__main__":
213 import doctest
214 doctest.testmod()
215
216
217
218