Skip to content

Commit

Permalink
Implement GenericMap.__iter__ in C++.
Browse files Browse the repository at this point in the history
Having GenericMap::keys() be a view makes it easy to iterate from C++,
which the style guide recommends.
  • Loading branch information
kfindeisen committed May 18, 2019
1 parent 3ac2730 commit bd25220
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
4 changes: 3 additions & 1 deletion python/lsst/afw/typehandling/_GenericMap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ void declareGenericMap(utils::python::WrapperCollection& wrappers, std::string c
// Prevent segfaults when assigning a key<Storable> to Python variable, then deleting from map
// No existing code depends on being able to modify an item stored by value
"key"_a, "default"_a = py::none(), py::return_value_policy::copy);
// __iter__ easier to implement in Python
cls.def("__iter__",
[](Class const& self) { return py::make_iterator(self.keys().begin(), self.keys().end()); },
py::keep_alive<0, 1>());
cls.def("__len__", &Class::size);
cls.def("__bool__", [](Class const& self) { return !self.empty(); });
// Private to let a proper view be defined in Python
Expand Down
4 changes: 0 additions & 4 deletions python/lsst/afw/typehandling/_GenericMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ def __repr__(self):
className = type(self).__name__
return className + "({" + ", ".join("%r: %r" % (key, value) for key, value in self.items()) + "})"

def __iter__(self):
for key in self._keys():
yield key

# Support equality with any Mapping, including dict
# Not clear why Mapping.__eq__ doesn't work
def __eq__(self, other):
Expand Down

0 comments on commit bd25220

Please sign in to comment.