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 | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
lsst.pex.config.config.Field Class Reference
Inheritance diagram for lsst.pex.config.config.Field:

Public Member Functions

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

Public Attributes

 dtype
 
 doc
 
 default
 
 check
 
 optional
 
 source
 

Static Public Attributes

tuple supportedTypes = (str, bool, float, int, complex)
 

Private Member Functions

def _setup
 
def _validateValue
 
def _compare
 

Private Attributes

 __doc__
 

Detailed Description

A field in a a Config.

Instances of Field should be class attributes of Config subclasses:
Field only supports basic data types (int, float, complex, bool, str)

class Example(Config):
    myInt = Field(int, "an integer field!", default=0)

Definition at line 137 of file config.py.

Constructor & Destructor Documentation

def lsst.pex.config.config.Field.__init__ (   self,
  doc,
  dtype,
  default = None,
  check = None,
  optional = False 
)
Initialize a Field.

dtype ------ Data type for the field.  
doc -------- Documentation for the field.
default ---- A default value for the field.
check ------ A callable to be called with the field value that returns 
     False if the value is invalid.  More complex inter-field 
     validation can be written as part of Config validate() 
     method; this will be ignored if set to None.
optional --- When False, Config validate() will fail if value is None

Definition at line 148 of file config.py.

149  def __init__(self, doc, dtype, default=None, check=None, optional=False):
150  """Initialize a Field.
151 
152  dtype ------ Data type for the field.
153  doc -------- Documentation for the field.
154  default ---- A default value for the field.
155  check ------ A callable to be called with the field value that returns
156  False if the value is invalid. More complex inter-field
157  validation can be written as part of Config validate()
158  method; this will be ignored if set to None.
159  optional --- When False, Config validate() will fail if value is None
160  """
161  if dtype not in self.supportedTypes:
162  raise ValueError("Unsupported Field dtype %s"%_typeStr(dtype))
163  source = traceback.extract_stack(limit=2)[0]
164  self._setup(doc=doc, dtype=dtype, default=default, check=check, optional=optional, source=source)
165 

Member Function Documentation

def lsst.pex.config.config.Field.__delete__ (   self,
  instance,
  at = None,
  label = 'deletion' 
)
Describe how attribute deletion should occur on the Config instance.
This is invoked by the owning config object and should not be called
directly

Definition at line 319 of file config.py.

320  def __delete__(self, instance, at=None, label='deletion'):
321  """
322  Describe how attribute deletion should occur on the Config instance.
323  This is invoked by the owning config object and should not be called
324  directly
325  """
326  if at is None:
327  at = traceback.extract_stack()[:-1]
328  self.__set__(instance, None, at=at, label=label)
def lsst.pex.config.config.Field.__get__ (   self,
  instance,
  owner = None,
  at = None,
  label = "default" 
)
Define how attribute access should occur on the Config instance
This is invoked by the owning config object and should not be called
directly

When the field attribute is accessed on a Config class object, it 
returns the field object itself in order to allow inspection of 
Config classes.

When the field attribute is access on a config instance, the actual
value described by the field (and held by the Config instance) is 
returned.

Definition at line 262 of file config.py.

263  def __get__(self, instance, owner=None, at=None, label="default"):
264  """
265  Define how attribute access should occur on the Config instance
266  This is invoked by the owning config object and should not be called
267  directly
268 
269  When the field attribute is accessed on a Config class object, it
270  returns the field object itself in order to allow inspection of
271  Config classes.
272 
273  When the field attribute is access on a config instance, the actual
274  value described by the field (and held by the Config instance) is
275  returned.
276  """
277  if instance is None or not isinstance(instance, Config):
278  return self
279  else:
280  return instance._storage[self.name]
def lsst.pex.config.config.Field.__set__ (   self,
  instance,
  value,
  at = None,
  label = 'assignment' 
)
Describe how attribute setting should occur on the config instance.
This is invoked by the owning config object and should not be called
directly

Derived Field classes may need to override the behavior. When overriding 
__set__, Field authors should follow the following rules:
* Do not allow modification of frozen configs
* Validate the new value *BEFORE* modifying the field. Except if the 
    new value is None. None is special and no attempt should be made to
    validate it until Config.validate is called. 
* Do not modify the Config instance to contain invalid values.
* If the field is modified, update the history of the field to reflect the 
    changes

