|
9 | 9 |
|
10 | 10 | class TooShort(Failure, ValueError): |
11 | 11 | """ Custom Failure raised by minlen """ |
12 | | - def __init__(self, wrong_value, min_length, strict): |
13 | | - symbol = '>' if strict else '>=' |
14 | | - help_msg = 'len(x) {symbol} {min_length} does not hold for x={wrong_value}' |
15 | | - super(TooShort, self).__init__(wrong_value=wrong_value, min_length=min_length, symbol=symbol, help_msg=help_msg) |
| 12 | + def __init__(self, wrong_value, min_length): |
| 13 | + help_msg = 'len(x) >= {min_length} does not hold for x={wrong_value}' |
| 14 | + super(TooShort, self).__init__(wrong_value=wrong_value, min_length=min_length, help_msg=help_msg) |
16 | 15 |
|
17 | 16 |
|
18 | | -def minlen(min_length, |
19 | | - strict=False # type: bool |
| 17 | +def minlen(min_length |
20 | 18 | ): |
21 | 19 | """ |
22 | 20 | 'Minimum length' validation_function generator. |
23 | | - Returns a validation_function to check that len(x) >= min_length (strict=False, default) |
24 | | - or len(x) > min_length (strict=True) |
| 21 | + Returns a validation_function to check that len(x) >= min_length |
25 | 22 |
|
26 | 23 | :param min_length: minimum length for x |
27 | | - :param strict: Boolean flag to switch between len(x) >= min_length (strict=False) and len(x) > min_length |
28 | | - (strict=True) |
29 | 24 | :return: |
30 | 25 | """ |
31 | | - if strict: |
32 | | - def minlen_(x): |
33 | | - if len(x) > min_length: |
34 | | - return True |
35 | | - else: |
36 | | - # raise Failure('minlen: len(x) > ' + str(min_length) + ' does not hold for x=' + str(x)) |
37 | | - raise TooShort(wrong_value=x, min_length=min_length, strict=True) |
38 | | - else: |
39 | | - def minlen_(x): |
40 | | - if len(x) >= min_length: |
41 | | - return True |
42 | | - else: |
43 | | - # raise Failure('minlen: len(x) >= ' + str(min_length) + ' does not hold for x=' + str(x)) |
44 | | - raise TooShort(wrong_value=x, min_length=min_length, strict=False) |
| 26 | + def minlen_(x): |
| 27 | + if len(x) >= min_length: |
| 28 | + return True |
| 29 | + else: |
| 30 | + raise TooShort(wrong_value=x, min_length=min_length) |
45 | 31 |
|
46 | | - minlen_.__name__ = 'length_{}greater_than_{}'.format('strictly_' if strict else '', min_length) |
| 32 | + minlen_.__name__ = 'length_greater_than_%s' % min_length |
47 | 33 | return minlen_ |
48 | 34 |
|
49 | 35 |
|
50 | | -def minlens(min_length_strict): |
51 | | - """ Alias for 'Minimum length' validation_function generator in strict mode """ |
52 | | - return minlen(min_length_strict, True) |
53 | | - |
54 | | - |
55 | 36 | class TooLong(Failure, ValueError): |
56 | 37 | """ Custom Failure raised by maxlen """ |
57 | | - def __init__(self, wrong_value, max_length, strict): |
58 | | - symbol = '<' if strict else '<=' |
59 | | - help_msg = 'len(x) {symbol} {max_length} does not hold for x={wrong_value}' |
60 | | - super(TooLong, self).__init__(wrong_value=wrong_value, max_length=max_length, symbol=symbol, help_msg=help_msg) |
| 38 | + def __init__(self, wrong_value, max_length): |
| 39 | + help_msg = 'len(x) <= {max_length} does not hold for x={wrong_value}' |
| 40 | + super(TooLong, self).__init__(wrong_value=wrong_value, max_length=max_length, help_msg=help_msg) |
61 | 41 |
|
62 | 42 |
|
63 | 43 | def maxlen(max_length, |
64 | | - strict=False # type: bool |
65 | 44 | ): |
66 | 45 | """ |
67 | 46 | 'Maximum length' validation_function generator. |
68 | | - Returns a validation_function to check that len(x) <= max_length (strict=False, default) or len(x) < max_length (strict=True) |
| 47 | + Returns a validation_function to check that len(x) <= max_length |
69 | 48 |
|
70 | 49 | :param max_length: maximum length for x |
71 | | - :param strict: Boolean flag to switch between len(x) <= max_length (strict=False) and len(x) < max_length |
72 | | - (strict=True) |
73 | 50 | :return: |
74 | 51 | """ |
75 | | - if strict: |
76 | | - def maxlen_(x): |
77 | | - if len(x) < max_length: |
78 | | - return True |
79 | | - else: |
80 | | - # raise Failure('maxlen: len(x) < ' + str(max_length) + ' does not hold for x=' + str(x)) |
81 | | - raise TooLong(wrong_value=x, max_length=max_length, strict=True) |
82 | | - else: |
83 | | - def maxlen_(x): |
84 | | - if len(x) <= max_length: |
85 | | - return True |
86 | | - else: |
87 | | - # raise Failure('maxlen: len(x) <= ' + str(max_length) + ' does not hold for x=' + str(x)) |
88 | | - raise TooLong(wrong_value=x, max_length=max_length, strict=False) |
| 52 | + def maxlen_(x): |
| 53 | + if len(x) <= max_length: |
| 54 | + return True |
| 55 | + else: |
| 56 | + # raise Failure('maxlen: len(x) <= ' + str(max_length) + ' does not hold for x=' + str(x)) |
| 57 | + raise TooLong(wrong_value=x, max_length=max_length) |
89 | 58 |
|
90 | | - maxlen_.__name__ = 'length_{}lesser_than_{}'.format('strictly_' if strict else '', max_length) |
| 59 | + maxlen_.__name__ = 'length_lesser_than_%s' % max_length |
91 | 60 | return maxlen_ |
92 | 61 |
|
93 | 62 |
|
94 | | -def maxlens(max_length_strict): |
95 | | - """ Alias for 'Maximum length' validation_function generator in strict mode """ |
96 | | - return maxlen(max_length_strict, True) |
97 | | - |
98 | | - |
99 | 63 | class WrongLength(Failure, ValueError): |
100 | 64 | """ Custom failure raised by has_length """ |
101 | 65 | def __init__(self, wrong_value, ref_length): |
|
0 commit comments