|
def | __iter__ (self) |
|
def | keys (self) |
|
def | values (self) |
|
def | items (self) |
|
def | iteritems (self) |
|
def | itervalues (self) |
|
def | iterkeys (self) |
|
def | __contains__ (self, name) |
| Return True if the specified field exists in this config. More...
|
|
def | __new__ (cls, *args, **kw) |
|
def | __reduce__ (self) |
|
def | setDefaults (self) |
|
def | update (self, **kw) |
|
def | load (self, filename, root="config") |
|
def | loadFromStream (self, stream, root="config", filename=None) |
|
def | save (self, filename, root="config") |
|
def | saveToStream (self, outfile, root="config", skipImports=False) |
|
def | freeze (self) |
|
def | toDict (self) |
|
def | names (self) |
|
def | validate (self) |
|
def | formatHistory (self, name, **kwargs) |
|
def | __setattr__ (self, attr, value, at=None, label="assignment") |
|
def | __delattr__ (self, attr, at=None, label="deletion") |
|
def | __eq__ (self, other) |
|
def | __ne__ (self, other) |
|
def | __str__ (self) |
|
def | __repr__ (self) |
|
def | compare (self, other, shortcut=True, rtol=1E-8, atol=1E-8, output=None) |
|
def | __setattr__ (cls, name, value) |
|
Base class for configuration (*config*) objects.
Notes
-----
A ``Config`` object will usually have several `~lsst.pex.config.Field`
instances as class attributes. These are used to define most of the base
class behavior.
``Config`` implements a mapping API that provides many `dict`-like methods,
such as `keys`, `values`, `items`, `iteritems`, `iterkeys`, and
`itervalues`. ``Config`` instances also support the ``in`` operator to
test if a field is in the config. Unlike a `dict`, ``Config`` classes are
not subscriptable. Instead, access individual fields as attributes of the
configuration instance.
Examples
--------
Config classes are subclasses of ``Config`` that have
`~lsst.pex.config.Field` instances (or instances of
`~lsst.pex.config.Field` subclasses) as class attributes:
>>> from lsst.pex.config import Config, Field, ListField
>>> class DemoConfig(Config):
... intField = Field(doc="An integer field", dtype=int, default=42)
... listField = ListField(doc="List of favorite beverages.", dtype=str,
... default=['coffee', 'green tea', 'water'])
...
>>> config = DemoConfig()
Configs support many `dict`-like APIs:
>>> config.keys()
['intField', 'listField']
>>> 'intField' in config
True
Individual fields can be accessed as attributes of the configuration:
>>> config.intField
42
>>> config.listField.append('earl grey tea')
>>> print(config.listField)
['coffee', 'green tea', 'water', 'earl grey tea']
Definition at line 684 of file config.py.
def pex.config.config.Config.compare |
( |
|
self, |
|
|
|
other, |
|
|
|
shortcut = True , |
|
|
|
rtol = 1E-8 , |
|
|
|
atol = 1E-8 , |
|
|
|
output = None |
|
) |
| |
Compare this configuration to another `~lsst.pex.config.Config` for
equality.
Parameters
----------
other : `lsst.pex.config.Config`
Other `~lsst.pex.config.Config` object to compare against this
config.
shortcut : `bool`, optional
If `True`, return as soon as an inequality is found. Default is
`True`.
rtol : `float`, optional
Relative tolerance for floating point comparisons.
atol : `float`, optional
Absolute tolerance for floating point comparisons.
output : callable, optional
A callable that takes a string, used (possibly repeatedly) to
report inequalities.
Returns
-------
isEqual : `bool`
`True` when the two `lsst.pex.config.Config` instances are equal.
`False` if there is an inequality.
See also
--------
lsst.pex.config.compareConfigs
Notes
-----
Unselected targets of `~lsst.pex.config.RegistryField` fields and
unselected choices of `~lsst.pex.config.ConfigChoiceField` fields
are not considered by this method.
Floating point comparisons are performed by `numpy.allclose`.
Definition at line 1341 of file config.py.
1341 def compare(self, other, shortcut=True, rtol=1E-8, atol=1E-8, output=None):
1342 """Compare this configuration to another `~lsst.pex.config.Config` for
1347 other : `lsst.pex.config.Config`
1348 Other `~lsst.pex.config.Config` object to compare against this
1350 shortcut : `bool`, optional
1351 If `True`, return as soon as an inequality is found. Default is
1353 rtol : `float`, optional
1354 Relative tolerance for floating point comparisons.
1355 atol : `float`, optional
1356 Absolute tolerance for floating point comparisons.
1357 output : callable, optional
1358 A callable that takes a string, used (possibly repeatedly) to
1359 report inequalities.
1364 `True` when the two `lsst.pex.config.Config` instances are equal.
1365 `False` if there is an inequality.
1369 lsst.pex.config.compareConfigs
1373 Unselected targets of `~lsst.pex.config.RegistryField` fields and
1374 unselected choices of `~lsst.pex.config.ConfigChoiceField` fields
1375 are not considered by this method.
1377 Floating point comparisons are performed by `numpy.allclose`.
1379 name1 = self._name
if self._name
is not None else "config"
1380 name2 = other._name
if other._name
is not None else "config"
1383 rtol=rtol, atol=atol, output=output)
def pex.config.config.Config.loadFromStream |
( |
|
self, |
|
|
|
stream, |
|
|
|
root = "config" , |
|
|
|
filename = None |
|
) |
| |
Modify this Config in place by executing the Python code in the
provided stream.
Parameters
----------
stream : file-like object, `str`, or compiled string
Stream containing configuration override code.
root : `str`, optional
Name of the variable in file that refers to the config being
overridden.
For example, the value of root is ``"config"`` and the file
contains::
config.myField = 5
Then this config's field ``myField`` is set to ``5``.
**Deprecated:** For backwards compatibility, older config files
that use ``root="root"`` instead of ``root="config"`` will be
loaded with a warning printed to `sys.stderr`. This feature will be
removed at some point.
filename : `str`, optional
Name of the configuration file, or `None` if unknown or contained
in the stream. Used for error reporting.
See also
--------
lsst.pex.config.Config.load
lsst.pex.config.Config.save
lsst.pex.config.Config.saveFromStream
Definition at line 986 of file config.py.
986 def loadFromStream(self, stream, root="config", filename=None):
987 """Modify this Config in place by executing the Python code in the
992 stream : file-like object, `str`, or compiled string
993 Stream containing configuration override code.
994 root : `str`, optional
995 Name of the variable in file that refers to the config being
998 For example, the value of root is ``"config"`` and the file
1003 Then this config's field ``myField`` is set to ``5``.
1005 **Deprecated:** For backwards compatibility, older config files
1006 that use ``root="root"`` instead of ``root="config"`` will be
1007 loaded with a warning printed to `sys.stderr`. This feature will be
1008 removed at some point.
1009 filename : `str`, optional
1010 Name of the configuration file, or `None` if unknown or contained
1011 in the stream. Used for error reporting.
1015 lsst.pex.config.Config.load
1016 lsst.pex.config.Config.save
1017 lsst.pex.config.Config.saveFromStream
1019 with RecordingImporter()
as importer:
1020 globals = {
"__file__": filename}
1022 local = {root: self}
1023 exec(stream, globals, local)
1024 except NameError
as e:
1025 if root ==
"config" and "root" in e.args[0]:
1026 if filename
is None:
1030 filename = getattr(stream,
"co_filename",
None)
1031 if filename
is None:
1032 filename = getattr(stream,
"name",
"?")
1033 print(f
"Config override file {filename!r}"
1034 " appears to use 'root' instead of 'config'; trying with 'root'", file=sys.stderr)
1035 local = {
"root": self}
1036 exec(stream, globals, local)
1040 self._imports.update(importer.getModules())
def pex.config.config.Config.update |
( |
|
self, |
|
|
** |
kw |
|
) |
| |
Update values of fields specified by the keyword arguments.
Parameters
----------
kw
Keywords are configuration field names. Values are configuration
field values.
Notes
-----
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. Do not modify these keywords to
subvert a `~lsst.pex.config.Config` instance's history.
Examples
--------
This is a config with three fields:
>>> from lsst.pex.config import Config, Field
>>> class DemoConfig(Config):
... fieldA = Field(doc='Field A', dtype=int, default=42)
... fieldB = Field(doc='Field B', dtype=bool, default=True)
... fieldC = Field(doc='Field C', dtype=str, default='Hello world')
...
>>> config = DemoConfig()
These are the default values of each field:
>>> for name, value in config.iteritems():
... print(f"{name}: {value}")
...
fieldA: 42
fieldB: True
fieldC: 'Hello world'
Using this method to update ``fieldA`` and ``fieldC``:
>>> config.update(fieldA=13, fieldC='Updated!')
Now the values of each field are:
>>> for name, value in config.iteritems():
... print(f"{name}: {value}")
...
fieldA: 13
fieldB: True
fieldC: 'Updated!'
Definition at line 891 of file config.py.
891 def update(self, **kw):
892 """Update values of fields specified by the keyword arguments.
897 Keywords are configuration field names. Values are configuration
902 The ``__at`` and ``__label`` keyword arguments are special internal
903 keywords. They are used to strip out any internal steps from the
904 history tracebacks of the config. Do not modify these keywords to
905 subvert a `~lsst.pex.config.Config` instance's history.
909 This is a config with three fields:
911 >>> from lsst.pex.config import Config, Field
912 >>> class DemoConfig(Config):
913 ... fieldA = Field(doc='Field A', dtype=int, default=42)
914 ... fieldB = Field(doc='Field B', dtype=bool, default=True)
915 ... fieldC = Field(doc='Field C', dtype=str, default='Hello world')
917 >>> config = DemoConfig()
919 These are the default values of each field:
921 >>> for name, value in config.iteritems():
922 ... print(f"{name}: {value}")
926 fieldC: 'Hello world'
928 Using this method to update ``fieldA`` and ``fieldC``:
930 >>> config.update(fieldA=13, fieldC='Updated!')
932 Now the values of each field are:
934 >>> for name, value in config.iteritems():
935 ... print(f"{name}: {value}")
942 label = kw.pop(
"__label",
"update")
944 for name, value
in kw.items():
946 field = self._fields[name]
947 field.__set__(self, value, at=at, label=label)
949 raise KeyError(
"No field of name %s exists in config type %s" % (name, _typeStr(self)))