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
Functions | Variables
lsst.pex.config.wrap Namespace Reference

Functions

 makeConfigClass (ctrl, name=None, base=Config, doc=None, module=None, cls=None)
 
 wrap (ctrl)
 

Variables

dict _dtypeMap
 
 _containerRegex = re.compile(r"(std::)?(vector|list)<\s*(?P<type>[a-z0-9_:]+)\s*>")
 
 _history
 
 Control
 
 makeControl
 
 readControl
 
 setDefaults
 
 validate
 

Function Documentation

◆ makeConfigClass()

lsst.pex.config.wrap.makeConfigClass ( ctrl,
name = None,
base = Config,
doc = None,
module = None,
cls = None )
Create a `~lsst.pex.config.Config` class that matches a  C++ control
object class.

See the `wrap` decorator as a convenient interface to ``makeConfigClass``.

Parameters
----------
ctrl : class
    C++ control class to wrap.
name : `str`, optional
    Name of the new config class; defaults to the ``__name__`` of the
    control class with ``'Control'`` replaced with ``'Config'``.
base : `lsst.pex.config.Config`-type, optional
    Base class for the config class.
doc : `str`, optional
    Docstring for the config class.
module : object, `str`, `int`, or `None` optional
    Either a module object, a string specifying the name of the module, or
    an integer specifying how far back in the stack to look for the module
    to use: 0 is the immediate caller of `~lsst.pex.config.wrap`. This will
    be used to set ``__module__`` for the new config class, and the class
    will also be added to the module. Ignored if `None` or if ``cls`` is
    not `None`. Defaults to None in which case module is looked up from the
    module of ctrl.
cls : class
    An existing config class to use instead of creating a new one; name,
    base doc, and module will be ignored if this is not `None`.

Notes
-----
To use ``makeConfigClass``, write a control object in C++ using the
``LSST_CONTROL_FIELD`` macro in ``lsst/pex/config.h`` (note that it must
have sensible default constructor):

.. code-block:: cpp

   // myHeader.h

   struct InnerControl {
       LSST_CONTROL_FIELD(wim, std::string,
                          "documentation for field 'wim'");
   };

   struct FooControl {
       LSST_CONTROL_FIELD(bar, int, "documentation for field 'bar'");
       LSST_CONTROL_FIELD(baz, double, "documentation for field 'baz'");
       LSST_NESTED_CONTROL_FIELD(zot, myWrappedLib, InnerControl,
                                 "documentation for field 'zot'");

       FooControl() : bar(0), baz(0.0) {}
   };

You can use ``LSST_NESTED_CONTROL_FIELD`` to nest control objects. Wrap
those control objects as you would any other C++ class, but make sure you
include ``lsst/pex/config.h`` before including the header file where
the control object class is defined.

Next, in Python:

.. code-block:: py

   import lsst.pex.config
   import myWrappedLib

   InnerConfig = lsst.pex.config.makeConfigClass(myWrappedLib.InnerControl)
   FooConfig = lsst.pex.config.makeConfigClass(myWrappedLib.FooControl)

This does the following things:

- Adds ``bar``, ``baz``, and ``zot`` fields to ``FooConfig``.
- Set ``FooConfig.Control`` to ``FooControl``.
- Adds ``makeControl`` and ``readControl`` methods to create a
  ``FooControl`` and set the ``FooConfig`` from the ``FooControl``,
  respectively.
- If ``FooControl`` has a ``validate()`` member function,
  a custom ``validate()`` method will be added to ``FooConfig`` that uses
  it.

All of the above are done for ``InnerConfig`` as well.

Any field that would be injected that would clash with an existing
attribute of the class is be silently ignored. This allows you to
customize fields and inherit them from wrapped control classes. However,
these names are still be processed when converting between config and
control classes, so they should generally be present as base class fields
or other instance attributes or descriptors.

While ``LSST_CONTROL_FIELD`` will work for any C++ type, automatic
`~lsst.pex.config.Config` generation only supports ``bool``, ``int``,
``std::int64_t``, ``double``, and ``std::string`` fields, along with
``std::list`` and ``std::vectors`` of those types.

