Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-21357: Add value and items methods to PropertySet/List #54

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import enum
import numbers
from collections.abc import Mapping, KeysView
from collections.abc import Mapping, KeysView, ValuesView, ItemsView

# Ensure that C++ exceptions are properly translated to Python
import lsst.pex.exceptions # noqa: F401
Expand Down Expand Up @@ -600,6 +600,12 @@ def __iter__(self):
def keys(self):
return KeysView(self)

def items(self):
return ItemsView(self)

def values(self):
return ValuesView(self)

def __reduce__(self):
# It would be a bit simpler to use __setstate__ and __getstate__.
# However, implementing __setstate__ in Python causes segfaults
Expand Down
17 changes: 17 additions & 0 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ def testDictPropertySet(self):
for k in keys:
self.assertIn(k, container)

for k, v in container.items():
self.assertIn(k, container)
self.assertEqual(v, container[k])

# Check that string form of the non-PropertySet values are present
# when iterating over values. This is a simple test to ensure
# that values() does do something useful
values = {str(container[k]) for k in container if not isinstance(container[k],
lsst.daf.base.PropertySet)}
for v in container.values():
if not isinstance(v, lsst.daf.base.PropertySet):
self.assertIn(str(v), values)

# Assign a PropertySet
ps2 = lsst.daf.base.PropertySet()
ps2.setString("newstring", "stringValue")
Expand Down Expand Up @@ -163,6 +176,10 @@ def testDictPropertyList(self):
for k in keys:
self.assertIn(k, container)

for k, v in container.items():
self.assertIn(k, container)
self.assertEqual(v, container[k])

# Assign a PropertySet
ps2 = lsst.daf.base.PropertySet()
ps2["newstring"] = "stringValue"
Expand Down