LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
lsst.pex.config.config.Field Class Reference
Inheritance diagram for lsst.pex.config.config.Field:
lsst.pex.config.choiceField.ChoiceField lsst.pex.config.configChoiceField.ConfigChoiceField lsst.pex.config.configField.ConfigField lsst.pex.config.configurableField.ConfigurableField lsst.pex.config.dictField.DictField lsst.pex.config.listField.ListField lsst.pex.config.rangeField.RangeField lsst.pex.config.registry.RegistryField lsst.pex.config.configDictField.ConfigDictField

Public Member Functions

def __init__ (self, doc, dtype, default=None, check=None, optional=False)
 
def rename (self, instance)
 
def validate (self, instance)
 
def freeze (self, instance)
 
def save (self, outfile, instance)
 
def toDict (self, instance)
 
def __get__ (self, instance, owner=None, at=None, label="default")
 
def __set__ (self, instance, value, at=None, label='assignment')
 
def __delete__ (self, instance, at=None, label='deletion')
 

Public Attributes

 dtype
 
 doc
 
 default
 
 check
 
 optional
 
 source
 

Static Public Attributes

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

Detailed Description

A field in a `~lsst.pex.config.Config` that supports `int`, `float`,
`complex`, `bool`, and `str` data types.

Parameters
----------
doc : `str`
    A description of the field for users.
dtype : type
    The field's data type. ``Field`` only supports basic data types:
    `int`, `float`, `complex`, `bool`, and `str`. See
    `Field.supportedTypes`.
default : object, optional
    The field's default value.
check : callable, optional
    A callable that is called with the field's value. This callable should
    return `False` if the value is invalid. More complex inter-field
    validation can be written as part of the
    `lsst.pex.config.Config.validate` method.
optional : `bool`, optional
    This sets whether the field is considered optional, and therefore
    doesn't need to be set by the user. When `False`,
    `lsst.pex.config.Config.validate` fails if the field's value is `None`.

Raises
------
ValueError
    Raised when the ``dtype`` parameter is not one of the supported types
    (see `Field.supportedTypes`).

See also
--------
ChoiceField
ConfigChoiceField
ConfigDictField
ConfigField
ConfigurableField
DictField
ListField
RangeField
RegistryField

Notes
-----
``Field`` instances (including those of any subclass of ``Field``) are used
as class attributes of `~lsst.pex.config.Config` subclasses (see the
example, below). ``Field`` attributes work like the `property` attributes
of classes that implement custom setters and getters. `Field` attributes
belong to the class, but operate on the instance. Formally speaking,
`Field` attributes are `descriptors
<https://docs.python.org/3/howto/descriptor.html>`_.

When you access a `Field` attribute on a `Config` instance, you don't
get the `Field` instance itself. Instead, you get the value of that field,
which might be a simple type (`int`, `float`, `str`, `bool`) or a custom
container type (like a `lsst.pex.config.List`) depending on the field's
type. See the example, below.

Examples
--------
Instances of ``Field`` should be used as class attributes of
`lsst.pex.config.Config` subclasses:

>>> from lsst.pex.config import Config, Field
>>> class Example(Config):
...     myInt = Field("An integer field.", int, default=0)
...
>>> print(config.myInt)
0
>>> config.myInt = 5
>>> print(config.myInt)
5

Definition at line 179 of file config.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 257 of file config.py.

257  def __init__(self, doc, dtype, default=None, check=None, optional=False):
258  if dtype not in self.supportedTypes:
259  raise ValueError("Unsupported Field dtype %s" % _typeStr(dtype))
260 
261  source = getStackFrame()
262  self._setup(doc=doc, dtype=dtype, default=default, check=check, optional=optional, source=source)
263 
def getStackFrame(relative=0)
Definition: callStack.py:52
def __init__(self, minimum, dataRange, Q)

Member Function Documentation

◆ __delete__()

def lsst.pex.config.config.Field.__delete__ (   self,
  instance,
  at = None,
  label = 'deletion' 
)
Delete an attribute from a `lsst.pex.config.Config` instance.

Parameters
----------
instance : `lsst.pex.config.Config`
    The config instance that contains this field.
at : `list` of `lsst.pex.config.callStack.StackFrame`
    The call stack (created by
    `lsst.pex.config.callStack.getCallStack`).