See Also
--------
wrap : Add fields from C++ object.

Definition at line 56 of file wrap.py.

56def makeConfigClass(ctrl, name=None, base=Config, doc=None, module=None, cls=None):
57 """Create a `~lsst.pex.config.Config` class that matches a C++ control
58 object class.
59
60 See the `wrap` decorator as a convenient interface to ``makeConfigClass``.
61
62 Parameters
63 ----------
64 ctrl : class
65 C++ control class to wrap.
66 name : `str`, optional
67 Name of the new config class; defaults to the ``__name__`` of the
68 control class with ``'Control'`` replaced with ``'Config'``.
69 base : `lsst.pex.config.Config`-type, optional
70 Base class for the config class.
71 doc : `str`, optional
72 Docstring for the config class.
73 module : object, `str`, `int`, or `None` optional
74 Either a module object, a string specifying the name of the module, or
75 an integer specifying how far back in the stack to look for the module
76 to use: 0 is the immediate caller of `~lsst.pex.config.wrap`. This will
77 be used to set ``__module__`` for the new config class, and the class
78 will also be added to the module. Ignored if `None` or if ``cls`` is
79 not `None`. Defaults to None in which case module is looked up from the
80 module of ctrl.
81 cls : class
82 An existing config class to use instead of creating a new one; name,
83 base doc, and module will be ignored if this is not `None`.
84
85 Notes
86 -----
87 To use ``makeConfigClass``, write a control object in C++ using the
88 ``LSST_CONTROL_FIELD`` macro in ``lsst/pex/config.h`` (note that it must
89 have sensible default constructor):
90
91 .. code-block:: cpp
92
93 // myHeader.h
94
95 struct InnerControl {
96 LSST_CONTROL_FIELD(wim, std::string,
97 "documentation for field 'wim'");
98 };
99
100 struct FooControl {
101 LSST_CONTROL_FIELD(bar, int, "documentation for field 'bar'");
102 LSST_CONTROL_FIELD(baz, double, "documentation for field 'baz'");
103 LSST_NESTED_CONTROL_FIELD(zot, myWrappedLib, InnerControl,
104 "documentation for field 'zot'");
105
106 FooControl() : bar(0), baz(0.0) {}
107 };
108
109 You can use ``LSST_NESTED_CONTROL_FIELD`` to nest control objects. Wrap
110 those control objects as you would any other C++ class, but make sure you
111 include ``lsst/pex/config.h`` before including the header file where
112 the control object class is defined.
113
114 Next, in Python:
115
116 .. code-block:: py
117
118 import lsst.pex.config
119 import myWrappedLib
120
121 InnerConfig = lsst.pex.config.makeConfigClass(myWrappedLib.InnerControl)
122 FooConfig = lsst.pex.config.makeConfigClass(myWrappedLib.FooControl)
123
124 This does the following things:
125
126 - Adds ``bar``, ``baz``, and ``zot`` fields to ``FooConfig``.
127 - Set ``FooConfig.Control`` to ``FooControl``.
128 - Adds ``makeControl`` and ``readControl`` methods to create a
129 ``FooControl`` and set the ``FooConfig`` from the ``FooControl``,
130 respectively.
131 - If ``FooControl`` has a ``validate()`` member function,
132 a custom ``validate()`` method will be added to ``FooConfig`` that uses
133 it.
134
135 All of the above are done for ``InnerConfig`` as well.
136
137 Any field that would be injected that would clash with an existing
138 attribute of the class is be silently ignored. This allows you to
139 customize fields and inherit them from wrapped control classes. However,
140 these names are still be processed when converting between config and
141 control classes, so they should generally be present as base class fields
142 or other instance attributes or descriptors.
143
144 While ``LSST_CONTROL_FIELD`` will work for any C++ type, automatic
145 `~lsst.pex.config.Config` generation only supports ``bool``, ``int``,
146 ``std::int64_t``, ``double``, and ``std::string`` fields, along with
147 ``std::list`` and ``std::vectors`` of those types.
148
149 See Also
150 --------
151 wrap : Add fields from C++ object.
152 """
153 if name is None:
154 if "Control" not in ctrl.__name__:
155 raise ValueError("Cannot guess appropriate Config class name for %s." % ctrl)
156 name = ctrl.__name__.replace("Control", "Config")
157 if cls is None:
158 cls = type(name, (base,), {"__doc__": doc})
159 if module is not None:
160 # Not only does setting __module__ make Python pretty-printers
161 # more useful, it's also necessary if we want to pickle Config
162 # objects.
163 if isinstance(module, int):
164 frame = getCallerFrame(module)
165 moduleObj = inspect.getmodule(frame)
166 moduleName = moduleObj.__name__
167 elif isinstance(module, str):
168 moduleName = module
169 moduleObj = __import__(moduleName)
170 else:
171 moduleObj = module
172 moduleName = moduleObj.__name__
173 cls.__module__ = moduleName
174 setattr(moduleObj, name, cls)
175 else:
176 cls.__module__ = ctrl.__module__
177 moduleName = ctrl.__module__
178 else:
179 moduleName = cls.__module__
180 if doc is None:
181 doc = ctrl.__doc__
182 fields = {}
183 # loop over all class attributes, looking for the special static methods
184 # that indicate a field defined by one of the macros in pex/config.h.
185 for attr in dir(ctrl):
186 if attr.startswith("_type_"):
187 k = attr[len("_type_") :]
188 getDoc = "_doc_" + k
189 getModule = "_module_" + k
190 getType = attr
191 if hasattr(ctrl, k) and hasattr(ctrl, getDoc):
192 doc = getattr(ctrl, getDoc)()
193 ctype = getattr(ctrl, getType)()
194 if hasattr(ctrl, getModule): # if this is present, it's a nested control object
195 nestedModuleName = getattr(ctrl, getModule)()
196 if nestedModuleName == moduleName:
197 nestedModuleObj = moduleObj
198 else:
199 nestedModuleObj = importlib.import_module(nestedModuleName)
200 try:
201 dtype = getattr(nestedModuleObj, ctype).ConfigClass
202 except AttributeError:
203 raise AttributeError(f"'{moduleName}.{ctype}.ConfigClass' does not exist")
204 fields[k] = ConfigField(doc=doc, dtype=dtype)
205 else:
206 try:
207 dtype = _dtypeMap[ctype]
208 FieldCls = Field
209 except KeyError:
210 dtype = None
211 m = _containerRegex.match(ctype)
212 if m:
213 dtype = _dtypeMap.get(m.group("type"), None)
214 FieldCls = ListField
215 if dtype is None:
216 raise TypeError("Could not parse field type '%s'." % ctype)
217 fields[k] = FieldCls(doc=doc, dtype=dtype, optional=True)
218
219 # Define a number of methods to put in the new Config class. Note that
220 # these are "closures"; they have access to local variables defined in
221 # the makeConfigClass function (like the fields dict).
222 def makeControl(self):
223 """Construct a C++ Control object from this Config object.
224
225 Fields set to `None` will be ignored, and left at the values defined
226 by the Control object's default constructor.
227 """
228 r = self.Control()
229 for k, f in fields.items():
230 value = getattr(self, k)
231 if isinstance(f, ConfigField):
232 value = value.makeControl()
233 if value is not None:
234 if isinstance(value, List):
235 setattr(r, k, value._list)
236 else:
237 setattr(r, k, value)
238 return r
239
240 def readControl(self, control, __at=None, __label="readControl", __reset=False):
241 """Read values from a C++ Control object and assign them to self's
242 fields.
243
244 Parameters
245 ----------
246 control : `type`
247 C++ Control object.
248 __at : `list` of `~lsst.pex.config.callStack.StackFrame` or `None`,\
249 optional
250 Internal use only.
251 __label : `str`, optional
252 Internal use only.
253 __reset : `bool`, optional
254 Internal use only.
255
256 Notes
257 -----
258 The ``__at``, ``__label``, and ``__reset`` arguments are for internal
259 use only; they are used to remove internal calls from the history.
260 """
261 if __at is None:
262 __at = getCallStack()
263 values = {}
264 for k, f in fields.items():
265 if isinstance(f, ConfigField):
266 getattr(self, k).readControl(getattr(control, k), __at=__at, __label=__label, __reset=__reset)
267 else:
268 values[k] = getattr(control, k)
269 if __reset:
270 self._history = {}
271 self.update(__at=__at, __label=__label, **values)
272
273 def validate(self):
274 """Validate the config object by constructing a control object and
275 using a C++ ``validate()`` implementation.
276 """
277 super(cls, self).validate()
278 r = self.makeControl()
279 r.validate()
280
281 def setDefaults(self):
282 """Initialize the config object, using the Control objects default ctor
283 to provide defaults.
284 """
285 super(cls, self).setDefaults()
286 try:
287 r = self.Control()
288 # Indicate in the history that these values came from C++, even
289 # if we can't say which line
290 self.readControl(
291 r,
292 __at=[StackFrame(ctrl.__name__ + " C++", 0, "setDefaults", "")],
293 __label="defaults",
294 __reset=True,
295 )
296 except Exception:
297 pass # if we can't instantiate the Control, don't set defaults
298
299 ctrl.ConfigClass = cls
300 cls.Control = ctrl
301 cls.makeControl = makeControl
302 cls.readControl = readControl
303 cls.setDefaults = setDefaults
304 if hasattr(ctrl, "validate"):
305 cls.validate = validate
306 for k, field in fields.items():
307 if not hasattr(cls, k):
308 setattr(cls, k, field)
309 return cls
310
311

