LSST Applications g0265f82a02+d6b5cd48b5,g02d81e74bb+a41d3748ce,g1470d8bcf6+6be6c9203b,g2079a07aa2+14824f138e,g212a7c68fe+a4f2ea4efa,g2305ad1205+72971fe858,g295015adf3+ab2c85acae,g2bbee38e9b+d6b5cd48b5,g337abbeb29+d6b5cd48b5,g3ddfee87b4+31b3a28dff,g487adcacf7+082e807817,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+b2918d57ae,g5a732f18d5+66d966b544,g64a986408d+a41d3748ce,g858d7b2824+a41d3748ce,g8a8a8dda67+a6fc98d2e7,g99cad8db69+7fe4acdf18,g9ddcbc5298+d4bad12328,ga1e77700b3+246acaaf9c,ga8c6da7877+84af8b3ff8,gb0e22166c9+3863383f4c,gb6a65358fc+d6b5cd48b5,gba4ed39666+9664299f35,gbb8dafda3b+d8d527deb2,gc07e1c2157+b2dbe6b631,gc120e1dc64+61440b2abb,gc28159a63d+d6b5cd48b5,gcf0d15dbbd+31b3a28dff,gdaeeff99f8+a38ce5ea23,ge6526c86ff+39927bb362,ge79ae78c31+d6b5cd48b5,gee10cc3b42+a6fc98d2e7,gf1cff7945b+a41d3748ce,v24.1.5.rc1
LSST Data Management Base Package
Loading...
Searching...
No Matches
Classes | Functions | Variables
lsst.daf.base.propertyContainer.propertyContainerContinued Namespace Reference

Classes

class  PropertyList
 
class  PropertySet
 
class  ReturnStyle
 

Functions

 getPropertySetState (container, asLists=False)
 
 getPropertyListState (container, asLists=False)
 
 setPropertySetState (container, state)
 
 setPropertyListState (container, state)
 
 _propertyContainerElementTypeName (container, name)
 
 _propertyContainerGet (container, name, returnStyle)
 
 _iterable (a)
 
 _guessIntegerType (container, name, value)
 
 _propertyContainerSet (container, name, value, typeMenu, *args)
 
 _propertyContainerAdd (container, name, value, typeMenu, *args)
 
 _makePropertySet (state)
 
 _makePropertyList (state)
 

Variables

TypeAlias NestedMetadataDict = Mapping[str, Union[str, float, int, bool, "NestedMetadataDict"]]
 
dict _TYPE_MAP = {}
 
 type_obj = getattr(PropertySet, "TYPE_" + checkType)
 

Function Documentation

◆ _guessIntegerType()

lsst.daf.base.propertyContainer.propertyContainerContinued._guessIntegerType ( container,
name,
value )
protected
Given an existing container and name, determine the type
that should be used for the supplied value. The supplied value
is assumed to be a scalar.

On Python 3 all ints are LongLong but we need to be able to store them
in Int containers if that is what is being used (testing for truncation).
Int is assumed to mean 32bit integer (2147483647 to -2147483648).

If there is no pre-existing value we have to decide what to do. For now
we pick Int if the value is less than maxsize.

Parameters
----------
container : `lsst.daf.base.PropertySet` or `lsst.daf.base.PropertyList`
    Container from which to get the value

name : `str`
    Name of item

value : `object`
    Value to be assigned a type. Can be an iterable.

Returns
-------
useType : `str` or none
    Type to use for the supplied value. `None` if the input is
    `bool` or a non-integral value.

Definition at line 259 of file propertyContainerContinued.py.

