Skip to content

Commit

Permalink
Merge pull request #63 from lsst/tickets/DM-10339
Browse files Browse the repository at this point in the history
DM-10339: Add valueCount() to get total number of entries.
  • Loading branch information
ktlim committed Nov 10, 2020
2 parents 1653f94 + 4cffe29 commit f910f65
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 9 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: lint

on:
- push
- pull_request

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.7

- name: Install
run: pip install -r <(curl https://raw.githubusercontent.com/lsst/linting/master/requirements.txt)

- name: Run linter
run: flake8
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

8 changes: 8 additions & 0 deletions include/lsst/daf/base/PropertySet.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ class LSST_EXPORT PropertySet {
*/
bool isUndefined(std::string const& name) const;

/**
* Get the number of values in the entire PropertySet, counting each
* element of a vector.
*
* @return Number of values.
*/
size_t valueCount() const;

/**
* Get the number of values for a property name (possibly hierarchical).
*
Expand Down
6 changes: 5 additions & 1 deletion python/lsst/daf/base/propertyContainer/propertySet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ PYBIND11_MODULE(propertySet, mod) {
cls.def("isArray", &PropertySet::isArray);
cls.def("isUndefined", &PropertySet::isUndefined);
cls.def("isPropertySetPtr", &PropertySet::isPropertySetPtr);
cls.def("valueCount", &PropertySet::valueCount);
cls.def("valueCount",
py::overload_cast<>(&PropertySet::valueCount, py::const_));
cls.def("valueCount",
py::overload_cast<std::string const&>(&PropertySet::valueCount,
py::const_));
cls.def("typeOf", &PropertySet::typeOf, py::return_value_policy::reference);
cls.def("toString", &PropertySet::toString, "topLevelOnly"_a = false, "indent"_a = "");
cls.def("copy", &PropertySet::copy, "dest"_a, "source"_a, "name"_a, "asScalar"_a=false);
Expand Down
8 changes: 8 additions & 0 deletions src/PropertySet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ bool PropertySet::isUndefined(std::string const& name) const {
return i != _map.end() && i->second->back().type() == typeid(nullptr);
}

size_t PropertySet::valueCount() const {
size_t sum = 0;
for (auto const& name : paramNames(false)) {
sum += valueCount(name);
}
return sum;
}

size_t PropertySet::valueCount(std::string const& name) const {
auto const i = _find(name);
if (i == _map.end()) return 0;
Expand Down
17 changes: 17 additions & 0 deletions tests/test_PropertyList.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ def testScalar(self):
self.assertEqual(apl.typeOf("undef"), dafBase.PropertyList.TYPE_Undef)
self.assertIsNone(apl.get("undef"))
self.assertIsNone(apl["undef"])
self.assertEqual(apl.valueCount(), 12)
self.checkPickle(apl)

# Now replace the undef value with a defined value
apl.set("undef", "not undefined")
self.assertEqual(apl.getScalar("undef"), "not undefined")
self.assertFalse(apl.isUndefined("undef"))
self.assertEqual(apl.typeOf("undef"), dafBase.PropertyList.TYPE_String)
self.assertEqual(apl.valueCount(), 12)

def testGetDefault(self):
apl = dafBase.PropertyList()
Expand Down Expand Up @@ -138,12 +140,14 @@ def testGetVector(self):
self.assertEqual(apl["ints"], v[-1])
self.assertEqual(apl.getArray("ints"), v)
self.assertEqual(apl.getScalar("ints"), v[-1])
self.assertEqual(apl.valueCount(), 6)
apl.setInt("int", 999)
x = apl.get("int")
self.assertEqual(x, 999)
self.assertEqual(apl.getArray("int"), [999])
self.assertEqual(apl.getScalar("int"), 999)
self.assertEqual(apl["int"], 999)
self.assertEqual(apl.valueCount(), 7)

self.checkPickle(apl)

Expand Down Expand Up @@ -181,6 +185,7 @@ def testAddScalar(self):
self.assertEqual(apl.get("subclass"), 1.23456789)
self.assertEqual(apl.getArray("subclass"), [1.23456789])
self.assertEqual(apl.getScalar("subclass"), 1.23456789)
self.assertEqual(apl.valueCount(), 7)

def testDateTimeToString(self):
apl = dafBase.PropertyList()
Expand Down Expand Up @@ -229,6 +234,7 @@ def testAddVector(self):
self.assertEqual(apl.get("subclass"), subclass[-1])
self.assertEqual(apl.getArray("subclass"), subclass)
self.assertEqual(apl.getScalar("subclass"), subclass[-1])
self.assertEqual(apl.valueCount(), 9)

def testComment(self):
apl = dafBase.PropertyList()
Expand All @@ -242,6 +248,7 @@ def testComment(self):
self.assertEqual(apl.getArray("NAXIS"), [3])
self.assertEqual(apl.getScalar("NAXIS"), 3)
self.assertEqual(apl.getComment("NAXIS"), "three-dimensional")
self.assertEqual(apl.valueCount(), 1)

def testOrder(self):
apl = dafBase.PropertyList()
Expand Down Expand Up @@ -333,6 +340,7 @@ def testHierarchy(self):
self.assertEqual(apl.get("CURRENT.bar"), 2)
self.assertEqual(apl.getArray("CURRENT.bar"), [2])
self.assertEqual(apl.getScalar("CURRENT.bar"), 2)
self.assertEqual(apl.valueCount(), 3)

aps = dafBase.PropertySet()
aps.set("bottom", "x")
Expand All @@ -344,6 +352,7 @@ def testHierarchy(self):
self.assertEqual(apl.get("top.sibling"), 42)
self.assertEqual(apl.getArray("top.sibling"), [42])
self.assertEqual(apl.getScalar("top.sibling"), 42)
self.assertEqual(apl.valueCount(), 5)
with self.assertRaises(KeyError):
apl["top"]
self.assertEqual(apl.toString(),
Expand Down Expand Up @@ -383,21 +392,25 @@ def testCopy(self):
self.assertEqual(dest.get("destItem1"), value1[-1])
self.assertEqual(dest.getArray("destItem1"), value1)
self.assertEqual(dest.getScalar("destItem1"), value1[-1])
self.assertEqual(dest.valueCount(), 2)

# items are replaced, regardless of type
dest.set("destItem2", "string value")
self.assertEqual(dest.valueCount(), 3)
value2 = [5, -4, 3]
source.set("srcItem2", value2)
dest.copy("destItem2", source, "srcItem2")
self.assertEqual(dest.get("destItem2"), value2[-1])
self.assertEqual(dest.getArray("destItem2"), value2)
self.assertEqual(dest.getScalar("destItem2"), value2[-1])
self.assertEqual(dest.valueCount(), 5)

# asScalar copies only the last value
dest.copy("destItem2Scalar", source, "srcItem2", asScalar=True)
self.assertEqual(dest.get("destItem2Scalar"), value2[-1])
self.assertEqual(dest.getArray("destItem2Scalar"), [value2[-1]])
self.assertEqual(dest.getScalar("destItem2Scalar"), value2[-1])
self.assertEqual(dest.valueCount(), 6)

def testArrayProperties(self):
apl = dafBase.PropertyList()
Expand All @@ -416,6 +429,7 @@ def testArrayProperties(self):
self.assertEqual(apl.typeOf("ints"), dafBase.PropertyList.TYPE_Int)
self.assertEqual(apl.typeOf("int"), dafBase.PropertyList.TYPE_Int)
self.assertEqual(apl.typeOf("ints2"), dafBase.PropertyList.TYPE_Int)
self.assertEqual(apl.valueCount(), 6)

def testHierarchy2(self):
apl = dafBase.PropertyList()
Expand Down Expand Up @@ -612,6 +626,7 @@ def testCombine(self):
self.assertEqual(v, [1, 3, 4])
v = apl.getArray("int")
self.assertEqual(v, [42, 2008])
self.assertEqual(apl.valueCount(), 12)

def testUpdate(self):
apl = dafBase.PropertyList()
Expand Down Expand Up @@ -652,13 +667,15 @@ def testUpdate(self):
self.assertEqual(v, [3, 4])
v = apl.getArray("int")
self.assertEqual(v, [2008])
self.assertEqual(apl.valueCount(), 10)

apld = {"int": 100, "str": "String", "apl1.foo": 10.5}
apl.update(apld)
self.assertEqual(apl["int"], apld["int"])
self.assertEqual(apl["str"], apld["str"])
self.assertEqual(apl["apl1.foo"], apld["apl1.foo"])
self.assertEqual(apl["double"], 3.14)
self.assertEqual(apl.valueCount(), 12)

def testCombineThrow(self):
apl = dafBase.PropertyList()
Expand Down
19 changes: 19 additions & 0 deletions tests/test_PropertySet_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def testScalar(self):
self.assertEqual(ps.typeOf("dt"), dafBase.PropertySet.TYPE_DateTime)
self.assertEqual(ps.getDateTime("dt").nsecs(), 1238657233314159265)
self.assertEqual(ps.getString("blank"), "")
self.assertEqual(ps.valueCount(), 13)

self.assertIsNone(ps.getScalar("undef"))
self.assertEqual(ps.typeOf("undef"), dafBase.PropertyList.TYPE_Undef)
Expand Down Expand Up @@ -126,6 +127,7 @@ def testNumPyScalars(self):
self.assertAlmostEqual(ps.getFloat("float"), 3.14159, 6)
self.assertEqual(ps.typeOf("double"), dafBase.PropertySet.TYPE_Double)
self.assertEqual(ps.getDouble("double"), 2.718281828459045)
self.assertEqual(ps.valueCount(), 6)
self.checkPickle(ps)

def testGetDefault(self):
Expand Down Expand Up @@ -161,12 +163,14 @@ def testGetVector(self):
self.assertEqual(ps.getArray("ints"), v)
self.assertEqual(ps.getScalar("ints"), v[-1])
self.assertEqual(ps["ints"], v[-1])
self.assertEqual(ps.valueCount(), 6)
ps.setInt("int", 999)
x = ps.get("int")
self.assertEqual(x, 999)
self.assertEqual(ps.getArray("int"), [999])
self.assertEqual(ps.getScalar("int"), 999)
self.assertEqual(ps["int"], 999)
self.assertEqual(ps.valueCount(), 7)
self.checkPickle(ps)

def testGetVector2(self):
Expand Down Expand Up @@ -198,6 +202,7 @@ def testAddScalar(self):
self.assertEqual(w[3], -999)
self.assertEqual(w[4], 13)
self.assertEqual(ps.getString("other"), "foo")
self.assertEqual(ps.valueCount(), 6)
self.checkPickle(ps)

def testSetAddVector(self):
Expand All @@ -222,6 +227,7 @@ def testSetAddVector(self):
self.assertEqual(ps.get("strs"), strArr[-1])
self.assertEqual(ps.getArray("strs"), strArr)
self.assertEqual(ps.getScalar("strs"), strArr[-1])
self.assertEqual(ps.valueCount(), 11)

ps.add("bools", list(reversed(boolArr)))
ps.add("ints", list(reversed(intArr)))
Expand All @@ -239,6 +245,7 @@ def testSetAddVector(self):
self.assertEqual(ps.get("strs"), strArr[0])
self.assertEqual(ps.getArray("strs"), strArr + list(reversed(strArr)))
self.assertEqual(ps.getScalar("strs"), strArr[0])
self.assertEqual(ps.valueCount(), 22)
self.checkPickle(ps)

def testDateTimeToString(self):
Expand Down Expand Up @@ -269,17 +276,20 @@ def testSubPS(self):
self.assertEqual(ps.get("b.a"), 1)
self.assertEqual(ps.getArray("b.a"), [1])
self.assertEqual(ps.getScalar("b.a"), 1)
self.assertEqual(ps.valueCount(), 1)
ps.set("c", ps1)
self.assertEqual(ps.getArray("c"), ps1)
self.assertEqual(ps.getScalar("c"), ps1)
self.assertEqual(ps.get("c.a"), 1)
self.assertEqual(ps.getArray("c.a"), [1])
self.assertEqual(ps.getScalar("c.a"), 1)
self.assertEqual(ps.valueCount(), 2)
ps.set("c.a", 2)
self.assertEqual(ps.get("b.a"), 2)
self.assertEqual(ps.getArray("b.a"), [2])
self.assertEqual(ps.getScalar("b.a"), 2)
self.assertEqual(ps.get("b").get("a"), 2)
self.assertEqual(ps.valueCount(), 2)
self.checkPickle(ps)

def testCopy(self):
Expand All @@ -291,6 +301,8 @@ def testCopy(self):
self.assertEqual(dest.get("destItem1"), value1[-1])
self.assertEqual(dest.getArray("destItem1"), value1)
self.assertEqual(dest.getScalar("destItem1"), value1[-1])
self.assertEqual(source.valueCount(), 2)
self.assertEqual(dest.valueCount(), 2)

# items are replaced, regardless of type
dest.set("destItem2", "string value")
Expand All @@ -300,12 +312,16 @@ def testCopy(self):
self.assertEqual(dest.get("destItem2"), value2[-1])
self.assertEqual(dest.getArray("destItem2"), value2)
self.assertEqual(dest.getScalar("destItem2"), value2[-1])
self.assertEqual(source.valueCount(), 5)
self.assertEqual(dest.valueCount(), 5)

# asScalar copies only the last value
dest.copy("destItem2Scalar", source, "srcItem2", asScalar=True)
self.assertEqual(dest.get("destItem2Scalar"), value2[-1])
self.assertEqual(dest.getArray("destItem2Scalar"), [value2[-1]])
self.assertEqual(dest.getScalar("destItem2Scalar"), value2[-1])
self.assertEqual(source.valueCount(), 5)
self.assertEqual(dest.valueCount(), 6)


class FlatTestCase(unittest.TestCase):
Expand Down Expand Up @@ -504,6 +520,7 @@ def testCombine(self):

self.assertEqual(ps.valueCount("ps1.pre"), 3)
self.assertEqual(ps.valueCount("int"), 2)
self.assertEqual(ps.valueCount(), 12)

v = ps.getArray("ps1.pre")
self.assertEqual(v, [1, 3, 4])
Expand Down Expand Up @@ -553,6 +570,7 @@ def testUpdate(self):

self.assertEqual(ps.valueCount("ps1.pre"), 2)
self.assertEqual(ps.valueCount("int"), 1)
self.assertEqual(ps.valueCount(), 7)

v = ps.getArray("ps1.pre")
self.assertEqual(v, [3, 4])
Expand Down Expand Up @@ -656,6 +674,7 @@ def testArrayProperties(self):
self.assertEqual(ps.valueCount("ints"), 3)
self.assertEqual(ps.valueCount("int"), 1)
self.assertEqual(ps.valueCount("ints2"), 2)
self.assertEqual(ps.valueCount(), 6)
self.assertEqual(ps.typeOf("ints"), dafBase.PropertySet.TYPE_Int)
self.assertEqual(ps.typeOf("int"), dafBase.PropertySet.TYPE_Int)
self.assertEqual(ps.typeOf("ints2"), dafBase.PropertySet.TYPE_Int)
Expand Down

0 comments on commit f910f65

Please sign in to comment.