In order to decrease the need to implement this method in derived Field
types, value validation is performed in the method _validateValue. If 
only the validation step differs in the derived Field, it is simpler to
implement _validateValue than to re-implement __set__. More complicated
behavior, however, may require a reimplementation.

Definition at line 281 of file config.py.

282  def __set__(self, instance, value, at=None, label='assignment'):
283  """
284  Describe how attribute setting should occur on the config instance.
285  This is invoked by the owning config object and should not be called
286  directly
287 
288  Derived Field classes may need to override the behavior. When overriding
289  __set__, Field authors should follow the following rules:
290  * Do not allow modification of frozen configs
291  * Validate the new value *BEFORE* modifying the field. Except if the
292  new value is None. None is special and no attempt should be made to
293  validate it until Config.validate is called.
294  * Do not modify the Config instance to contain invalid values.
295  * If the field is modified, update the history of the field to reflect the
296  changes
297 
298  In order to decrease the need to implement this method in derived Field
299  types, value validation is performed in the method _validateValue. If
300  only the validation step differs in the derived Field, it is simpler to
301  implement _validateValue than to re-implement __set__. More complicated
302  behavior, however, may require a reimplementation.
303  """
304  if instance._frozen:
305  raise FieldValidationError(self, instance, "Cannot modify a frozen Config")
306 
307  history = instance._history.setdefault(self.name, [])
308  if value is not None:
309  value = _autocast(value, self.dtype)
310  try:
311  self._validateValue(value)
312  except BaseException, e:
313  raise FieldValidationError(self, instance, e.message)
314 
315  instance._storage[self.name] = value
316  if at is None:
317  at = traceback.extract_stack()[:-1]
318  history.append((value, at, label))
def lsst.pex.config.config.Field._compare (   self,
  instance1,
  instance2,
  shortcut,
  rtol,
  atol,
  output 
)
private
Helper function for Config.compare; used to compare two fields for equality.

Must be overridden by more complex field types.

@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 329 of file config.py.

330  def _compare(self, instance1, instance2, shortcut, rtol, atol, output):
331  """Helper function for Config.compare; used to compare two fields for equality.
332 
333  Must be overridden by more complex field types.
334 
335  @param[in] instance1 LHS Config instance to compare.
336  @param[in] instance2 RHS Config instance to compare.
337  @param[in] shortcut If True, return as soon as an inequality is found.
338  @param[in] rtol Relative tolerance for floating point comparisons.
339  @param[in] atol Absolute tolerance for floating point comparisons.
340  @param[in] output If not None, a callable that takes a string, used (possibly repeatedly)
341  to report inequalities.
342 
343  Floating point comparisons are performed by numpy.allclose; refer to that for details.
344  """
345  v1 = getattr(instance1, self.name)
346  v2 = getattr(instance2, self.name)
347  name = getComparisonName(
348  _joinNamePath(instance1._name, self.name),
349  _joinNamePath(instance2._name, self.name)
350  )
351  return compareScalars(name, v1, v2, dtype=self.dtype, rtol=rtol, atol=atol, output=output)
def lsst.pex.config.config.Field._setup (   self,
  doc,
  dtype,
  default,
  check,
  optional,
  source 
)
private
Convenience function provided to simplify initialization of derived
Field types

Definition at line 166 of file config.py.

167  def _setup(self, doc, dtype, default, check, optional, source):
168  """
169  Convenience function provided to simplify initialization of derived
170  Field types
171  """
172  self.dtype = dtype
173  self.doc = doc
174  self.__doc__ = doc
175  self.default = default
176  self.check = check
177  self.optional = optional
178  self.source = source
def lsst.pex.config.config.Field._validateValue (   self,
  value 
)
private
Validate a value that is not None
    
This is called from __set__
This is not part of the Field API. However, simple derived field types
    may benifit from implementing _validateValue

Definition at line 212 of file config.py.

213  def _validateValue(self, value):
214  """
215  Validate a value that is not None
216 
217  This is called from __set__
218  This is not part of the Field API. However, simple derived field types
219  may benifit from implementing _validateValue
220  """
221  if value is None:
222  return
223 
224  if not isinstance(value, self.dtype):
225  msg = "Value %s is of incorrect type %s. Expected type %s"%\
226  (value, _typeStr(value), _typeStr(self.dtype))
227  raise TypeError(msg)
228  if self.check is not None and not self.check(value):
229  msg = "Value %s is not a valid value"%str(value)
230  raise ValueError(msg)
def lsst.pex.config.config.Field.freeze (   self,
  instance 
)
Make this field read-only.
Only important for fields which hold sub-configs.
Fields which hold subconfigs should freeze each sub-config.