259def _guessIntegerType(container, name, value):
260 """Given an existing container and name, determine the type
261 that should be used for the supplied value. The supplied value
262 is assumed to be a scalar.
263
264 On Python 3 all ints are LongLong but we need to be able to store them
265 in Int containers if that is what is being used (testing for truncation).
266 Int is assumed to mean 32bit integer (2147483647 to -2147483648).
267
268 If there is no pre-existing value we have to decide what to do. For now
269 we pick Int if the value is less than maxsize.
270
271 Parameters
272 ----------
273 container : `lsst.daf.base.PropertySet` or `lsst.daf.base.PropertyList`
274 Container from which to get the value
275
276 name : `str`
277 Name of item
278
279 value : `object`
280 Value to be assigned a type. Can be an iterable.
281
282 Returns
283 -------
284 useType : `str` or none
285 Type to use for the supplied value. `None` if the input is
286 `bool` or a non-integral value.
287 """
288 maxInt = 2147483647
289 minInt = -2147483648
290 maxLongLong = 2**63 - 1
291 minLongLong = -2**63
292 maxU64 = 2**64 - 1
293 minU64 = 0
294
295 # Go through the values to find the range of supplied integers,
296 # stopping early if we don't have an integer.
297 min = None
298 max = None
299 for v in _iterable(value):
300 # Do not try to convert a bool to an integer
301 if not isinstance(v, numbers.Integral) or isinstance(v, bool):
302 return None
303
304 if min is None:
305 min = v
306 max = v
307 elif v < min:
308 min = v
309 elif v > max:
310 max = v
311
312 # Safety net
313 if min is None or max is None:
314 raise RuntimeError(f"Internal logic failure calculating integer range of {value}")
315
316 def _choose_int_from_range(int_value, current_type):
317 # If this is changing type from non-integer the current type
318 # does not matter.
319 if current_type not in {"Int", "LongLong", "UnsignedLongLong"}:
320 current_type = None
321
322 if int_value <= maxInt and int_value >= minInt and current_type in (None, "Int"):
323 # Use Int only if in range and either no current type or the
324 # current type is an Int.
325 use_type = "Int"
326 elif int_value >= minLongLong and int_value < 0:
327 # All large negatives must be LongLong if they did not fit
328 # in Int clause above.
329 use_type = "LongLong"
330 elif int_value >= 0 and int_value <= maxLongLong and current_type in (None, "Int", "LongLong"):
331 # Larger than Int or already a LongLong
332 use_type = "LongLong"
333 elif int_value <= maxU64 and int_value >= minU64:
334 use_type = "UnsignedLongLong"
335 else:
336 raise RuntimeError("Unable to guess integer type for storing out of "
337 f"range value: {int_value}")
338 return use_type
339
340 if container.exists(name):
341 containerType = _propertyContainerElementTypeName(container, name)
342 else:
343 containerType = None
344
345 useTypeMin = _choose_int_from_range(min, containerType)
346 useTypeMax = _choose_int_from_range(max, containerType)
347
348 if useTypeMin == useTypeMax:
349 return useTypeMin
350
351 # When different the combinations are:
352 # Int + LongLong
353 # Int + UnsignedLongLong
354 # LongLong + UnsignedLongLong
355
356 choices = {useTypeMin, useTypeMax}
357 if choices == {"Int", "LongLong"}:
358 return "LongLong"
359
360 # If UnsignedLongLong is required things will break if the min
361 # is negative. They will break whatever we choose if that is the case
362 # but we have no choice but to return the UnsignedLongLong regardless.
363 if "UnsignedLongLong" in choices:
364 return "UnsignedLongLong"
365
366 raise RuntimeError(f"Logic error in guessing integer type from {min} and {max}")
367
368

◆ _iterable()

lsst.daf.base.propertyContainer.propertyContainerContinued._iterable ( a)
protected
Make input iterable.

Takes whatever is given to it and yields it back one element at a time.
If it is not an iterable or it is a string or PropertySet/List,
yields itself.

Definition at line 243 of file propertyContainerContinued.py.

243def _iterable(a):
244 """Make input iterable.
245
246 Takes whatever is given to it and yields it back one element at a time.
247 If it is not an iterable or it is a string or PropertySet/List,
248 yields itself.
249 """
250 if isinstance(a, (str, PropertyList, PropertySet)):
251 yield a
252 return
253 try:
254 yield from a
255 except Exception:
256 yield a
257
258

◆ _makePropertyList()

lsst.daf.base.propertyContainer.propertyContainerContinued._makePropertyList ( state)
protected
Make a `PropertyList` from the state returned by
`getPropertyListState`

Parameters
----------
state : `list`
    The data returned by `getPropertySetState`.

Definition at line 430 of file propertyContainerContinued.py.

430def _makePropertyList(state):
431 """Make a `PropertyList` from the state returned by
432 `getPropertyListState`
433
434 Parameters
435 ----------
436 state : `list`
437 The data returned by `getPropertySetState`.
438 """
439 pl = PropertyList()
440 setPropertyListState(pl, state)
441 return pl
442
443
444@continueClass

◆ _makePropertySet()

lsst.daf.base.propertyContainer.propertyContainerContinued._makePropertySet ( state)
protected
Make a `PropertySet` from the state returned by `getPropertySetState`

Parameters
----------
state : `list`
    The data returned by `getPropertySetState`.

Definition at line 417 of file propertyContainerContinued.py.

