Skip to content

Commit

Permalink
Change PropertySet.getArray to return all types
Browse files Browse the repository at this point in the history
In updating code to use `PropertySet.getArray` or `getScalar`,
instead of `get`, I realized that the best thing `getArray` can do
with items that are neither numeric or scalar (e.g. `PropertySet`
or `PropertyList`) is return them, preferably as a scalar.

This allows one to retrieve all known information about any
item in a container by calling `getArray`.
Formerly this was difficult: you would have to first determine
the data type (e.g. by calling `getScalar`) then call `getArray`
if the type was numeric or string.
  • Loading branch information
r-owen committed Jun 22, 2018
1 parent 94bc79c commit 60730e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,17 @@ def _propertyContainerGet(container, name, returnStyle):
name : ``str``
Name of item
returnStyle : ``ReturnStyle``
Control whether data is returned as an array or scalar:
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. Raise TypeError for
PropertyList, PropertySet or PersistablePtr.
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. Return PropertyList, PropertySet
or PersistablePtr as a single item.
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. Return PropertyList, PropertySet
or PersistablePtr as a single item.
Raises
------
ValueError
If `returnStyle`=``ReturnStyle.ARRAY`` and the item is of type
``PropertyList``, ``PropertySet`` or ``PersistablePtr``
Notes
-----
`returnStyle` is handled as follows:
otherwise.
"""
if not container.exists(name):
raise lsst.pex.exceptions.NotFoundError(name + " not found")
Expand All @@ -97,9 +86,6 @@ def _propertyContainerGet(container, name, returnStyle):
return value
return value[-1]

if returnStyle == ReturnStyle.ARRAY:
raise TypeError("Item {} is not numeric or string".format(name))

try:
return container.getAsPropertyListPtr(name)
except Exception:
Expand Down Expand Up @@ -253,7 +239,10 @@ def get(self, name):
return _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO)

def getArray(self, name):
"""Return an item as an array; the item type must be numeric or string
"""Return an item as an array if the item is numeric or string
If the item is a ``PropertySet``, ``PropertyList`` or
``lsst.daf.base.PersistablePtr`` then return the item as a scalar.
Parameters
----------
Expand All @@ -264,10 +253,6 @@ def getArray(self, name):
------
lsst.pex.exceptions.NotFoundError
If the item does not exist.
TypeError
If item type is ``lsst.daf.base.PropertyList``,
``lsst.daf.base.PropertySet``
or ``lsst.daf.base.PersistablePtr``.
"""
return _propertyContainerGet(self, name, returnStyle=ReturnStyle.ARRAY)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_PropertySet_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,14 @@ def testSubPS(self):
ps1 = dafBase.PropertySet()
ps1.set("a", 1)
ps.setPropertySet("b", ps1)
with self.assertRaises(TypeError):
ps.getArray("b")
self.assertEqual(ps.getArray("b"), ps1)
self.assertEqual(ps.getScalar("b"), ps1)
with self.assertWarns(DeprecationWarning):
self.assertEqual(ps.get("b.a"), 1)
self.assertEqual(ps.getArray("b.a"), [1])
self.assertEqual(ps.getScalar("b.a"), 1)
ps.set("c", ps1)
with self.assertRaises(TypeError):
ps.getArray("c")
self.assertEqual(ps.getArray("c"), ps1)
self.assertEqual(ps.getScalar("c"), ps1)
with self.assertWarns(DeprecationWarning):
self.assertEqual(ps.get("c.a"), 1)
Expand Down

0 comments on commit 60730e9

Please sign in to comment.