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 | 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 136 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 147 of file config.py.

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

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

319  def __delete__(self, instance, at=None, label='deletion'):
320  """
321  Describe how attribute deletion should occur on the Config instance.
322  This is invoked by the owning config object and should not be called
323  directly
324  """
325  if at is None:
326  at = traceback.extract_stack()[:-1]
327  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 261 of file config.py.

262  def __get__(self, instance, owner=None, at=None, label="default"):
263  """
264  Define how attribute access should occur on the Config instance
265  This is invoked by the owning config object and should not be called
266  directly
267 
268  When the field attribute is accessed on a Config class object, it
269  returns the field object itself in order to allow inspection of
270  Config classes.
271 
272  When the field attribute is access on a config instance, the actual
273  value described by the field (and held by the Config instance) is
274  returned.
275  """
276  if instance is None or not isinstance(instance, Config):
277  return self
278  else:
279  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 280 of file config.py.

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

329  def _compare(self, instance1, instance2, shortcut, rtol, atol, output):
330  """Helper function for Config.compare; used to compare two fields for equality.
331 
332  Must be overridden by more complex field types.
333 
334  @param[in] instance1 LHS Config instance to compare.
335  @param[in] instance2 RHS Config instance to compare.
336  @param[in] shortcut If True, return as soon as an inequality is found.
337  @param[in] rtol Relative tolerance for floating point comparisons.
338  @param[in] atol Absolute tolerance for floating point comparisons.
339  @param[in] output If not None, a callable that takes a string, used (possibly repeatedly)
340  to report inequalities.
341 
342  Floating point comparisons are performed by numpy.allclose; refer to that for details.
343  """
344  v1 = getattr(instance1, self.name)
345  v2 = getattr(instance2, self.name)
346  name = getComparisonName(
347  _joinNamePath(instance1._name, self.name),
348  _joinNamePath(instance2._name, self.name)
349  )
350  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 165 of file config.py.

166  def _setup(self, doc, dtype, default, check, optional, source):
167  """
168  Convenience function provided to simplify initialization of derived
169  Field types
170  """
171  self.dtype = dtype
172  self.doc = doc
173  self.__doc__ = doc
174  self.default = default
175  self.check = check
176  self.optional = optional
177  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 211 of file config.py.

212  def _validateValue(self, value):
213  """
214  Validate a value that is not None
215 
216  This is called from __set__
217  This is not part of the Field API. However, simple derived field types
218  may benifit from implementing _validateValue
219  """
220  if value is None:
221  return
222 
223  if not isinstance(value, self.dtype):
224  msg = "Value %s is of incorrect type %s. Expected type %s"%\
225  (value, _typeStr(value), _typeStr(self.dtype))
226  raise TypeError(msg)
227  if self.check is not None and not self.check(value):
228  msg = "Value %s is not a valid value"%str(value)
229  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 203 of file config.py.

204  def freeze(self, instance):
205  """
206  Make this field read-only.
207  Only important for fields which hold sub-configs.
208  Fields which hold subconfigs should freeze each sub-config.
209  """
210  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 178 of file config.py.

179  def rename(self, instance):
180  """
181  Rename an instance of this field, not the field itself.
182  This is invoked by the owning config object and should not be called
183  directly
184 
185  Only useful for fields which hold sub-configs.
186  Fields which hold subconfigs should rename each sub-config with
187  the full field name as generated by _joinNamePath
188  """
189  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 230 of file config.py.

231  def save(self, outfile, instance):
232  """
233  Saves an instance of this field to file.
234  This is invoked by the owning config object, and should not be called
235  directly
236 
237  outfile ---- an open output stream.
238  """
239  value = self.__get__(instance)
240  fullname = _joinNamePath(instance._name, self.name)
241  if isinstance(value, float) and (math.isinf(value) or math.isnan(value)):
242  # non-finite numbers need special care
243  print >> outfile, "%s=float('%r')"%(fullname, value)
244  else:
245  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 246 of file config.py.

247  def toDict(self, instance):
248  """
249  Convert the field value so that it can be set as the value of an item
250  in a dict.
251  This is invoked by the owning config object and should not be called
252  directly
253 
254  Simple values are passed through. Complex data structures must be
255  manipulated. For example, a field holding a sub-config should, instead
256  of the subconfig object, return a dict where the keys are the field
257  names in the subconfig, and the values are the field values in the
258  subconfig.
259  """
260  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 190 of file config.py.

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

Member Data Documentation

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

Definition at line 172 of file config.py.

lsst.pex.config.config.Field.check

Definition at line 174 of file config.py.

lsst.pex.config.config.Field.default

Definition at line 173 of file config.py.

lsst.pex.config.config.Field.doc

Definition at line 171 of file config.py.

lsst.pex.config.config.Field.dtype

Definition at line 170 of file config.py.

lsst.pex.config.config.Field.optional

Definition at line 175 of file config.py.

lsst.pex.config.config.Field.source

Definition at line 176 of file config.py.

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

Definition at line 145 of file config.py.


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