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.configChoiceField.ConfigChoiceField Class Reference
Inheritance diagram for lsst.pex.config.configChoiceField.ConfigChoiceField:

Public Member Functions

def __init__
 
def __get__
 
def __set__
 
def rename
 
def validate
 
def toDict
 
def freeze
 
def save
 
def __deepcopy__
 

Public Attributes

 typemap
 
 multi
 

Static Public Attributes

 instanceDictClass = ConfigInstanceDict
 

Private Member Functions

def _getOrMake
 
def _compare
 

Detailed Description

ConfigChoiceFields allow the config to choose from a set of possible Config types.
The set of allowable types is given by the typemap argument to the constructor

The typemap object must implement typemap[name], which must return a Config subclass.

While the typemap is shared by all instances of the field, each instance of
the field has its own instance of a particular sub-config type

For example:

  class AaaConfig(Config):
    somefield = Field(int, "...")
  TYPEMAP = {"A", AaaConfig}
  class MyConfig(Config):
      choice = ConfigChoiceField("doc for choice", TYPEMAP)

  instance = MyConfig()
  instance.choice['AAA'].somefield = 5
  instance.choice = "AAA"

Alternatively, the last line can be written:
  instance.choice.name = "AAA"

Validation of this field is performed only the "active" selection.
If active is None and the field is not optional, validation will fail.

ConfigChoiceFields can allow single selections or multiple selections.
Single selection fields set selection through property name, and
multi-selection fields use the property names.

ConfigChoiceFields also allow multiple values of the same type:
  TYPEMAP["CCC"] = AaaConfig
  TYPEMAP["BBB"] = AaaConfig

When saving a config with a ConfigChoiceField, the entire set is saved, as well as the active selection

Definition at line 260 of file configChoiceField.py.

Constructor & Destructor Documentation

def lsst.pex.config.configChoiceField.ConfigChoiceField.__init__ (   self,
  doc,
  typemap,
  default = None,
  optional = False,
  multi = False 
)

Definition at line 299 of file configChoiceField.py.

300  def __init__(self, doc, typemap, default=None, optional=False, multi=False):
301  source = traceback.extract_stack(limit=2)[0]
302  self._setup( doc=doc, dtype=self.instanceDictClass, default=default, check=None, optional=optional, source=source)
303  self.typemap = typemap
304  self.multi = multi

Member Function Documentation

def lsst.pex.config.configChoiceField.ConfigChoiceField.__deepcopy__ (   self,
  memo 
)
Customize deep-copying, because we always want a reference to the original typemap.

WARNING: this must be overridden by subclasses if they change the constructor signature!

Definition at line 385 of file configChoiceField.py.

386  def __deepcopy__(self, memo):
387  """Customize deep-copying, because we always want a reference to the original typemap.
388 
389  WARNING: this must be overridden by subclasses if they change the constructor signature!
390  """
391  other = type(self)(doc=self.doc, typemap=self.typemap, default=copy.deepcopy(self.default),
392  optional=self.optional, multi=self.multi)
393  other.source = self.source
394  return other
def lsst.pex.config.configChoiceField.ConfigChoiceField.__get__ (   self,
  instance,
  owner = None 
)

Definition at line 317 of file configChoiceField.py.

318  def __get__(self, instance, owner=None):
319  if instance is None or not isinstance(instance, Config):
320  return self
321  else:
322  return self._getOrMake(instance)
def lsst.pex.config.configChoiceField.ConfigChoiceField.__set__ (   self,
  instance,
  value,
  at = None,
  label = "assignment" 
)

Definition at line 323 of file configChoiceField.py.

324  def __set__(self, instance, value, at=None, label="assignment"):
325  if instance._frozen:
326  raise FieldValidationError(self, instance, "Cannot modify a frozen Config")
327  if at is None:
328  at = traceback.extract_stack()[:-1]
329  instanceDict = self._getOrMake(instance)
330  if isinstance(value, self.instanceDictClass):
331  for k,v in value.iteritems():
332  instanceDict.__setitem__(k, v, at=at, label=label)
333  instanceDict._setSelection(value._selection, at=at, label=label)
334 
335  else:
336  instanceDict._setSelection(value, at=at, label=label)
def lsst.pex.config.configChoiceField.ConfigChoiceField._compare (   self,
  instance1,
  instance2,
  shortcut,
  rtol,
  atol,
  output 
)
private
Helper function for Config.compare; used to compare two fields for equality.

Only the selected config(s) are compared, as the parameters of any others do not matter.

@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 395 of file configChoiceField.py.