417def _makePropertySet(state):
418 """Make a `PropertySet` from the state returned by `getPropertySetState`
419
420 Parameters
421 ----------
422 state : `list`
423 The data returned by `getPropertySetState`.
424 """
425 ps = PropertySet()
426 setPropertySetState(ps, state)
427 return ps
428
429

◆ _propertyContainerAdd()

lsst.daf.base.propertyContainer.propertyContainerContinued._propertyContainerAdd ( container,
name,
value,
typeMenu,
* args )
protected
Add a single Python value of unknown type

Definition at line 393 of file propertyContainerContinued.py.

393def _propertyContainerAdd(container, name, value, typeMenu, *args):
394 """Add a single Python value of unknown type
395 """
396 try:
397 exemplar = next(_iterable(value))
398 except StopIteration:
399 # Adding an empty iterable to an existing entry is a no-op
400 # since there is nothing to add.
401 return
402 t = type(exemplar)
403 addType = _guessIntegerType(container, name, exemplar)
404
405 if addType is not None or t in typeMenu:
406 if addType is None:
407 addType = typeMenu[t]
408 return getattr(container, "add" + addType)(name, value, *args)
409 # Allow for subclasses
410 for checkType in typeMenu:
411 if (checkType is None and exemplar is None) or \
412 (checkType is not None and isinstance(exemplar, checkType)):
413 return getattr(container, "add" + typeMenu[checkType])(name, value, *args)
414 raise TypeError("Unknown value type for key '%s': %s" % (name, t))
415
416

◆ _propertyContainerElementTypeName()

lsst.daf.base.propertyContainer.propertyContainerContinued._propertyContainerElementTypeName ( container,
name )
protected
Return name of the type of a particular element

Parameters
----------
container : `lsst.daf.base.PropertySet` or `lsst.daf.base.PropertyList`
    Container including the element
name : `str`
    Name of element

Definition at line 168 of file propertyContainerContinued.py.

168def _propertyContainerElementTypeName(container, name):
169 """Return name of the type of a particular element
170
171 Parameters
172 ----------
173 container : `lsst.daf.base.PropertySet` or `lsst.daf.base.PropertyList`
174 Container including the element
175 name : `str`
176 Name of element
177 """
178 try:
179 t = container.typeOf(name)
180 except LookupError as e:
181 # KeyError is more commonly expected when asking for an element
182 # from a mapping.
183 raise KeyError(str(e)) from None
184
185 return _TYPE_MAP.get(t, None)
186
187

◆ _propertyContainerGet()

lsst.daf.base.propertyContainer.propertyContainerContinued._propertyContainerGet ( container,
name,
returnStyle )
protected
Get a value of unknown type as a scalar or array

Parameters
----------
container : `lsst.daf.base.PropertySet` or `lsst.daf.base.PropertyList`
    Container from which to get the value
name : `str`
    Name of item
returnStyle : `ReturnStyle`
    Control whether numeric or string data is returned as an array
    or scalar (the other types, ``PropertyList``, ``PropertySet``
        and ``PersistablePtr``, are always returned as a scalar):
    - ReturnStyle.ARRAY: return numeric or string data types
        as an array of values.
    - ReturnStyle.SCALAR: return numeric or string data types
        as a single value; if the item has multiple values then
        return the last value.
    - ReturnStyle.AUTO: (deprecated) return numeric or string data
        as a scalar if there is just one item, or as an array
        otherwise.

Raises
------
KeyError
    Raised if the specified key does not exist in the container.
TypeError
    Raised if the value retrieved is of an unexpected type.
ValueError
    Raised if the value for ``returnStyle`` is not correct.

Definition at line 188 of file propertyContainerContinued.py.