label : `str`, optional
    Event label for the history.

Notes
-----
This is invoked by the owning `~lsst.pex.config.Config` object and
should not be called directly.

Definition at line 528 of file config.py.

528  def __delete__(self, instance, at=None, label='deletion'):
529  """Delete an attribute from a `lsst.pex.config.Config` instance.
530 
531  Parameters
532  ----------
533  instance : `lsst.pex.config.Config`
534  The config instance that contains this field.
535  at : `list` of `lsst.pex.config.callStack.StackFrame`
536  The call stack (created by
537  `lsst.pex.config.callStack.getCallStack`).
538  label : `str`, optional
539  Event label for the history.
540 
541  Notes
542  -----
543  This is invoked by the owning `~lsst.pex.config.Config` object and
544  should not be called directly.
545  """
546  if at is None:
547  at = getCallStack()
548  self.__set__(instance, None, at=at, label=label)
549 
def getCallStack(skip=0)
Definition: callStack.py:169

◆ __get__()

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

453  def __get__(self, instance, owner=None, at=None, label="default"):
454  """Define how attribute access should occur on the Config instance
455  This is invoked by the owning config object and should not be called
456  directly
457 
458  When the field attribute is accessed on a Config class object, it
459  returns the field object itself in order to allow inspection of
460  Config classes.
461 
462  When the field attribute is access on a config instance, the actual
463  value described by the field (and held by the Config instance) is
464  returned.
465  """
466  if instance is None or not isinstance(instance, Config):
467  return self
468  else:
469  return instance._storage[self.name]
470 

◆ __set__()

def lsst.pex.config.config.Field.__set__ (   self,
  instance,
  value,
  at = None,
  label = 'assignment' 
)
Set an attribute on the config instance.

Parameters
----------
instance : `lsst.pex.config.Config`
    The config instance that contains this field.
value : obj
    Value to set on this field.
at : `list` of `lsst.pex.config.callStack.StackFrame`
    The call stack (created by
    `lsst.pex.config.callStack.getCallStack`).
label : `str`, optional
    Event label for the history.

Notes
-----
This method is invoked by the owning `lsst.pex.config.Config` object
and should not be called directly.

