24 from .config
import Field, _typeStr
27 __all__ = [
"RangeField"]
32 Defines a Config Field which allows only a range of values.
33 The range is defined by providing min and/or max values.
34 If min or max is None, the range will be open in that direction
35 If inclusive[Min|Max] is True the range will include the [min|max] value
39 supportedTypes = set((int, float))
41 def __init__(self, doc, dtype, default=None, optional=False,
42 min=
None, max=
None, inclusiveMin=
True, inclusiveMax=
False):
44 raise ValueError(
"Unsupported RangeField dtype %s" % (_typeStr(dtype)))
45 source = traceback.extract_stack(limit=2)[0]
46 if min
is None and max
is None:
47 raise ValueError(
"min and max cannot both be None")
49 if min
is not None and max
is not None:
51 raise ValueError(
"min = %s > %s = max" % (min, max))
52 elif min == max
and not (inclusiveMin
and inclusiveMax):
53 raise ValueError(
"min = max = %s and min and max not both inclusive" % (min,))
59 ((
"[" if inclusiveMin
else "("),
60 (
"-inf" if self.
min is None else self.
min),
61 (
"inf" if self.
max is None else self.
max),
62 (
"]" if inclusiveMax
else ")"))
65 self.
maxCheck =
lambda x, y:
True if y
is None else x <= y
67 self.
maxCheck =
lambda x, y:
True if y
is None else x < y
69 self.
minCheck =
lambda x, y:
True if y
is None else x >= y
71 self.
minCheck =
lambda x, y:
True if y
is None else x > y
72 self._setup(doc, dtype=dtype, default=default, check=
None, optional=optional, source=source)
74 def _validateValue(self, value):
75 Field._validateValue(self, value)
78 msg =
"%s is outside of valid range %s" % (value, self.
rangeString)