LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
Public Member Functions | Properties | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
lsst.pex.config.config.Config Class Reference
Inheritance diagram for lsst.pex.config.config.Config:

Public Member Functions

def __iter__
 
def keys
 
def values
 
def items
 
def iteritems
 
def itervalues
 
def iterkeys
 
def __contains__
 
def __new__
 
def __reduce__
 
def setDefaults
 
def update
 
def load
 
def loadFromStream
 
def save
 
def saveToStream
 
def freeze
 
def toDict
 
def validate
 
def formatHistory
 
def __setattr__
 
def __delattr__
 
def __eq__
 
def __ne__
 
def __str__
 
def __repr__
 
def compare
 

Properties

 history = property(lambda x: x._history)
 

Private Member Functions

def _save
 
def _rename
 

Private Attributes

 _frozen
 
 _name
 

Static Private Attributes

 __metaclass__ = ConfigMeta
 

Detailed Description

Base class for control objects.

A Config object will usually have several Field instances as class 
attributes; these are used to define most of the base class behavior.  
Simple derived class should be able to be defined simply by setting those 
attributes.

Definition at line 393 of file config.py.

Member Function Documentation

def lsst.pex.config.config.Config.__contains__ (   self,
  name 
)
Determines whether the field 'name' exists in this config

Definition at line 444 of file config.py.

445  def __contains__(self, name):
446  """
447  Determines whether the field 'name' exists in this config
448  """
449  return self._storage.__contains__(name)
def lsst.pex.config.config.Config.__delattr__ (   self,
  attr,
  at = None,
  label = "deletion" 
)

Definition at line 684 of file config.py.

685  def __delattr__(self, attr, at=None, label="deletion"):
686  if attr in self._fields:
687  if at is None:
688  at=traceback.extract_stack()[:-1]
689  self._fields[attr].__delete__(self, at=at, label=label)
690  else:
691  object.__delattr__(self, attr)
def lsst.pex.config.config.Config.__eq__ (   self,
  other 
)

Definition at line 692 of file config.py.

693  def __eq__(self, other):
694  if type(other) == type(self):
695  for name in self._fields:
696  thisValue = getattr(self, name)
697  otherValue = getattr(other, name)
698  if isinstance(thisValue, float) and math.isnan(thisValue):
699  if not math.isnan(otherValue):
700  return False
701  elif thisValue != otherValue:
702  return False
703  return True
704  return False
def lsst.pex.config.config.Config.__iter__ (   self)
Enable iterating over a config allows inspection of a Config's fields,
so that a Config behaves like a dict mapping field names to field
descriptors

Definition at line 404 of file config.py.

405  def __iter__(self):
406  """
407  Enable iterating over a config allows inspection of a Config's fields,
408  so that a Config behaves like a dict mapping field names to field
409  descriptors
410  """
411  return self._fields.__iter__()
def lsst.pex.config.config.Config.__ne__ (   self,
  other 
)

Definition at line 705 of file config.py.

706  def __ne__(self, other):
707  return not self.__eq__(other)
def lsst.pex.config.config.Config.__new__ (   cls,
  args,
  kw 
)
Allocate a new Config object.

In order to ensure that all Config object are always in a proper
state when handed to users or to derived Config classes, some
attributes are handled at allocation time rather than at initialization

This ensures that even if a derived Config class implements __init__,
the author does not need to be concerned about when or even if he
should call the base Config.__init__

Definition at line 450 of file config.py.

451  def __new__(cls, *args, **kw):
452  """
453  Allocate a new Config object.
454 
455  In order to ensure that all Config object are always in a proper
456  state when handed to users or to derived Config classes, some
457  attributes are handled at allocation time rather than at initialization
458 
459  This ensures that even if a derived Config class implements __init__,
460  the author does not need to be concerned about when or even if he
461  should call the base Config.__init__
462  """
463  name=kw.pop("__name", None)
464  at=kw.pop("__at", traceback.extract_stack()[:-1])
465  label=kw.pop("__label", "default")
466 
467  instance = object.__new__(cls)
468  instance._frozen=False
469  instance._name=name
470  instance._storage = {}
471  instance._history = {}
472  instance._imports = set()
473  # load up defaults
474  for field in instance._fields.itervalues():
475  instance._history[field.name]=[]
476  field.__set__(instance, field.default, at=at+[field.source], label="default")
477  # set custom default-overides
478  instance.setDefaults()
479  # set constructor overides
480  instance.update(__at=at, **kw)
481  return instance
def lsst.pex.config.config.Config.__reduce__ (   self)
Reduction for pickling (function with arguments to reproduce).

