LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
Classes | Functions | Variables
lsst.pex.config.config Namespace Reference

Classes

class  _PexConfigGenericAlias
 
class  Config
 
class  ConfigMeta
 
class  Field
 
class  FieldValidationError
 
class  RecordingImporter
 
class  UnexpectedProxyUsageError
 

Functions

 _joinNamePath (prefix=None, name=None, index=None)
 
 _autocast (x, dtype)
 
 _typeStr (x)
 
 _yaml_config_representer (dumper, data)
 
 _yaml_config_constructor (loader, node)
 
 _classFromPython (config_py)
 
 unreduceConfig (cls_, stream)
 

Variables

 GenericAlias = type(Mapping[int, int])
 
 yaml = None
 
tuple YamlLoaders = (yaml.Loader, yaml.FullLoader, yaml.SafeLoader, yaml.UnsafeLoader)
 
 doImport = None
 
 FieldTypeVar = TypeVar("FieldTypeVar")
 
 _yaml_config_constructor
 
 Loader
 

Function Documentation

◆ _autocast()

lsst.pex.config.config._autocast ( x,
dtype )
protected
Cast a value to a type, if appropriate.

Parameters
----------
x : object
    A value.
dtype : tpye
    Data type, such as `float`, `int`, or `str`.

Returns
-------
values : object
    If appropriate, the returned value is ``x`` cast to the given type
    ``dtype``. If the cast cannot be performed the original value of
    ``x`` is returned.

Definition at line 126 of file config.py.

126def _autocast(x, dtype):
127 """Cast a value to a type, if appropriate.
128
129 Parameters
130 ----------
131 x : object
132 A value.
133 dtype : tpye
134 Data type, such as `float`, `int`, or `str`.
135
136 Returns
137 -------
138 values : object
139 If appropriate, the returned value is ``x`` cast to the given type
140 ``dtype``. If the cast cannot be performed the original value of
141 ``x`` is returned.
142 """
143 if dtype == float and isinstance(x, int):
144 return float(x)
145 return x
146
147

◆ _classFromPython()

lsst.pex.config.config._classFromPython ( config_py)
protected
Return the Config subclass required by this Config serialization.

Parameters
----------
config_py : `str`
    A serialized form of the Config as created by
    `Config.saveToStream`.

Returns
-------
cls : `type`
    The `Config` subclass associated with this config.

Definition at line 1704 of file config.py.

1704def _classFromPython(config_py):
1705 """Return the Config subclass required by this Config serialization.
1706
1707 Parameters
1708 ----------
1709 config_py : `str`
1710 A serialized form of the Config as created by
1711 `Config.saveToStream`.
1712
1713 Returns
1714 -------
1715 cls : `type`
1716 The `Config` subclass associated with this config.
1717 """
1718 # standard serialization has the form:
1719 # import config.class
1720 # assert type(config)==config.class.Config, ...
1721 # We want to parse these two lines so we can get the class itself
1722
1723 # Do a single regex to avoid large string copies when splitting a
1724 # large config into separate lines.
1725 matches = re.search(r"^import ([\w.]+)\nassert .*==(.*?),", config_py)
1726
1727 if not matches:
1728 first_line, second_line, _ = config_py.split("\n", 2)
1729 raise ValueError(
1730 f"First two lines did not match expected form. Got:\n - {first_line}\n - {second_line}"
1731 )
1732
1733 module_name = matches.group(1)
1734 module = importlib.import_module(module_name)
1735
1736 # Second line
1737 full_name = matches.group(2)
1738
1739 # Remove the module name from the full name
1740 if not full_name.startswith(module_name):
1741 raise ValueError(f"Module name ({module_name}) inconsistent with full name ({full_name})")
1742
1743 # if module name is a.b.c and full name is a.b.c.d.E then
1744 # we need to remove a.b.c. and iterate over the remainder
1745 # The +1 is for the extra dot after a.b.c
1746 remainder = full_name[len(module_name) + 1 :]
1747 components = remainder.split(".")
1748 pytype = module
1749 for component in components:
1750 pytype = getattr(pytype, component)
1751 return pytype
1752
1753

◆ _joinNamePath()

lsst.pex.config.config._joinNamePath ( prefix = None,
name = None,
index = None )
protected
Generate nested configuration names.

Definition at line 111 of file config.py.

111def _joinNamePath(prefix=None, name=None, index=None):
112 """Generate nested configuration names."""
113 if not prefix and not name:
114 raise ValueError("Invalid name: cannot be None")
115 elif not name:
116 name = prefix
117 elif prefix and name:
118 name = prefix + "." + name
119
120 if index is not None:
121 return f"{name}[{index!r}]"
122 else:
123 return name
124
125