396  def _compare(self, instance1, instance2, shortcut, rtol, atol, output):
397  """Helper function for Config.compare; used to compare two fields for equality.
398 
399  Only the selected config(s) are compared, as the parameters of any others do not matter.
400 
401  @param[in] instance1 LHS Config instance to compare.
402  @param[in] instance2 RHS Config instance to compare.
403  @param[in] shortcut If True, return as soon as an inequality is found.
404  @param[in] rtol Relative tolerance for floating point comparisons.
405  @param[in] atol Absolute tolerance for floating point comparisons.
406  @param[in] output If not None, a callable that takes a string, used (possibly repeatedly)
407  to report inequalities.
408 
409  Floating point comparisons are performed by numpy.allclose; refer to that for details.
410  """
411  d1 = getattr(instance1, self.name)
412  d2 = getattr(instance2, self.name)
413  name = getComparisonName(
414  _joinNamePath(instance1._name, self.name),
415  _joinNamePath(instance2._name, self.name)
416  )
417  if not compareScalars("selection for %s" % name, d1._selection, d2._selection, output=output):
418  return False
419  if d1._selection is None:
420  return True
421  if self.multi:
422  nested = [(k, d1[k], d2[k]) for k in d1._selection]
423  else:
424  nested = [(d1._selection, d1[d1._selection], d2[d1._selection])]
425  equal = True
426  for k, c1, c2 in nested:
427  result = compareConfigs("%s[%r]" % (name, k), c1, c2, shortcut=shortcut,
428  rtol=rtol, atol=atol, output=output)
429  if not result and shortcut:
430  return False
431  equal = equal and result
432  return equal
def lsst.pex.config.configChoiceField.ConfigChoiceField._getOrMake (   self,
  instance,
  label = "default" 
)
private

Definition at line 305 of file configChoiceField.py.

306  def _getOrMake(self, instance, label="default"):
307  instanceDict = instance._storage.get(self.name)
308  if instanceDict is None:
309  at = traceback.extract_stack()[:-2]
310  instanceDict = self.dtype(instance, self)
311  instanceDict.__doc__ = self.doc
312  instance._storage[self.name] = instanceDict
313  history = instance._history.setdefault(self.name, [])
314  history.append(("Initialized from defaults", at, label))
315 
316  return instanceDict
def lsst.pex.config.configChoiceField.ConfigChoiceField.freeze (   self,
  instance 
)

Definition at line 370 of file configChoiceField.py.

371  def freeze(self, instance):
372  instanceDict = self.__get__(instance)
373  for v in instanceDict.itervalues():
374  v.freeze()
def lsst.pex.config.configChoiceField.ConfigChoiceField.rename (   self,
  instance 
)

Definition at line 337 of file configChoiceField.py.

338  def rename(self, instance):
339  instanceDict = self.__get__(instance)
340  fullname = _joinNamePath(instance._name, self.name)
341  instanceDict._rename(fullname)
def lsst.pex.config.configChoiceField.ConfigChoiceField.save (   self,
  outfile,
  instance 
)

Definition at line 375 of file configChoiceField.py.

376  def save(self, outfile, instance):
377  instanceDict = self.__get__(instance)
378  fullname = _joinNamePath(instance._name, self.name)
379  for v in instanceDict.itervalues():
380  v._save(outfile)
381  if self.multi:
382  print >> outfile, "%s.names=%r"%(fullname, instanceDict.names)
383  else:
384  print >> outfile, "%s.name=%r"%(fullname, instanceDict.name)
def lsst.pex.config.configChoiceField.ConfigChoiceField.toDict (   self,
  instance 
)

Definition at line 354 of file configChoiceField.py.

355  def toDict(self, instance):
356  instanceDict = self.__get__(instance)
357 
358  dict_ = {}
359  if self.multi:
360  dict_["names"]=instanceDict.names
361  else:
362  dict_["name"] =instanceDict.name
363 
364  values = {}
365  for k, v in instanceDict.iteritems():
366  values[k]=v.toDict()
367  dict_["values"]=values
368 
369  return dict_
def lsst.pex.config.configChoiceField.ConfigChoiceField.validate (   self,
  instance 
)

Definition at line 342 of file configChoiceField.py.

343  def validate(self, instance):
344  instanceDict = self.__get__(instance)
345  if instanceDict.active is None and not self.optional:
346  msg = "Required field cannot be None"
347  raise FieldValidationError(self, instance, msg)
348  elif instanceDict.active is not None:
349  if self.multi:
350  for a in instanceDict.active:
351  a.validate()
352  else:
353  instanceDict.active.validate()

Member Data Documentation

lsst.pex.config.configChoiceField.ConfigChoiceField.instanceDictClass = ConfigInstanceDict
static

Definition at line 298 of file configChoiceField.py.

lsst.pex.config.configChoiceField.ConfigChoiceField.multi

Definition at line 303 of file configChoiceField.py.

lsst.pex.config.configChoiceField.ConfigChoiceField.typemap

Definition at line 302 of file configChoiceField.py.


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