Skip to content

Commit e406173

Browse files
Add TODOs
1 parent 68c21f8 commit e406173

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

libsemigroups_pybind11/action.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@
5757
may_return_wrapped_cxx_obj as _may_return_wrapped_cxx_obj,
5858
register_cxx_wrapped_type as _register_cxx_wrapped_type,
5959
to_cxx as _to_cxx,
60-
to_py as _to_py,
60+
to_py_new as _to_py,
6161
)
6262

63+
from .detail.decorators import copydoc as _copydoc
64+
6365

6466
########################################################################
6567
# Action python class
@@ -209,9 +211,7 @@ def __init__(
209211
if _to_cxx(self) is not None:
210212
return
211213
if len(args) != 0:
212-
raise ValueError(
213-
f"expected 0 positional arguments, but found {len(args)}"
214-
)
214+
raise ValueError(f"expected 0 positional arguments, but found {len(args)}")
215215
if not isinstance(generators, list):
216216
raise TypeError(
217217
"expected the keyword argument 'generators' to be "
@@ -257,6 +257,17 @@ def __len__(self: _Self) -> int:
257257
def __contains__(self: _Self, pt: Point) -> bool:
258258
return self.position(pt) != _UNDEFINED
259259

260+
########################################################################
261+
# Iterators
262+
########################################################################
263+
264+
@_copydoc(_RightActionPPerm1PPerm1.generators)
265+
def generators(self: _Self) -> Iterator[Element]:
266+
return map(
267+
_to_py,
268+
_to_cxx(self).generators(),
269+
)
270+
260271

261272
########################################################################
262273
# Copy mem fns from sample C++ type and register types

libsemigroups_pybind11/detail/cxx_wrapper.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ def __getattr__(self: Self, name: str):
131131
def cxx_fn_wrapper(*args) -> Any:
132132
if len(args) == 1 and isinstance(args[0], list):
133133
args = args[0]
134-
return getattr(self._cxx_obj, name)(
135-
[to_cxx(x) for x in args]
136-
)
134+
return getattr(self._cxx_obj, name)([to_cxx(x) for x in args])
137135
return getattr(self._cxx_obj, name)(*(to_cxx(x) for x in args))
138136

139137
return cxx_fn_wrapper
@@ -164,6 +162,7 @@ def __eq__(self: Self, that) -> bool:
164162
)
165163
raise NameError("_cxx_obj has not been defined")
166164

165+
# TODO rm
167166
def _cxx_obj_type_from(self: Self, samples=(), types=()) -> Any:
168167
py_types = tuple([type(x) for x in samples] + list(types))
169168
lookup = self._py_template_params_to_cxx_type
@@ -199,9 +198,9 @@ def init_cxx_obj(self: Self, *args) -> None:
199198
defined.
200199
"""
201200
assert self.py_template_params is not None
202-
self._cxx_obj = self._py_template_params_to_cxx_type[
203-
self.py_template_params
204-
](*(to_cxx(x) for x in args))
201+
self._cxx_obj = self._py_template_params_to_cxx_type[self.py_template_params](
202+
*(to_cxx(x) for x in args)
203+
)
205204

206205

207206
# TODO proper annotations
@@ -217,9 +216,7 @@ def cxx_mem_fn_wrapper(self, *args):
217216
# TODO move the first if-clause into to_cxx?
218217
if len(args) == 1 and isinstance(args[0], list):
219218
args = [[to_cxx(x) for x in args[0]]]
220-
result = getattr(to_cxx(self), cxx_mem_fn.__name__)(
221-
*(to_cxx(x) for x in args)
222-
)
219+
result = getattr(to_cxx(self), cxx_mem_fn.__name__)(*(to_cxx(x) for x in args))
223220
if result is to_cxx(self):
224221
return self
225222
if type(result) in _CXX_WRAPPED_TYPE_TO_PY_TYPE:
@@ -265,8 +262,7 @@ def copy_cxx_mem_fns(cxx_class: pybind11_type, py_class: CxxWrapper) -> None:
265262
"""
266263
for py_meth_name in dir(cxx_class):
267264
if (
268-
(not py_meth_name.startswith("_"))
269-
and py_meth_name not in dir(py_class)
265+
(not py_meth_name.startswith("_")) and py_meth_name not in dir(py_class)
270266
# and type(getattr(cxx_class, py_meth_name)) is MethodType
271267
):
272268
setattr(

src/action.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,24 @@ Returns the number of generators.
208208
:rtype:
209209
int
210210
)pbdoc");
211-
thing.def("generators",
212-
&Action_::generators,
213-
R"pbdoc(
214-
:sig=(self: Action) -> List[Element]:
211+
thing.def(
212+
"generators",
213+
[](Action_ const& self) {
214+
return py::make_iterator(self.generators().cbegin(),
215+
self.generators().cend());
216+
},
217+
R"pbdoc(
218+
:sig=(self: Action) -> Iterator[Element]:
215219
216-
Returns the list of generators.
220+
Returns an iterator yielding the generators.
217221
218222
:complexity:
219223
Constant.
220224
221225
:returns:
222-
The generators.
226+
An iterator yielding the generators.
223227
:rtype:
224-
List[Element]
228+
Iterator[Element]
225229
)pbdoc");
226230
thing.def("position",
227231
&Action_::position,

0 commit comments

Comments
 (0)