We need to condense and reconstitute the Config, since it may contain lambdas
(as the 'check' elements) that cannot be pickled.

Definition at line 482 of file config.py.

483  def __reduce__(self):
484  """Reduction for pickling (function with arguments to reproduce).
485 
486  We need to condense and reconstitute the Config, since it may contain lambdas
487  (as the 'check' elements) that cannot be pickled.
488  """
489  stream = io.BytesIO()
490  self.saveToStream(stream)
491  return (unreduceConfig, (self.__class__, stream.getvalue()))
def lsst.pex.config.config.Config.__repr__ (   self)

Definition at line 711 of file config.py.

712  def __repr__(self):
713  return "%s(%s)" % (
714  _typeStr(self),
715  ", ".join("%s=%r" % (k, v) for k, v in self.toDict().iteritems() if v is not None)
716  )
def lsst.pex.config.config.Config.__setattr__ (   self,
  attr,
  value,
  at = None,
  label = "assignment" 
)
Regulate which attributes can be set.

Unlike normal python objects, Config objects are locked such 
that no additional attributes nor properties may be added to them
dynamically. 

Although this is not the standard Python behavior, it helps to 
protect users from accidentally mispelling a field name, or
trying to set a non-existent field.

Definition at line 657 of file config.py.

658  def __setattr__(self, attr, value, at=None, label="assignment"):
659  """
660  Regulate which attributes can be set.
661 
662  Unlike normal python objects, Config objects are locked such
663  that no additional attributes nor properties may be added to them
664  dynamically.
665 
666  Although this is not the standard Python behavior, it helps to
667  protect users from accidentally mispelling a field name, or
668  trying to set a non-existent field.
669  """
670  if attr in self._fields:
671  if at is None:
672  at=traceback.extract_stack()[:-1]
673  # This allows Field descriptors to work.
674  self._fields[attr].__set__(self, value, at=at, label=label)
675  elif hasattr(getattr(self.__class__, attr, None), '__set__'):
676  # This allows properties and other non-Field descriptors to work.
677  return object.__setattr__(self, attr, value)
678  elif attr in self.__dict__ or attr in ("_name", "_history", "_storage", "_frozen", "_imports"):
679  # This allows specific private attributes to work.
680  self.__dict__[attr] = value
681  else:
682  # We throw everything else.
683  raise AttributeError("%s has no attribute %s"%(_typeStr(self), attr))
def lsst.pex.config.config.Config.__str__ (   self)

Definition at line 708 of file config.py.

709  def __str__(self):
710  return str(self.toDict())
def lsst.pex.config.config.Config._rename (   self,
  name 
)
private
Internal use only. 
Rename this Config object to reflect its position in a Config hierarchy


Correct behavior is dependent on proper implementation of 
Field.rename. If implementing a new Field type, you may need to
implement your own rename method.

Definition at line 613 of file config.py.

614  def _rename(self, name):
615  """
616  Internal use only.
617  Rename this Config object to reflect its position in a Config hierarchy
618 
619 
620  Correct behavior is dependent on proper implementation of
621  Field.rename. If implementing a new Field type, you may need to
622  implement your own rename method.
623  """
624  self._name = name
625  for field in self._fields.itervalues():
626  field.rename(self)
def lsst.pex.config.config.Config._save (   self,
  outfile 
)
private
Internal use only. Save this Config to an open stream object

Definition at line 589 of file config.py.

590  def _save(self, outfile):
591  """
592  Internal use only. Save this Config to an open stream object
593  """
594  for imp in self._imports:
595  if imp in sys.modules and sys.modules[imp] is not None:
596  print >> outfile, "import %s" % imp
597  for field in self._fields.itervalues():
598  field.save(outfile, self)
def lsst.pex.config.config.Config.compare (   self,
  other,
  shortcut = True,
  rtol = 1E-8,
  atol = 1E-8,
  output = None 
)
Compare two Configs for equality.

If the Configs contain RegistryFields or ConfigChoiceFields, unselected Configs
will not be compared.

@param[in] other      Config object to compare with self.
@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 717 of file config.py.

718  def compare(self, other, shortcut=True, rtol=1E-8, atol=1E-8, output=None):
719  """Compare two Configs for equality.
720 
721  If the Configs contain RegistryFields or ConfigChoiceFields, unselected Configs
722  will not be compared.
723 
724  @param[in] other Config object to compare with self.
725  @param[in] shortcut If True, return as soon as an inequality is found.
726  @param[in] rtol Relative tolerance for floating point comparisons.
727  @param[in] atol Absolute tolerance for floating point comparisons.
728  @param[in] output If not None, a callable that takes a string, used (possibly repeatedly)
729  to report inequalities.
730 
731  Floating point comparisons are performed by numpy.allclose; refer to that for details.
732  """
733  name1 = self._name if self._name is not None else "root"
734  name2 = other._name if other._name is not None else "root"
735  name = getComparisonName(name1, name2)
736  return compareConfigs(name, self, other, shortcut=shortcut,
737  rtol=rtol, atol=atol, output=output)
def lsst.pex.config.config.Config.formatHistory (   self,
  name,
  kwargs 
)
Format the config's history to a more human-readable format