◆ _typeStr()

lsst.pex.config.config._typeStr ( x)
protected
Generate a fully-qualified type name.

Returns
-------
`str`
    Fully-qualified type name.

Notes
-----
This function is used primarily for writing config files to be executed
later upon with the 'load' function.

Definition at line 148 of file config.py.

148def _typeStr(x):
149 """Generate a fully-qualified type name.
150
151 Returns
152 -------
153 `str`
154 Fully-qualified type name.
155
156 Notes
157 -----
158 This function is used primarily for writing config files to be executed
159 later upon with the 'load' function.
160 """
161 if hasattr(x, "__module__") and hasattr(x, "__name__"):
162 xtype = x
163 else:
164 xtype = type(x)
165 if xtype.__module__ == "builtins":
166 return xtype.__name__
167 else:
168 return f"{xtype.__module__}.{xtype.__name__}"
169
170

◆ _yaml_config_constructor()

lsst.pex.config.config._yaml_config_constructor ( loader,
node )
protected
Construct a config from YAML.

Definition at line 193 of file config.py.

193 def _yaml_config_constructor(loader, node):
194 """Construct a config from YAML."""
195 config_py = loader.construct_scalar(node)
196 return Config._fromPython(config_py)
197

◆ _yaml_config_representer()

lsst.pex.config.config._yaml_config_representer ( dumper,
data )
protected
Represent a Config object in a form suitable for YAML.

Stores the serialized stream as a scalar block string.

Definition at line 173 of file config.py.

173 def _yaml_config_representer(dumper, data):
174 """Represent a Config object in a form suitable for YAML.
175
176 Stores the serialized stream as a scalar block string.
177 """
178 stream = io.StringIO()
179 data.saveToStream(stream)
180 config_py = stream.getvalue()
181
182 # Strip multiple newlines from the end of the config
183 # This simplifies the YAML to use | and not |+
184 config_py = config_py.rstrip() + "\n"
185
186 # Trailing spaces force pyyaml to use non-block form.
187 # Remove the trailing spaces so it has no choice
188 config_py = re.sub(r"\s+$", "\n", config_py, flags=re.MULTILINE)
189
190 # Store the Python as a simple scalar
191 return dumper.represent_scalar("lsst.pex.config.Config", config_py, style="|")
192

◆ unreduceConfig()

lsst.pex.config.config.unreduceConfig ( cls_,
stream )
Create a `~lsst.pex.config.Config` from a stream.

Parameters
----------
cls_ : `lsst.pex.config.Config`-type
    A `lsst.pex.config.Config` type (not an instance) that is instantiated
    with configurations in the ``stream``.
stream : file-like object, `str`, or `~types.CodeType`
    Stream containing configuration override code.

Returns
-------
config : `lsst.pex.config.Config`
    Config instance.

See Also
--------
lsst.pex.config.Config.loadFromStream

Definition at line 1754 of file config.py.

1754def unreduceConfig(cls_, stream):
1755 """Create a `~lsst.pex.config.Config` from a stream.
1756
1757 Parameters
1758 ----------
1759 cls_ : `lsst.pex.config.Config`-type
1760 A `lsst.pex.config.Config` type (not an instance) that is instantiated
1761 with configurations in the ``stream``.
1762 stream : file-like object, `str`, or `~types.CodeType`
1763 Stream containing configuration override code.
1764
1765 Returns
1766 -------
1767 config : `lsst.pex.config.Config`
1768 Config instance.
1769
1770 See Also
1771 --------
1772 lsst.pex.config.Config.loadFromStream
1773 """
1774 config = cls_()
1775 config.loadFromStream(stream)
1776 return config

Variable Documentation

◆ _yaml_config_constructor

lsst.pex.config.config._yaml_config_constructor
protected

Definition at line 201 of file config.py.

◆ doImport

lsst.pex.config.config.doImport = None

Definition at line 79 of file config.py.

◆ FieldTypeVar

lsst.pex.config.config.FieldTypeVar = TypeVar("FieldTypeVar")

Definition at line 102 of file config.py.

◆ GenericAlias

lsst.pex.config.config.GenericAlias = type(Mapping[int, int])

Definition at line 55 of file config.py.

◆ Loader

lsst.pex.config.config.Loader

Definition at line 201 of file config.py.

◆ yaml

lsst.pex.config.config.yaml = None

Definition at line 62 of file config.py.

◆ YamlLoaders

tuple lsst.pex.config.config.YamlLoaders = (yaml.Loader, yaml.FullLoader, yaml.SafeLoader, yaml.UnsafeLoader)

Definition at line 68 of file config.py.