LSST Applications g02d81e74bb+86cf3d8bc9,g180d380827+7a4e862ed4,g2079a07aa2+86d27d4dc4,g2305ad1205+e1ca1c66fa,g29320951ab+012e1474a1,g295015adf3+341ea1ce94,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+c429d67c83,g48712c4677+f88676dd22,g487adcacf7+27e1e21933,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+b41db86c35,g5a732f18d5+53520f316c,g64a986408d+86cf3d8bc9,g858d7b2824+86cf3d8bc9,g8a8a8dda67+585e252eca,g99cad8db69+84912a7fdc,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+a2b54eae19,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+6681f309db,gc120e1dc64+f0fcc2f6d8,gc28159a63d+0e5473021a,gcf0d15dbbd+c429d67c83,gdaeeff99f8+f9a426f77a,ge6526c86ff+0433e6603d,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+86cf3d8bc9,w.2024.17
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 1706 of file config.py.

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

◆ _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 1756 of file config.py.

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