LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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

 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 : type
    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 121 of file config.py.

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

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

1688def _classFromPython(config_py):
1689 """Return the Config subclass required by this Config serialization.
1690
1691 Parameters
1692 ----------
1693 config_py : `str`
1694 A serialized form of the Config as created by
1695 `Config.saveToStream`.
1696
1697 Returns
1698 -------
1699 cls : `type`
1700 The `Config` subclass associated with this config.
1701 """
1702 # standard serialization has the form:
1703 # import config.class
1704 # assert type(config) is config.class.Config, ...
1705 # Older files use "type(config)==" instead.
1706 # We want to parse these two lines so we can get the class itself
1707
1708 # Do a single regex to avoid large string copies when splitting a
1709 # large config into separate lines.
1710 # The assert regex cannot be greedy because the assert error string
1711 # can include both "," and " is ".
1712 matches = re.search(r"^import ([\w.]+)\nassert type\‍(\S+\‍)(?:\s*==\s*| is )(.*?),", config_py)
1713
1714 if not matches:
1715 first_line, second_line, _ = config_py.split("\n", 2)
1716 raise ValueError(
1717 f"First two lines did not match expected form. Got:\n - {first_line}\n - {second_line}"
1718 )
1719
1720 module_name = matches.group(1)
1721 module = importlib.import_module(module_name)
1722
1723 # Second line
1724 full_name = matches.group(2)
1725
1726 # Remove the module name from the full name
1727 if not full_name.startswith(module_name):
1728 raise ValueError(f"Module name ({module_name}) inconsistent with full name ({full_name})")
1729
1730 # if module name is a.b.c and full name is a.b.c.d.E then
1731 # we need to remove a.b.c. and iterate over the remainder
1732 # The +1 is for the extra dot after a.b.c
1733 remainder = full_name[len(module_name) + 1 :]
1734 components = remainder.split(".")
1735 pytype = module
1736 for component in components:
1737 pytype = getattr(pytype, component)
1738 return pytype
1739
1740

◆ _joinNamePath()

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

Definition at line 106 of file config.py.

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

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

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

◆ _yaml_config_constructor()

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

Definition at line 188 of file config.py.

188 def _yaml_config_constructor(loader, node):
189 """Construct a config from YAML."""
190 config_py = loader.construct_scalar(node)
191 return Config._fromPython(config_py)
192

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

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

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

1741def unreduceConfig(cls_, stream):
1742 """Create a `~lsst.pex.config.Config` from a stream.
1743
1744 Parameters
1745 ----------
1746 cls_ : `lsst.pex.config.Config`-type
1747 A `lsst.pex.config.Config` type (not an instance) that is instantiated
1748 with configurations in the ``stream``.
1749 stream : file-like object, `str`, or `~types.CodeType`
1750 Stream containing configuration override code.
1751
1752 Returns
1753 -------
1754 config : `lsst.pex.config.Config`
1755 Config instance.
1756
1757 See Also
1758 --------
1759 lsst.pex.config.Config.loadFromStream
1760 """
1761 config = cls_()
1762 config.loadFromStream(stream)
1763 return config

Variable Documentation

◆ _yaml_config_constructor

lsst.pex.config.config._yaml_config_constructor
protected

Definition at line 196 of file config.py.

◆ doImport

lsst.pex.config.config.doImport = None

Definition at line 74 of file config.py.

◆ FieldTypeVar

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

Definition at line 97 of file config.py.

◆ Loader

lsst.pex.config.config.Loader

Definition at line 196 of file config.py.

◆ yaml

lsst.pex.config.config.yaml = None

Definition at line 57 of file config.py.

◆ YamlLoaders

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

Definition at line 63 of file config.py.