Derived `~lsst.pex.config.Field` classes may need to override the
behavior. When overriding ``__set__``, `~lsst.pex.config.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 `lsst.pex.config.Config.validate` is called.
- Do not modify the `~lsst.pex.config.Config` instance to contain
  invalid values.
- If the field is modified, update the history of the
  `lsst.pex.config.field.Field` to reflect the changes.

In order to decrease the need to implement this method in derived
`~lsst.pex.config.Field` types, value validation is performed in the
`lsst.pex.config.Field._validateValue`. If only the validation step
differs in the derived `~lsst.pex.config.Field`, it is simpler to
implement `lsst.pex.config.Field._validateValue` than to reimplement
``__set__``. More complicated behavior, however, may require
reimplementation.

Definition at line 471 of file config.py.

471  def __set__(self, instance, value, at=None, label='assignment'):
472  """Set an attribute on the config instance.
473 
474  Parameters
475  ----------
476  instance : `lsst.pex.config.Config`
477  The config instance that contains this field.
478  value : obj
479  Value to set on this field.
480  at : `list` of `lsst.pex.config.callStack.StackFrame`
481  The call stack (created by
482  `lsst.pex.config.callStack.getCallStack`).
483  label : `str`, optional
484  Event label for the history.
485 
486  Notes
487  -----
488  This method is invoked by the owning `lsst.pex.config.Config` object
489  and should not be called directly.
490 
491  Derived `~lsst.pex.config.Field` classes may need to override the
492  behavior. When overriding ``__set__``, `~lsst.pex.config.Field` authors
493  should follow the following rules:
494 
495  - Do not allow modification of frozen configs.
496  - Validate the new value **before** modifying the field. Except if the
497  new value is `None`. `None` is special and no attempt should be made
498  to validate it until `lsst.pex.config.Config.validate` is called.
499  - Do not modify the `~lsst.pex.config.Config` instance to contain
500  invalid values.
501  - If the field is modified, update the history of the
502  `lsst.pex.config.field.Field` to reflect the changes.
503 
504  In order to decrease the need to implement this method in derived
505  `~lsst.pex.config.Field` types, value validation is performed in the
506  `lsst.pex.config.Field._validateValue`. If only the validation step
507  differs in the derived `~lsst.pex.config.Field`, it is simpler to
508  implement `lsst.pex.config.Field._validateValue` than to reimplement
509  ``__set__``. More complicated behavior, however, may require
510  reimplementation.
511  """
512  if instance._frozen:
513  raise FieldValidationError(self, instance, "Cannot modify a frozen Config")
514 
515  history = instance._history.setdefault(self.name, [])
516  if value is not None:
517  value = _autocast(value, self.dtype)
518  try:
519  self._validateValue(value)
520  except BaseException as e:
521  raise FieldValidationError(self, instance, str(e))
522 
523  instance._storage[self.name] = value
524  if at is None:
525  at = getCallStack()
526  history.append((value, at, label))
527 
def getCallStack(skip=0)
Definition: callStack.py:169

◆ freeze()

def lsst.pex.config.config.Field.freeze (   self,
  instance 
)
Make this field read-only (for internal use only).

Parameters
----------
instance : `lsst.pex.config.Config`
    The config instance that contains this field.

Notes
-----
Freezing is only relevant for fields that hold subconfigs. Fields which
hold subconfigs should freeze each subconfig.

**Subclasses should implement this method.**

Definition at line 350 of file config.py.

350  def freeze(self, instance):
351  """Make this field read-only (for internal use only).
352 
353  Parameters
354  ----------
355  instance : `lsst.pex.config.Config`
356  The config instance that contains this field.
357 
358  Notes
359  -----
360  Freezing is only relevant for fields that hold subconfigs. Fields which
361  hold subconfigs should freeze each subconfig.
362 
363  **Subclasses should implement this method.**
364  """
365  pass
366 

◆ rename()

def lsst.pex.config.config.Field.rename (   self,
  instance 
)
Rename the field in a `~lsst.pex.config.Config` (for internal use
only).

Parameters
----------
instance : `lsst.pex.config.Config`
    The config instance that contains this field.

Notes
-----
This method is invoked by the `lsst.pex.config.Config` object that
contains this field and should not be called directly.

Renaming is only relevant for `~lsst.pex.config.Field` instances that
hold subconfigs. `~lsst.pex.config.Fields` that hold subconfigs should
rename each subconfig with the full field name as generated by
`lsst.pex.config.config._joinNamePath`.

Definition at line 300 of file config.py.

300  def rename(self, instance):
301  """Rename the field in a `~lsst.pex.config.Config` (for internal use
302  only).
303 
304  Parameters
305  ----------
306  instance : `lsst.pex.config.Config`
307  The config instance that contains this field.
308 
309  Notes
310  -----
311  This method is invoked by the `lsst.pex.config.Config` object that
312  contains this field and should not be called directly.
313 
314  Renaming is only relevant for `~lsst.pex.config.Field` instances that
315  hold subconfigs. `~lsst.pex.config.Fields` that hold subconfigs should
316  rename each subconfig with the full field name as generated by
317  `lsst.pex.config.config._joinNamePath`.
318  """
319  pass
320 

◆ save()

def lsst.pex.config.config.Field.save (   self,
  outfile,
  instance 
)
Save this field to a file (for internal use only).

Parameters
----------
outfile : file-like object
    A writeable field handle.
instance : `Config`
    The `Config` instance that contains this field.

Notes
-----
This method is invoked by the `~lsst.pex.config.Config` object that
contains this field and should not be called directly.

The output consists of the documentation string
(`lsst.pex.config.Field.doc`) formatted as a Python comment. The second
line is formatted as an assignment: ``{fullname}={value}``.

This output can be executed with Python.

Definition at line 394 of file config.py.

394  def save(self, outfile, instance):
395  """Save this field to a file (for internal use only).
396 
397  Parameters
398  ----------
399  outfile : file-like object
400  A writeable field handle.
401  instance : `Config`
402  The `Config` instance that contains this field.
403 
404  Notes
405  -----
406  This method is invoked by the `~lsst.pex.config.Config` object that
407  contains this field and should not be called directly.
408 
409  The output consists of the documentation string
410  (`lsst.pex.config.Field.doc`) formatted as a Python comment. The second
411  line is formatted as an assignment: ``{fullname}={value}``.
412 
413  This output can be executed with Python.
414  """
415  value = self.__get__(instance)
416  fullname = _joinNamePath(instance._name, self.name)
417 
418  # write full documentation string as comment lines (i.e. first character is #)
419  doc = "# " + str(self.doc).replace("\n", "\n# ")
420  if isinstance(value, float) and (math.isinf(value) or math.isnan(value)):
421  # non-finite numbers need special care
422  outfile.write(u"{}\n{}=float('{!r}')\n\n".format(doc, fullname, value))
423  else:
424  outfile.write(u"{}\n{}={!r}\n\n".format(doc, fullname, value))
425 
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168

◆ toDict()

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` (for internal use only).