Definition at line 204 of file config.py.

205  def freeze(self, instance):
206  """
207  Make this field read-only.
208  Only important for fields which hold sub-configs.
209  Fields which hold subconfigs should freeze each sub-config.
210  """
211  pass
def lsst.pex.config.config.Field.rename (   self,
  instance 
)
Rename an instance of this field, not the field itself. 
This is invoked by the owning config object and should not be called
directly

Only useful for fields which hold sub-configs.
Fields which hold subconfigs should rename each sub-config with
the full field name as generated by _joinNamePath

Definition at line 179 of file config.py.

180  def rename(self, instance):
181  """
182  Rename an instance of this field, not the field itself.
183  This is invoked by the owning config object and should not be called
184  directly
185 
186  Only useful for fields which hold sub-configs.
187  Fields which hold subconfigs should rename each sub-config with
188  the full field name as generated by _joinNamePath
189  """
190  pass
def lsst.pex.config.config.Field.save (   self,
  outfile,
  instance 
)
Saves an instance of this field to file.
This is invoked by the owning config object, and should not be called
directly

outfile ---- an open output stream.

Definition at line 231 of file config.py.

232  def save(self, outfile, instance):
233  """
234  Saves an instance of this field to file.
235  This is invoked by the owning config object, and should not be called
236  directly
237 
238  outfile ---- an open output stream.
239  """
240  value = self.__get__(instance)
241  fullname = _joinNamePath(instance._name, self.name)
242  if isinstance(value, float) and (math.isinf(value) or math.isnan(value)):
243  # non-finite numbers need special care
244  print >> outfile, "%s=float('%r')"%(fullname, value)
245  else:
246  print >> outfile, "%s=%r"%(fullname, value)
def lsst.pex.config.config.Field.toDict (   self,
  instance 
)
Convert the field value so that it can be set as the value of an item 
in a dict.
This is invoked by the owning config object and should not be called
directly

Simple values are passed through. Complex data structures must be
manipulated. For example, a field holding a sub-config should, instead
of the subconfig object, return a dict where the keys are the field 
names in the subconfig, and the values are the field values in the 
subconfig.

Definition at line 247 of file config.py.

248  def toDict(self, instance):
249  """
250  Convert the field value so that it can be set as the value of an item
251  in a dict.
252  This is invoked by the owning config object and should not be called
253  directly
254 
255  Simple values are passed through. Complex data structures must be
256  manipulated. For example, a field holding a sub-config should, instead
257  of the subconfig object, return a dict where the keys are the field
258  names in the subconfig, and the values are the field values in the
259  subconfig.
260  """
261  return self.__get__(instance)
def lsst.pex.config.config.Field.validate (   self,
  instance 
)
Base validation for any field.
Ensures that non-optional fields are not None.
Ensures type correctness
Ensures that user-provided check function is valid
Most derived Field types should call Field.validate if they choose
to re-implement validate

Definition at line 191 of file config.py.

192  def validate(self, instance):
193  """
194  Base validation for any field.
195  Ensures that non-optional fields are not None.
196  Ensures type correctness
197  Ensures that user-provided check function is valid
198  Most derived Field types should call Field.validate if they choose
199  to re-implement validate
200  """
201  value = self.__get__(instance)
202  if not self.optional and value is None:
203  raise FieldValidationError(self, instance, "Required value cannot be None")

Member Data Documentation

lsst.pex.config.config.Field.__doc__
private

Definition at line 173 of file config.py.

lsst.pex.config.config.Field.check

Definition at line 175 of file config.py.

lsst.pex.config.config.Field.default

Definition at line 174 of file config.py.

lsst.pex.config.config.Field.doc

Definition at line 172 of file config.py.

lsst.pex.config.config.Field.dtype

Definition at line 171 of file config.py.

lsst.pex.config.config.Field.optional

Definition at line 176 of file config.py.

lsst.pex.config.config.Field.source

Definition at line 177 of file config.py.

tuple lsst.pex.config.config.Field.supportedTypes = (str, bool, float, int, complex)
static

Definition at line 146 of file config.py.


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