25 __all__ = [
"getPropertySetState",
"getPropertyListState",
"setPropertySetState",
"setPropertyListState"]
33 from .propertySet
import PropertySet
34 from .propertyList
import PropertyList
36 from ..dateTime
import DateTime
40 """Get the state of a PropertySet in a form that can be pickled. 44 container : `PropertySet` 45 The property container. 46 asLists : `bool`, optional 47 If False, the default, `tuple` will be used for the contents. If true 48 a `list` will be used. 52 state : `list` of `tuple` or `list` of `list` 53 The state, as a list of tuples (or lists), each of which contains 54 the following 3 items: 55 - name (a `str`): the name of the item 56 - elementTypeName (a `str`): the suffix of a ``setX`` method name 57 which is appropriate for the data type. For example integer 58 data has ``elementTypeName="Int"` which corresponds to 59 the ``setInt`` method. 60 - value: the data for the item, in a form compatible 61 with the set method named by ``elementTypeName`` 63 sequence = list
if asLists
else tuple
64 return [sequence((name, _propertyContainerElementTypeName(container, name),
65 _propertyContainerGet(container, name, returnStyle=ReturnStyle.AUTO)))
66 for name
in container.paramNames(
False)]
70 """Get the state of a PropertyList in a form that can be pickled. 74 container : `PropertyList` 75 The property container. 76 asLists : `bool`, optional 77 If False, the default, `tuple` will be used for the contents. If true 78 a `list` will be used. 82 state : `list` of `tuple` or `list` of `list` 83 The state, as a list of tuples (or lists), each of which contains 84 the following 4 items: 85 - name (a `str`): the name of the item 86 - elementTypeName (a `str`): the suffix of a ``setX`` method name 87 which is appropriate for the data type. For example integer 88 data has ``elementTypeName="Int"` which corresponds to 89 the ``setInt`` method. 90 - value: the data for the item, in a form compatible 91 with the set method named by ``elementTypeName`` 92 - comment (a `str`): the comment. This item is only present 93 if ``container`` is a PropertyList. 95 sequence = list
if asLists
else tuple
96 return [sequence((name, _propertyContainerElementTypeName(container, name),
97 _propertyContainerGet(container, name, returnStyle=ReturnStyle.AUTO),
98 container.getComment(name)))
99 for name
in container.getOrderedNames()]
103 """Restore the state of a PropertySet, in place. 107 container : `PropertySet` 108 The property container whose state is to be restored. 109 It should be empty to start with and is updated in place. 111 The state, as returned by ``getPropertySetState`` 113 for name, elemType, value
in state:
114 getattr(container,
"set" + elemType)(name, value)
118 """Restore the state of a PropertyList, in place. 122 container : `PropertyList` 123 The property container whose state is to be restored. 124 It should be empty to start with and is updated in place. 126 The state, as returned by ``getPropertyListState`` 128 for name, elemType, value, comment
in state:
129 getattr(container,
"set" + elemType)(name, value, comment)
138 def _propertyContainerElementTypeName(container, name):
139 """Return name of the type of a particular element""" 140 t = container.typeOf(name)
141 for checkType
in (
"Bool",
"Short",
"Int",
"Long",
"LongLong",
"Float",
"Double",
"String",
"DateTime"):
142 if t == getattr(container,
"TYPE_" + checkType):
147 def _propertyContainerGet(container, name, returnStyle):
148 """Get a value of unknown type as a scalar or array 152 container : `lsst.daf.base.PropertySet` or `lsst.daf.base.PropertyList` 153 Container from which to get the value 156 returnStyle : `ReturnStyle` 157 Control whether numeric or string data is returned as an array 158 or scalar (the other types, ``PropertyList``, ``PropertySet`` 159 and ``PersistablePtr``, are always returned as a scalar): 160 - ReturnStyle.ARRAY: return numeric or string data types 161 as an array of values. 162 - ReturnStyle.SCALAR: return numeric or string data types 163 as a single value; if the item has multiple values then 164 return the last value. 165 - ReturnStyle.AUTO: (deprecated) return numeric or string data 166 as a scalar if there is just one item, or as an array 169 if not container.exists(name):
171 if returnStyle
not in ReturnStyle:
172 raise ValueError(
"returnStyle {} must be a ReturnStyle".format(returnStyle))
174 elemType = _propertyContainerElementTypeName(container, name)
176 value = getattr(container,
"getArray" + elemType)(name)
177 if returnStyle == ReturnStyle.ARRAY
or (returnStyle == ReturnStyle.AUTO
and len(value) > 1):
182 return container.getAsPropertyListPtr(name)
185 if container.typeOf(name) == container.TYPE_PropertySet:
186 return container.getAsPropertySetPtr(name)
188 return container.getAsPersistablePtr(name)
194 def _guessIntegerType(container, name, value):
195 """Given an existing container and name, determine the type 196 that should be used for the supplied value. The supplied value 197 is assumed to be a scalar. 199 On Python 3 all ints are LongLong but we need to be able to store them 200 in Int containers if that is what is being used (testing for truncation). 201 Int is assumed to mean 32bit integer (2147483647 to -2147483648). 203 If there is no pre-existing value we have to decide what to do. For now 204 we pick Int if the value is less than maxsize. 206 Returns None if the value supplied is a bool or not an integral value. 214 if isinstance(value, bool):
217 if isinstance(value, numbers.Integral):
219 containerType = _propertyContainerElementTypeName(container, name)
223 if value <= maxInt
and value >= minInt:
228 if containerType ==
"Int":
234 elif containerType ==
"LongLong":
239 def _propertyContainerSet(container, name, value, typeMenu, *args):
240 """Set a single Python value of unknown type""" 241 if hasattr(value,
"__iter__")
and not isinstance(value, str):
247 setType = _guessIntegerType(container, name, exemplar)
249 if setType
is not None or t
in typeMenu:
251 setType = typeMenu[t]
252 return getattr(container,
"set" + setType)(name, value, *args)
254 for checkType
in typeMenu:
255 if isinstance(exemplar, checkType):
256 return getattr(container,
"set" + typeMenu[checkType])(name, value, *args)
260 def _propertyContainerAdd(container, name, value, typeMenu, *args):
261 """Add a single Python value of unknown type""" 262 if hasattr(value,
"__iter__"):
268 addType = _guessIntegerType(container, name, exemplar)
270 if addType
is not None or t
in typeMenu:
272 addType = typeMenu[t]
273 return getattr(container,
"add" + addType)(name, value, *args)
275 for checkType
in typeMenu:
276 if isinstance(exemplar, checkType):
277 return getattr(container,
"add" + typeMenu[checkType])(name, value, *args)
281 def _makePropertySet(state):
282 """Make a `PropertySet` from the state returned by `getPropertySetState` 287 The data returned by `getPropertySetState`. 294 def _makePropertyList(state):
295 """Make a `PropertyList` from the state returned by 296 `getPropertyListState` 301 The data returned by `getPropertySetState`. 312 _typeMenu = {bool:
"Bool",
315 DateTime:
"DateTime",
316 PropertySet:
"PropertySet",
317 PropertyList:
"PropertySet",
323 _typeMenu[unicode] =
"String" 328 """Return an item as a scalar or array 330 Return an array if the item is of numeric or string type and has 331 more than one value, otherwise return a scalar. 333 .. deprecated:: 20180-06 334 `get` is superseded by `getArray` or `getScalar` 343 lsst.pex.exceptions.NotFoundError 344 If the item does not exist. 346 warnings.warn(
"Use getArray or getScalar instead", DeprecationWarning, stacklevel=2)
347 return _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO)
350 """Return an item as an array if the item is numeric or string 352 If the item is a `PropertySet`, `PropertyList` or 353 ``lsst.daf.base.PersistablePtr`` then return the item as a scalar. 362 lsst.pex.exceptions.NotFoundError 363 If the item does not exist. 365 return _propertyContainerGet(self, name, returnStyle=ReturnStyle.ARRAY)
368 """Return an item as a scalar 370 If the item has more than one value then the last value is returned 379 lsst.pex.exceptions.NotFoundError 380 If the item does not exist. 382 return _propertyContainerGet(self, name, returnStyle=ReturnStyle.SCALAR)
384 def set(self, name, value):
385 """Set the value of an item 387 If the item already exists it is silently replaced; the types 394 value : any supported type 395 Value of item; may be a scalar or array 397 return _propertyContainerSet(self, name, value, self.
_typeMenu)
399 def add(self, name, value):
400 """Append one or more values to a given item, which need not exist 402 If the item exists then the new value(s) are appended; 403 otherwise it is like calling `set` 409 value : any supported type 410 Value of item; may be a scalar or array 414 If ``value`` is an `lsst.daf.base.PropertySet` or 415 `lsst.daf.base.PropertyList` then ``value`` replaces 416 the existing value. Also the item is added as a live 417 reference, so updating ``value`` will update this container 422 lsst::pex::exceptions::TypeError 423 If the type of `value` is incompatible with the existing value 426 return _propertyContainerAdd(self, name, value, self.
_typeMenu)
429 """Returns a (possibly nested) dictionary with all properties. 434 Dictionary with all names and values (no comments). 438 for name
in self.names():
439 v = _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO)
441 if isinstance(v, PropertySet):
442 d[name] = PropertySet.toDict(v)
459 _typeMenu = {bool:
"Bool",
463 DateTime:
"DateTime",
464 PropertySet:
"PropertySet",
465 PropertyList:
"PropertySet",
471 _typeMenu[unicode] =
"String" 476 """Return an item as a scalar or array 478 Return an array if the item has more than one value, 479 otherwise return a scalar. 481 .. deprecated:: 20180-06 482 `get` is superseded by `getArray` or `getScalar` 491 lsst.pex.exceptions.NotFoundError 492 If the item does not exist. 494 warnings.warn(
"Use getArray or getScalar instead", DeprecationWarning, stacklevel=2)
495 return _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO)
498 """Return an item as an array 507 lsst.pex.exceptions.NotFoundError 508 If the item does not exist. 510 return _propertyContainerGet(self, name, returnStyle=ReturnStyle.ARRAY)
513 """Return an item as a scalar 515 If the item has more than one value then the last value is returned 524 lsst.pex.exceptions.NotFoundError 525 If the item does not exist. 527 return _propertyContainerGet(self, name, returnStyle=ReturnStyle.SCALAR)
529 def set(self, name, value, comment=None):
530 """Set the value of an item 532 If the item already exists it is silently replaced; the types 539 value : any supported type 540 Value of item; may be a scalar or array 543 if comment
is not None:
545 return _propertyContainerSet(self, name, value, self.
_typeMenu, *args)
547 def add(self, name, value, comment=None):
548 """Append one or more values to a given item, which need not exist 550 If the item exists then the new value(s) are appended; 551 otherwise it is like calling `set` 557 value : any supported type 558 Value of item; may be a scalar or array 562 If `value` is an `lsst.daf.base.PropertySet` items are added 563 using dotted names (e.g. if name="a" and value contains 564 an item "b" which is another PropertySet and contains an 565 item "c" which is numeric or string, then the value of "c" 566 is added as "a.b.c", appended to the existing values of 567 "a.b.c" if any (in which case the types must be compatible). 571 lsst::pex::exceptions::TypeError 572 If the type of `value` is incompatible with the existing value 576 if comment
is not None:
578 return _propertyContainerAdd(self, name, value, self.
_typeMenu, *args)
581 """Return a list of tuples of name, value, comment for each property 582 in the order that they were inserted. 586 ret : `list` of `tuple` 587 Tuples of name, value, comment for each property in the order 588 in which they were inserted. 590 orderedNames = self.getOrderedNames()
592 for name
in orderedNames:
593 if self.isArray(name):
594 values = _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO)
596 ret.append((name, v, self.getComment(name)))
598 ret.append((name, _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO),
599 self.getComment(name)))
603 """Return an ordered dictionary with all properties in the order that 608 d : `~collections.OrderedDict` 609 Ordered dictionary with all properties in the order that they 610 were inserted. Comments are not included. 612 from collections
import OrderedDict
615 for name
in self.getOrderedNames():
616 d[name] = _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO)
def add(self, name, value, comment=None)
def set(self, name, value, comment=None)
def set(self, name, value)
def add(self, name, value)
def getPropertySetState(container, asLists=False)
def getScalar(self, name)
def setPropertyListState(container, state)
def getPropertyListState(container, asLists=False)
def setPropertySetState(container, state)
def getScalar(self, name)