LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | List of all members
lsst.pex.config.dictField.DictField Class Reference
Inheritance diagram for lsst.pex.config.dictField.DictField:

Public Member Functions

def __init__
 
def validate
 
def __set__
 
def toDict
 

Public Attributes

 keytype
 
 itemtype
 
 dictCheck
 
 itemCheck
 

Static Public Attributes

 DictClass = Dict
 

Private Member Functions

def _compare
 

Detailed Description

Defines a field which is a mapping of values

Both key and item types are restricted to builtin POD types:
    (int, float, complex, bool, str)

Users can provide two check functions:
    dictCheck: used to validate the dict as a whole, and
    itemCheck: used to validate each item individually

For example to define a field which is a mapping from names to int values:

class MyConfig(Config):
    field = DictField(
            doc="example string-to-int mapping field", 
            keytype=str, itemtype=int, 
            default= {})

Definition at line 133 of file dictField.py.

Constructor & Destructor Documentation

def lsst.pex.config.dictField.DictField.__init__ (   self,
  doc,
  keytype,
  itemtype,
  default = None,
  optional = False,
  dictCheck = None,
  itemCheck = None 
)

Definition at line 154 of file dictField.py.

155  def __init__(self, doc, keytype, itemtype, default=None, optional=False, dictCheck=None, itemCheck=None):
156  source = traceback.extract_stack(limit=2)[0]
157  self._setup( doc=doc, dtype=Dict, default=default, check=None,
158  optional=optional, source=source)
159  if keytype not in self.supportedTypes:
160  raise ValueError("'keytype' %s is not a supported type"%\
161  _typeStr(keytype))
162  elif itemtype is not None and itemtype not in self.supportedTypes:
163  raise ValueError("'itemtype' %s is not a supported type"%\
164  _typeStr(itemtype))
165  if dictCheck is not None and not hasattr(dictCheck, "__call__"):
166  raise ValueError("'dictCheck' must be callable")
167  if itemCheck is not None and not hasattr(itemCheck, "__call__"):
168  raise ValueError("'itemCheck' must be callable")
170  self.keytype = keytype
171  self.itemtype = itemtype
172  self.dictCheck = dictCheck
173  self.itemCheck = itemCheck

Member Function Documentation

def lsst.pex.config.dictField.DictField.__set__ (   self,
  instance,
  value,
  at = None,
  label = "assignment" 
)

Definition at line 188 of file dictField.py.

189  def __set__(self, instance, value, at=None, label="assignment"):
190  if instance._frozen:
191  msg = "Cannot modify a frozen Config. "\
192  "Attempting to set field to value %s"%value
193  raise FieldValidationError(self, instance, msg)
194 
195  if at is None:
196  at = traceback.extract_stack()[:-1]
197  if value is not None:
198  value = self.DictClass(instance, self, value, at=at, label=label)
199  else:
200  history = instance._history.setdefault(self.name, [])
201  history.append((value, at, label))
202 
203  instance._storage[self.name] = value
def lsst.pex.config.dictField.DictField._compare (   self,
  instance1,
  instance2,
  shortcut,
  rtol,
  atol,
  output 
)
private
Helper function for Config.compare; used to compare two fields for equality.

@param[in] instance1  LHS Config instance to compare.
@param[in] instance2  RHS Config instance to compare.
@param[in] shortcut   If True, return as soon as an inequality is found.
@param[in] rtol       Relative tolerance for floating point comparisons.
@param[in] atol       Absolute tolerance for floating point comparisons.
@param[in] output     If not None, a callable that takes a string, used (possibly repeatedly)
              to report inequalities.

Floating point comparisons are performed by numpy.allclose; refer to that for details.

Definition at line 208 of file dictField.py.

209  def _compare(self, instance1, instance2, shortcut, rtol, atol, output):
210  """Helper function for Config.compare; used to compare two fields for equality.
211 
212  @param[in] instance1 LHS Config instance to compare.
213  @param[in] instance2 RHS Config instance to compare.
214  @param[in] shortcut If True, return as soon as an inequality is found.
215  @param[in] rtol Relative tolerance for floating point comparisons.
216  @param[in] atol Absolute tolerance for floating point comparisons.
217  @param[in] output If not None, a callable that takes a string, used (possibly repeatedly)
218  to report inequalities.
219 
220  Floating point comparisons are performed by numpy.allclose; refer to that for details.
221  """
222  d1 = getattr(instance1, self.name)
223  d2 = getattr(instance2, self.name)
224  name = getComparisonName(
225  _joinNamePath(instance1._name, self.name),
226  _joinNamePath(instance2._name, self.name)
227  )
228  if not compareScalars("isnone for %s" % name, d1 is None, d2 is None, output=output):
229  return False
230  if d1 is None and d2 is None:
231  return True
232  if not compareScalars("keys for %s" % name, d1.keys(), d2.keys(), output=output):
233  return False
234  equal = True
235  for k, v1 in d1.iteritems():
236  v2 = d2[k]
237  result = compareScalars("%s[%r]" % (name, k), v1, v2, dtype=self.itemtype,
238  rtol=rtol, atol=atol, output=output)
239  if not result and shortcut:
240  return False
241  equal = equal and result
242  return equal
def lsst.pex.config.dictField.DictField.toDict (   self,
  instance 
)

Definition at line 204 of file dictField.py.

205  def toDict(self, instance):
206  value = self.__get__(instance)
207  return dict(value) if value is not None else None
def lsst.pex.config.dictField.DictField.validate (   self,
  instance 
)
DictField validation ensures that non-optional fields are not None,
    and that non-None values comply with dictCheck.
Individual Item checks are applied at set time and are not re-checked.

Definition at line 174 of file dictField.py.

175  def validate(self, instance):
176  """
177  DictField validation ensures that non-optional fields are not None,
178  and that non-None values comply with dictCheck.
179  Individual Item checks are applied at set time and are not re-checked.
180  """
181  Field.validate(self, instance)
182  value = self.__get__(instance)
183  if value is not None and self.dictCheck is not None \
184  and not self.dictCheck(value):
185  msg = "%s is not a valid value"%str(value)
186  raise FieldValidationError(self, instance, msg)
187 

Member Data Documentation

lsst.pex.config.dictField.DictField.dictCheck

Definition at line 171 of file dictField.py.

lsst.pex.config.dictField.DictField.DictClass = Dict
static

Definition at line 152 of file dictField.py.

lsst.pex.config.dictField.DictField.itemCheck

Definition at line 172 of file dictField.py.

lsst.pex.config.dictField.DictField.itemtype

Definition at line 170 of file dictField.py.

lsst.pex.config.dictField.DictField.keytype

Definition at line 169 of file dictField.py.


The documentation for this class was generated from the following file: