|
16 | 16 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | // |
18 | 18 |
|
| 19 | +#include <stdexcept> |
| 20 | + |
19 | 21 | // libsemigroups headers |
20 | 22 | #include <libsemigroups/ranges.hpp> |
21 | 23 | #include <libsemigroups/transf.hpp> |
|
27 | 29 |
|
28 | 30 | // libsemigroups_pybind11.... |
29 | 31 | #include "debug.hpp" |
30 | | -#include "main.hpp" // for init_transf |
| 32 | +#include "errors.hpp" // for formatted_error_message |
| 33 | +#include "main.hpp" // for init_transf |
31 | 34 |
|
32 | 35 | namespace libsemigroups { |
33 | 36 |
|
@@ -104,16 +107,27 @@ the image of the point ``i`` under the {0} is ``imgs[i]``. |
104 | 107 | "__getitem__", |
105 | 108 | [](PTransfSubclass const& a, |
106 | 109 | size_t b) -> int_or_unsigned_constant<Scalar> { |
107 | | - auto result = a.at(b); |
108 | | - if (result != UNDEFINED) { |
109 | | - return {result}; |
| 110 | + // __getitem__ is expected by python to throw an IndexError which |
| 111 | + // corresponds to a std::out_of_range for things like list(a) to |
| 112 | + // work. |
| 113 | + try { |
| 114 | + auto result = a.at(b); |
| 115 | + if (result != UNDEFINED) { |
| 116 | + return {result}; |
| 117 | + } |
| 118 | + return {UNDEFINED}; |
| 119 | + } catch (LibsemigroupsException const& e) { |
| 120 | + throw std::out_of_range(formatted_error_message(e)); |
110 | 121 | } |
111 | | - return {UNDEFINED}; |
112 | 122 | }, |
113 | 123 | py::is_operator()); |
114 | 124 |
|
115 | 125 | thing.def("__hash__", &PTransfSubclass::hash_value, py::is_operator()); |
116 | 126 |
|
| 127 | + thing.def("__iter__", [](PTransfSubclass const& self) { |
| 128 | + return py::make_iterator(self.begin(), self.end()); |
| 129 | + }); |
| 130 | + |
117 | 131 | //////////////////////////////////////////////////////////////////////// |
118 | 132 | // Non-special methods |
119 | 133 | //////////////////////////////////////////////////////////////////////// |
@@ -179,6 +193,7 @@ yielding these values. |
179 | 193 | doc_type_name, |
180 | 194 | long_name) |
181 | 195 | .c_str()); |
| 196 | + |
182 | 197 | thing.def( |
183 | 198 | "increase_degree_by", |
184 | 199 | [](PTransfSubclass& self, size_t m) -> PTransfSubclass& { |
@@ -718,7 +733,7 @@ fewer points requiring less space per point. |
718 | 733 | m.def("transf_inverse", |
719 | 734 | py::overload_cast<Perm_ const&>(&inverse<N, Scalar>)); |
720 | 735 | } // bind_perm |
721 | | - } // namespace |
| 736 | + } // namespace |
722 | 737 |
|
723 | 738 | void init_transf(py::module& m) { |
724 | 739 | // Transformations |
|
0 commit comments