188def _propertyContainerGet(container, name, returnStyle):
189 """Get a value of unknown type as a scalar or array
190
191 Parameters
192 ----------
193 container : `lsst.daf.base.PropertySet` or `lsst.daf.base.PropertyList`
194 Container from which to get the value
195 name : `str`
196 Name of item
197 returnStyle : `ReturnStyle`
198 Control whether numeric or string data is returned as an array
199 or scalar (the other types, ``PropertyList``, ``PropertySet``
200 and ``PersistablePtr``, are always returned as a scalar):
201 - ReturnStyle.ARRAY: return numeric or string data types
202 as an array of values.
203 - ReturnStyle.SCALAR: return numeric or string data types
204 as a single value; if the item has multiple values then
205 return the last value.
206 - ReturnStyle.AUTO: (deprecated) return numeric or string data
207 as a scalar if there is just one item, or as an array
208 otherwise.
209
210 Raises
211 ------
212 KeyError
213 Raised if the specified key does not exist in the container.
214 TypeError
215 Raised if the value retrieved is of an unexpected type.
216 ValueError
217 Raised if the value for ``returnStyle`` is not correct.
218 """
219 if not container.exists(name):
220 raise KeyError(name + " not found")
221 if returnStyle not in ReturnStyle:
222 raise ValueError("returnStyle {} must be a ReturnStyle".format(returnStyle))
223
224 elemType = _propertyContainerElementTypeName(container, name)
225 if elemType and elemType != "PropertySet":
226 value = getattr(container, "getArray" + elemType)(name)
227 if returnStyle == ReturnStyle.ARRAY or (returnStyle == ReturnStyle.AUTO and len(value) > 1):
228 return value
229 return value[-1]
230
231 if container.isPropertySetPtr(name):
232 try:
233 return container.getAsPropertyListPtr(name)
234 except Exception:
235 return container.getAsPropertySetPtr(name)
236 try:
237 return container.getAsPersistablePtr(name)
238 except Exception:
239 pass
240 raise TypeError('Unknown PropertySet value type for ' + name)
241
242

◆ _propertyContainerSet()

lsst.daf.base.propertyContainer.propertyContainerContinued._propertyContainerSet ( container,
name,
value,
typeMenu,
* args )
protected
Set a single Python value of unknown type

Definition at line 369 of file propertyContainerContinued.py.

369def _propertyContainerSet(container, name, value, typeMenu, *args):
370 """Set a single Python value of unknown type
371 """
372 try:
373 exemplar = next(_iterable(value))
374 except StopIteration:
375 # Do nothing if nothing provided. This matches the behavior
376 # of the explicit setX() methods.
377 return
378 t = type(exemplar)
379 setType = _guessIntegerType(container, name, value)
380
381 if setType is not None or t in typeMenu:
382 if setType is None:
383 setType = typeMenu[t]
384 return getattr(container, "set" + setType)(name, value, *args)
385 # Allow for subclasses
386 for checkType in typeMenu:
387 if (checkType is None and exemplar is None) or \
388 (checkType is not None and isinstance(exemplar, checkType)):
389 return getattr(container, "set" + typeMenu[checkType])(name, value, *args)
390 raise TypeError("Unknown value type for key '%s': %s" % (name, t))
391
392

◆ getPropertyListState()

lsst.daf.base.propertyContainer.propertyContainerContinued.getPropertyListState ( container,
asLists = False )
Get the state of a PropertyList in a form that can be pickled.

Parameters
----------
container : `PropertyList`
    The property container.
asLists : `bool`, optional
    If False, the default, `tuple` will be used for the contents. If true
    a `list` will be used.