◆ wrap()

lsst.pex.config.wrap.wrap ( ctrl)
Add fields from a C++ control class to a `lsst.pex.config.Config` class.

Parameters
----------
ctrl : object
    The C++ control class.

Notes
-----
See `makeConfigClass` for more information. This `wrap` decorator is
equivalent to calling `makeConfigClass` with the decorated class as the
``cls`` argument.

Examples
--------
Use `wrap` like this::

    @wrap(MyControlClass)
    class MyConfigClass(Config):
        pass

See Also
--------
makeConfigClass : Make a config class.

Definition at line 312 of file wrap.py.

312def wrap(ctrl):
313 """Add fields from a C++ control class to a `lsst.pex.config.Config` class.
314
315 Parameters
316 ----------
317 ctrl : object
318 The C++ control class.
319
320 Notes
321 -----
322 See `makeConfigClass` for more information. This `wrap` decorator is
323 equivalent to calling `makeConfigClass` with the decorated class as the
324 ``cls`` argument.
325
326 Examples
327 --------
328 Use `wrap` like this::
329
330 @wrap(MyControlClass)
331 class MyConfigClass(Config):
332 pass
333
334 See Also
335 --------
336 makeConfigClass : Make a config class.
337 """
338
339 def decorate(cls):
340 return makeConfigClass(ctrl, cls=cls)
341
342 return decorate

Variable Documentation

◆ _containerRegex

lsst.pex.config.wrap._containerRegex = re.compile(r"(std::)?(vector|list)<\s*(?P<type>[a-z0-9_:]+)\s*>")
protected

Definition at line 53 of file wrap.py.

◆ _dtypeMap

dict lsst.pex.config.wrap._dtypeMap
protected
Initial value:
1= {
2 "bool": bool,
3 "int": int,
4 "double": float,
5 "float": float,
6 "std::int64_t": int,
7 "std::string": str,
8}

Definition at line 39 of file wrap.py.

◆ _history

lsst.pex.config.wrap._history
protected

Definition at line 270 of file wrap.py.

◆ Control

lsst.pex.config.wrap.Control

Definition at line 300 of file wrap.py.

◆ makeControl

lsst.pex.config.wrap.makeControl

Definition at line 301 of file wrap.py.

◆ readControl

lsst.pex.config.wrap.readControl

Definition at line 302 of file wrap.py.

◆ setDefaults

lsst.pex.config.wrap.setDefaults

Definition at line 303 of file wrap.py.

◆ validate

lsst.pex.config.wrap.validate

Definition at line 305 of file wrap.py.