Parameters
----------
instance : `Config`
    The `Config` that contains this field.

Returns
-------
value : object
    The field's value. See *Notes*.

Notes
-----
This method invoked by the owning `~lsst.pex.config.Config` object and
should not be called directly.

Simple values are passed through. Complex data structures must be
manipulated. For example, a `~lsst.pex.config.Field` holding a
subconfig 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 426 of file config.py.

426  def toDict(self, instance):
427  """Convert the field value so that it can be set as the value of an
428  item in a `dict` (for internal use only).
429 
430  Parameters
431  ----------
432  instance : `Config`
433  The `Config` that contains this field.
434 
435  Returns
436  -------
437  value : object
438  The field's value. See *Notes*.
439 
440  Notes
441  -----
442  This method invoked by the owning `~lsst.pex.config.Config` object and
443  should not be called directly.
444 
445  Simple values are passed through. Complex data structures must be
446  manipulated. For example, a `~lsst.pex.config.Field` holding a
447  subconfig should, instead of the subconfig object, return a `dict`
448  where the keys are the field names in the subconfig, and the values are
449  the field values in the subconfig.
450  """
451  return self.__get__(instance)
452 

◆ validate()

def lsst.pex.config.config.Field.validate (   self,
  instance 
)
Validate the field (for internal use only).

Parameters
----------
instance : `lsst.pex.config.Config`
    The config instance that contains this field.

Raises
------
lsst.pex.config.FieldValidationError
    Raised if verification fails.

Notes
-----
This method provides basic validation:

- Ensures that the value is not `None` if the field is not optional.
- Ensures type correctness.
- Ensures that the user-provided ``check`` function is valid.

Most `~lsst.pex.config.Field` subclasses should call
`lsst.pex.config.field.Field.validate` if they re-implement
`~lsst.pex.config.field.Field.validate`.

Definition at line 321 of file config.py.

321  def validate(self, instance):
322  """Validate the field (for internal use only).
323 
324  Parameters
325  ----------
326  instance : `lsst.pex.config.Config`
327  The config instance that contains this field.
328 
329  Raises
330  ------
331  lsst.pex.config.FieldValidationError
332  Raised if verification fails.
333 
334  Notes
335  -----
336  This method provides basic validation:
337 
338  - Ensures that the value is not `None` if the field is not optional.
339  - Ensures type correctness.
340  - Ensures that the user-provided ``check`` function is valid.
341 
342  Most `~lsst.pex.config.Field` subclasses should call
343  `lsst.pex.config.field.Field.validate` if they re-implement
344  `~lsst.pex.config.field.Field.validate`.
345  """
346  value = self.__get__(instance)
347  if not self.optional and value is None:
348  raise FieldValidationError(self, instance, "Required value cannot be None")
349 

Member Data Documentation

◆ check

lsst.pex.config.config.Field.check

Definition at line 284 of file config.py.

◆ default

lsst.pex.config.config.Field.default

Definition at line 280 of file config.py.

◆ doc

lsst.pex.config.config.Field.doc

Definition at line 271 of file config.py.

◆ dtype

lsst.pex.config.config.Field.dtype

Definition at line 267 of file config.py.

◆ optional

lsst.pex.config.config.Field.optional

Definition at line 288 of file config.py.

◆ source

lsst.pex.config.config.Field.source

Definition at line 295 of file config.py.

◆ supportedTypes

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

Definition at line 253 of file config.py.


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