|
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") |
|
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 662 of file config.py.
def lsst.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 963 of file config.py.
963 def loadFromStream(self, stream, root="config", filename=None):
964 """Modify this Config in place by executing the Python code in the 969 stream : file-like object, `str`, or compiled string 970 Stream containing configuration override code. 971 root : `str`, optional 972 Name of the variable in file that refers to the config being 975 For example, the value of root is ``"config"`` and the file 980 Then this config's field ``myField`` is set to ``5``. 982 **Deprecated:** For backwards compatibility, older config files 983 that use ``root="root"`` instead of ``root="config"`` will be 984 loaded with a warning printed to `sys.stderr`. This feature will be 985 removed at some point. 986 filename : `str`, optional 987 Name of the configuration file, or `None` if unknown or contained 988 in the stream. Used for error reporting. 992 lsst.pex.config.Config.load 993 lsst.pex.config.Config.save 994 lsst.pex.config.Config.saveFromStream 996 with RecordingImporter()
as importer:
999 exec(stream, {}, local)
1000 except NameError
as e:
1001 if root ==
"config" and "root" in e.args[0]:
1002 if filename
is None:
1005 filename = getattr(stream,
"co_filename",
None)
1006 if filename
is None:
1007 filename = getattr(stream,
"name",
"?")
1008 print(f
"Config override file {filename!r}" 1009 " appears to use 'root' instead of 'config'; trying with 'root'", file=sys.stderr)
1010 local = {
"root": self}
1011 exec(stream, {}, local)
1015 self._imports.update(importer.getModules())
def lsst.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 868 of file config.py.
868 def update(self, **kw):
869 """Update values of fields specified by the keyword arguments. 874 Keywords are configuration field names. Values are configuration 879 The ``__at`` and ``__label`` keyword arguments are special internal 880 keywords. They are used to strip out any internal steps from the 881 history tracebacks of the config. Do not modify these keywords to 882 subvert a `~lsst.pex.config.Config` instance's history. 886 This is a config with three fields: 888 >>> from lsst.pex.config import Config, Field 889 >>> class DemoConfig(Config): 890 ... fieldA = Field(doc='Field A', dtype=int, default=42) 891 ... fieldB = Field(doc='Field B', dtype=bool, default=True) 892 ... fieldC = Field(doc='Field C', dtype=str, default='Hello world') 894 >>> config = DemoConfig() 896 These are the default values of each field: 898 >>> for name, value in config.iteritems(): 899 ... print(f"{name}: {value}") 903 fieldC: 'Hello world' 905 Using this method to update ``fieldA`` and ``fieldC``: 907 >>> config.update(fieldA=13, fieldC='Updated!') 909 Now the values of each field are: 911 >>> for name, value in config.iteritems(): 912 ... print(f"{name}: {value}") 919 label = kw.pop(
"__label",
"update")
921 for name, value
in kw.items():
923 field = self._fields[name]
924 field.__set__(self, value, at=at, label=label)
926 raise KeyError(
"No field of name %s exists in config type %s" % (name, _typeStr(self)))