lsst.pex.config  18.1.0-2-g919ecaf
rangeField.py
Go to the documentation of this file.
1 # This file is part of pex_config.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (http://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 __all__ = ["RangeField"]
23 
24 from .config import Field, _typeStr
25 from .callStack import getStackFrame
26 
27 
29  """A configuration field (`lsst.pex.config.Field` subclass) that requires
30  the value to be in a specific numeric range.
31 
32  Parameters
33  ----------
34  doc : `str`
35  A description of the field.
36  dtype : {`int`-type, `float`-type}
37  The field's data type: either the `int` or `float` type.
38  default : `int` or `float`, optional
39  Default value for the field.
40  optional : `bool`, optional
41  When `False`, `lsst.pex.config.Config.validate` will fail if the
42  field's value is `None`.
43  min : int, float, or `None`, optional
44  Minimum value accepted in the range. If `None`, the range has no
45  lower bound (equivalent to negative infinity).
46  max : `int`, `float`, or None, optional
47  Maximum value accepted in the range. If `None`, the range has no
48  upper bound (equivalent to positive infinity).
49  inclusiveMin : `bool`, optional
50  If `True` (default), the ``min`` value is included in the allowed
51  range.
52  inclusiveMax : `bool`, optional
53  If `True` (default), the ``max`` value is included in the allowed
54  range.
55  deprecated : None or `str`, optional
56  A description of why this Field is deprecated, including removal date.
57  If not None, the string is appended to the docstring for this Field.
58 
59  See also
60  --------
61  ChoiceField
62  ConfigChoiceField
63  ConfigDictField
64  ConfigField
65  ConfigurableField
66  DictField
67  Field
68  ListField
69  RegistryField
70  """
71 
72  supportedTypes = set((int, float))
73  """The set of data types allowed by `RangeField` instances (`set`
74  containing `int` and `float` types).
75  """
76 
77  def __init__(self, doc, dtype, default=None, optional=False,
78  min=None, max=None, inclusiveMin=True, inclusiveMax=False,
79  deprecated=None):
80  if dtype not in self.supportedTypes:
81  raise ValueError("Unsupported RangeField dtype %s" % (_typeStr(dtype)))
82  source = getStackFrame()
83  if min is None and max is None:
84  raise ValueError("min and max cannot both be None")
85 
86  if min is not None and max is not None:
87  if min > max:
88  raise ValueError("min = %s > %s = max" % (min, max))
89  elif min == max and not (inclusiveMin and inclusiveMax):
90  raise ValueError("min = max = %s and min and max not both inclusive" % (min,))
91 
92  self.min = min
93  """Minimum value accepted in the range. If `None`, the range has no
94  lower bound (equivalent to negative infinity).
95  """
96 
97  self.max = max
98  """Maximum value accepted in the range. If `None`, the range has no
99  upper bound (equivalent to positive infinity).
100  """
101 
102  if inclusiveMax:
103  self.maxCheck = lambda x, y: True if y is None else x <= y
104  else:
105  self.maxCheck = lambda x, y: True if y is None else x < y
106  if inclusiveMin:
107  self.minCheck = lambda x, y: True if y is None else x >= y
108  else:
109  self.minCheck = lambda x, y: True if y is None else x > y
110  self._setup(doc, dtype=dtype, default=default, check=None, optional=optional, source=source,
111  deprecated=deprecated)
112  self.rangeString = "%s%s,%s%s" % \
113  (("[" if inclusiveMin else "("),
114  ("-inf" if self.min is None else self.min),
115  ("inf" if self.max is None else self.max),
116  ("]" if inclusiveMax else ")"))
117  """String representation of the field's allowed range (`str`).
118  """
119 
120  self.__doc__ += "\n\nValid Range = " + self.rangeString
121 
122  def _validateValue(self, value):
123  Field._validateValue(self, value)
124  if not self.minCheck(value, self.min) or \
125  not self.maxCheck(value, self.max):
126  msg = "%s is outside of valid range %s" % (value, self.rangeString)
127  raise ValueError(msg)
def __init__(self, doc, dtype, default=None, optional=False, min=None, max=None, inclusiveMin=True, inclusiveMax=False, deprecated=None)
Definition: rangeField.py:79
def getStackFrame(relative=0)
Definition: callStack.py:51
def _setup(self, doc, dtype, default, check, optional, source, deprecated)
Definition: config.py:278