Definition at line 645 of file config.py.

646  def formatHistory(self, name, **kwargs):
647  """
648  Format the config's history to a more human-readable format
649  """
650  import lsst.pex.config.history as pexHist
651  return pexHist.format(self, name, **kwargs)
def lsst.pex.config.config.Config.freeze (   self)
Make this Config, and recursively all sub-configs read-only

Definition at line 581 of file config.py.

582  def freeze(self):
583  """
584  Make this Config, and recursively all sub-configs read-only
585  """
586  self._frozen=True
587  for field in self._fields.itervalues():
588  field.freeze(self)
def lsst.pex.config.config.Config.items (   self)
Return the list of (field name, field value) pairs

Definition at line 422 of file config.py.

423  def items(self):
424  """
425  Return the list of (field name, field value) pairs
426  """
427  return self._storage.items()
def lsst.pex.config.config.Config.iteritems (   self)
Enable iterate over (field name, field value) pairs

Definition at line 428 of file config.py.

429  def iteritems(self):
430  """
431  Enable iterate over (field name, field value) pairs
432  """
return self._storage.iteritems()
def lsst.pex.config.config.Config.iterkeys (   self)
Enable iteration over field names

Definition at line 438 of file config.py.

439  def iterkeys(self):
440  """
441  Enable iteration over field names
442  """
443  return self.storage.iterkeys()
def lsst.pex.config.config.Config.itervalues (   self)
Enable iteration over field values

Definition at line 433 of file config.py.

434  def itervalues(self):
435  """
436  Enable iteration over field values
437  """
return self.storage.itervalues()
def lsst.pex.config.config.Config.keys (   self)
Return the list of field names

Definition at line 412 of file config.py.

413  def keys(self):
414  """
415  Return the list of field names
416  """
return self._storage.keys()
def lsst.pex.config.config.Config.load (   self,
  filename,
  root = "root" 
)
Load override from files, modify this config in place by executing the
Python code in the given file.

The file should modify a Config named root

For example:
    root.myField = 5

Definition at line 521 of file config.py.

522  def load(self, filename, root="root"):
523  """
524  Load override from files, modify this config in place by executing the
525  Python code in the given file.
526 
527  The file should modify a Config named root
528 
529  For example:
530  root.myField = 5
531  """
532  with RecordingImporter() as importer:
533  local = {root: self}
534  execfile(filename, {}, local)
535  self._imports.update(importer.getModules())
def lsst.pex.config.config.Config.loadFromStream (   self,
  stream,
  root = "root" 
)
Modify this config in place by executign the python code in the
provided stream.

The stream should modify a Config named 'root', e.g.: root.myField = 5

Definition at line 536 of file config.py.

537  def loadFromStream(self, stream, root="root"):
538  """
539  Modify this config in place by executign the python code in the
540  provided stream.
541 
542  The stream should modify a Config named 'root', e.g.: root.myField = 5
543  """
544  with RecordingImporter() as importer:
545  local = {root: self}
546  exec stream in {}, local
547  self._imports.update(importer.getModules())
def lsst.pex.config.config.Config.save (   self,
  filename,
  root = "root" 
)
Generates a python script at the given filename, which, when loaded,
reproduces this Config.

@param filename [in] name of file to write to
@param root [in] name to use for the root config variable
    If not "root", must match what is used in load())

Definition at line 548 of file config.py.

549  def save(self, filename, root="root"):
550  """
551  Generates a python script at the given filename, which, when loaded,
552  reproduces this Config.
553 
554  @param filename [in] name of file to write to
555  @param root [in] name to use for the root config variable
556  If not "root", must match what is used in load())
557  """
558  with open(filename, 'w') as outfile:
559  self.saveToStream(outfile, root)
def lsst.pex.config.config.Config.saveToStream (   self,
  outfile,
  root = "root" 
)
Generates a python script to the given open file object, which, when
loaded, reproduces this Config.

@param outfile [inout] open file object to write to
@param root [in] name to use for the root config variable
    If not "root", must match what is used in load())

Definition at line 560 of file config.py.