Returns
-------
state : `list` of `tuple` or `list` of `list`
    The state, as a list of tuples (or lists), each of which contains
    the following 4 items:

    name (a `str`):
        the name of the item
    elementTypeName (a `str`):
        the suffix of a ``setX`` method name
        which is appropriate for the data type. For example integer
        data has ``elementTypeName="Int"` which corresponds to
        the ``setInt`` method.
    value
        the data for the item, in a form compatible
        with the set method named by ``elementTypeName``
    comment (a `str`): the comment. This item is only present
        if ``container`` is a PropertyList.

Definition at line 92 of file propertyContainerContinued.py.

92def getPropertyListState(container, asLists=False):
93 """Get the state of a PropertyList in a form that can be pickled.
94
95 Parameters
96 ----------
97 container : `PropertyList`
98 The property container.
99 asLists : `bool`, optional
100 If False, the default, `tuple` will be used for the contents. If true
101 a `list` will be used.
102
103 Returns
104 -------
105 state : `list` of `tuple` or `list` of `list`
106 The state, as a list of tuples (or lists), each of which contains
107 the following 4 items:
108
109 name (a `str`):
110 the name of the item
111 elementTypeName (a `str`):
112 the suffix of a ``setX`` method name
113 which is appropriate for the data type. For example integer
114 data has ``elementTypeName="Int"` which corresponds to
115 the ``setInt`` method.
116 value
117 the data for the item, in a form compatible
118 with the set method named by ``elementTypeName``
119 comment (a `str`): the comment. This item is only present
120 if ``container`` is a PropertyList.
121 """
122 sequence = list if asLists else tuple
123 return [sequence((name, _propertyContainerElementTypeName(container, name),
124 _propertyContainerGet(container, name, returnStyle=ReturnStyle.AUTO),
125 container.getComment(name)))
126 for name in container.getOrderedNames()]
127
128

◆ getPropertySetState()

lsst.daf.base.propertyContainer.propertyContainerContinued.getPropertySetState ( container,
asLists = False )
Get the state of a PropertySet in a form that can be pickled.

Parameters
----------
container : `PropertySet`
    The property container.
asLists : `bool`, optional
    If False, the default, `tuple` will be used for the contents. If true
    a `list` will be used.

Returns
-------
state : `list` of `tuple` or `list` of `list`
    The state, as a list of tuples (or lists), each of which contains
    the following 3 items:

    name (a `str`)
        the name of the item
    elementTypeName (a `str`)
        the suffix of a ``setX`` method name
        which is appropriate for the data type. For example integer
        data has ``elementTypeName="Int"` which corresponds to
        the ``setInt`` method.
    value
        the data for the item, in a form compatible
        with the set method named by ``elementTypeName``

Definition at line 57 of file propertyContainerContinued.py.

57def getPropertySetState(container, asLists=False):
58 """Get the state of a PropertySet in a form that can be pickled.
59
60 Parameters
61 ----------
62 container : `PropertySet`
63 The property container.
64 asLists : `bool`, optional
65 If False, the default, `tuple` will be used for the contents. If true
66 a `list` will be used.
67
68 Returns
69 -------
70 state : `list` of `tuple` or `list` of `list`
71 The state, as a list of tuples (or lists), each of which contains
72 the following 3 items:
73
74 name (a `str`)
75 the name of the item
76 elementTypeName (a `str`)
77 the suffix of a ``setX`` method name
78 which is appropriate for the data type. For example integer
79 data has ``elementTypeName="Int"` which corresponds to
80 the ``setInt`` method.
81 value
82 the data for the item, in a form compatible
83 with the set method named by ``elementTypeName``
84 """
85 names = container.names(topLevelOnly=True)
86 sequence = list if asLists else tuple
87 return [sequence((name, _propertyContainerElementTypeName(container, name),
88 _propertyContainerGet(container, name, returnStyle=ReturnStyle.AUTO)))
89 for name in names]
90
91

◆ setPropertyListState()

lsst.daf.base.propertyContainer.propertyContainerContinued.setPropertyListState ( container,
state )
Restore the state of a PropertyList, in place.

Parameters
----------
container : `PropertyList`
    The property container whose state is to be restored.
    It should be empty to start with and is updated in place.
state : `list`
    The state, as returned by ``getPropertyListState``

Definition at line 147 of file propertyContainerContinued.py.

147def setPropertyListState(container, state):
148 """Restore the state of a PropertyList, in place.
149
150 Parameters
151 ----------
152 container : `PropertyList`
153 The property container whose state is to be restored.
154 It should be empty to start with and is updated in place.
155 state : `list`
156 The state, as returned by ``getPropertyListState``
157 """
158 for name, elemType, value, comment in state:
159 getattr(container, "set" + elemType)(name, value, comment)
160
161

◆ setPropertySetState()

lsst.daf.base.propertyContainer.propertyContainerContinued.setPropertySetState ( container,
state )
Restore the state of a PropertySet, in place.

Parameters
----------
container : `PropertySet`
    The property container whose state is to be restored.
    It should be empty to start with and is updated in place.
state : `list`
    The state, as returned by `getPropertySetState`

Definition at line 129 of file propertyContainerContinued.py.

129def setPropertySetState(container, state):
130 """Restore the state of a PropertySet, in place.
131
132 Parameters
133 ----------
134 container : `PropertySet`
135 The property container whose state is to be restored.
136 It should be empty to start with and is updated in place.
137 state : `list`
138 The state, as returned by `getPropertySetState`
139 """
140 for name, elemType, value in state:
141 if elemType is not None:
142 getattr(container, "set" + elemType)(name, value)
143 else:
144 raise ValueError(f"Unrecognized values for state restoration: ({name}, {elemType}, {value})")
145
146

Variable Documentation

◆ _TYPE_MAP

dict lsst.daf.base.propertyContainer.propertyContainerContinued._TYPE_MAP = {}
protected

Definition at line 47 of file propertyContainerContinued.py.

◆ NestedMetadataDict

TypeAlias lsst.daf.base.propertyContainer.propertyContainerContinued.NestedMetadataDict = Mapping[str, Union[str, float, int, bool, "NestedMetadataDict"]]

Definition at line 43 of file propertyContainerContinued.py.

◆ type_obj

lsst.daf.base.propertyContainer.propertyContainerContinued.type_obj = getattr(PropertySet, "TYPE_" + checkType)

Definition at line 51 of file propertyContainerContinued.py.