561  def saveToStream(self, outfile, root="root"):
562  """
563  Generates a python script to the given open file object, which, when
564  loaded, reproduces this Config.
565 
566  @param outfile [inout] open file object to write to
567  @param root [in] name to use for the root config variable
568  If not "root", must match what is used in load())
569  """
570  tmp = self._name
571  self._rename(root)
572  try:
573  configType = type(self)
574  typeString = _typeStr(configType)
575  print >> outfile, "import %s" % (configType.__module__)
576  print >> outfile, "assert type(%s)==%s, 'config is of type %%s.%%s" % (root, typeString), \
577  "instead of %s' %% (type(root).__module__, type(root).__name__)" % (typeString,)
578  self._save(outfile)
579  finally:
580  self._rename(tmp)
def lsst.pex.config.config.Config.setDefaults (   self)
Derived config classes that must compute defaults rather than using the 
Field defaults should do so here. 
To correctly use inherited defaults, implementations of setDefaults() 
must call their base class' setDefaults()

Definition at line 492 of file config.py.

493  def setDefaults(self):
494  """
495  Derived config classes that must compute defaults rather than using the
496  Field defaults should do so here.
497  To correctly use inherited defaults, implementations of setDefaults()
498  must call their base class' setDefaults()
499  """
500  pass
def lsst.pex.config.config.Config.toDict (   self)
Convert this Config into a dict whose keys are field names, 
and whose values are field values.

Correct behavior is dependent on proper implementation of 
Field.toDict. If implementing a new Field type, you may need to
implement your own toDict method.

Definition at line 599 of file config.py.

600  def toDict(self):
601  """
602  Convert this Config into a dict whose keys are field names,
603  and whose values are field values.
604 
605  Correct behavior is dependent on proper implementation of
606  Field.toDict. If implementing a new Field type, you may need to
607  implement your own toDict method.
608  """
609  dict_ = {}
610  for name, field in self._fields.iteritems():
611  dict_[name] = field.toDict(self)
612  return dict_
def lsst.pex.config.config.Config.update (   self,
  kw 
)
Treat the Config as a dict, updating values as provided by the keyword
arguments.

The '__at' and '__label' keyword arguments are special internal 
keywords. They are used to strip out any internal steps from the 
history tracebacks of the config. Modifying these keywords allows users
to lie about a Config's history. Please do not do so!

Definition at line 501 of file config.py.

502  def update(self, **kw):
503  """
504  Treat the Config as a dict, updating values as provided by the keyword
505  arguments.
506 
507  The '__at' and '__label' keyword arguments are special internal
508  keywords. They are used to strip out any internal steps from the
509  history tracebacks of the config. Modifying these keywords allows users
510  to lie about a Config's history. Please do not do so!
511  """
512  at=kw.pop("__at", traceback.extract_stack()[:-1])
513  label = kw.pop("__label", "update")
514 
515  for name, value in kw.iteritems():
516  try:
517  field = self._fields[name]
518  field.__set__(self, value, at=at, label=label)
519  except KeyError, e:
520  raise KeyError("No field of name %s exists in config type %s"%(name, _typeStr(self)))
def lsst.pex.config.config.Config.validate (   self)
Validate the Config.

The base class implementation performs type checks on all fields by 
calling Field.validate(). 

Complex single-field validation can be defined by deriving new Field 
types. As syntactic sugar, some derived Field types are defined in 
this module which handle recursing into sub-configs 
(ConfigField, ConfigChoiceField)

Inter-field relationships should only be checked in derived Config 
classes after calling this method, and base validation is complete

Definition at line 627 of file config.py.

628  def validate(self):
629  """
630  Validate the Config.
631 
632  The base class implementation performs type checks on all fields by
633  calling Field.validate().
634 
635  Complex single-field validation can be defined by deriving new Field
636  types. As syntactic sugar, some derived Field types are defined in
637  this module which handle recursing into sub-configs
638  (ConfigField, ConfigChoiceField)
639 
640  Inter-field relationships should only be checked in derived Config
641  classes after calling this method, and base validation is complete
642  """
643  for field in self._fields.itervalues():
644  field.validate(self)
def lsst.pex.config.config.Config.values (   self)
Return the list of field values

Definition at line 417 of file config.py.

418  def values(self):
419  """
420  Return the list of field values
421  """
return self._storage.values()

Member Data Documentation

lsst.pex.config.config.Config.__metaclass__ = ConfigMeta
staticprivate

Definition at line 402 of file config.py.

lsst.pex.config.config.Config._frozen
private

Definition at line 585 of file config.py.

lsst.pex.config.config.Config._name
private

Definition at line 623 of file config.py.

Property Documentation

lsst.pex.config.config.Config.history = property(lambda x: x._history)
static

Definition at line 655 of file config.py.


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