Skip to content

Commit d12c0fe

Browse files
Renovating adapters x1
1 parent e406173 commit d12c0fe

File tree

8 files changed

+153
-147
lines changed

8 files changed

+153
-147
lines changed

libsemigroups_pybind11/action.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def __init__(
202202
"""
203203

204204
super().__init__(
205+
*args,
205206
required_kwargs=("func", "side", "seeds", "generators"),
206207
generators=generators,
207208
seeds=seeds,

libsemigroups_pybind11/adapters.py

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,68 @@
1313
various adapters from libsemigroups.
1414
"""
1515

16-
from typing import Any
16+
from typing import Any, Union, Iterator, TypeVar as _TypeVar, List
1717

18-
from typing_extensions import Self
18+
from typing_extensions import Self as _Self
1919

2020
from _libsemigroups_pybind11 import (
21-
ImageRightActionBMat8BMat8 as _ImageRightActionBMat8BMat8,
22-
ImageLeftActionBMat8BMat8 as _ImageLeftActionBMat8BMat8,
23-
ImageRightActionPPerm1PPerm1 as _ImageRightActionPPerm1PPerm1,
24-
ImageLeftActionPPerm1PPerm1 as _ImageLeftActionPPerm1PPerm1,
25-
ImageRightActionPPerm1List as _ImageRightActionPPerm1List,
2621
# TODO Transf
2722
# TODO other pperms
2823
BMat8 as _BMat8,
24+
ImageLeftActionBMat8BMat8 as _ImageLeftActionBMat8BMat8,
25+
ImageLeftActionPPerm1PPerm1 as _ImageLeftActionPPerm1PPerm1,
26+
ImageRightActionBMat8BMat8 as _ImageRightActionBMat8BMat8,
27+
ImageRightActionPPerm1List as _ImageRightActionPPerm1List,
28+
ImageRightActionPPerm1PPerm1 as _ImageRightActionPPerm1PPerm1,
2929
PPerm1 as _PPerm1,
3030
)
3131

32-
from .detail.cxx_wrapper import CxxWrapper, to_cxx, to_py
32+
from .detail.cxx_wrapper import (
33+
CxxWrapper as _CxxWrapper,
34+
to_cxx as _to_cxx,
35+
to_py_new as _to_py,
36+
)
3337

3438
from .tools import ordinal
35-
from .transf import PPerm
36-
37-
38-
class _ImageAction(CxxWrapper):
39-
def __init__(self: Self, **kwargs):
40-
super().__init__(required_kwargs=("Element", "Point"), **kwargs)
41-
self.Element = kwargs["Element"]
42-
self.Point = kwargs["Point"]
43-
44-
def _init_cxx_obj(self: Self, elt: Any, pt: Any) -> Any:
45-
cxx_obj_t = self._cxx_obj_type_from(samples=(elt, pt))
46-
if self._cxx_obj is None or not isinstance(self._cxx_obj, cxx_obj_t):
47-
self._cxx_obj = cxx_obj_t()
48-
return self._cxx_obj
49-
50-
def __call__( # pylint: disable=inconsistent-return-statements
51-
self: Self, *args
52-
):
53-
# Point1, Point2, Element -> Point1 = Point2 ^ Element
54-
if 2 > len(args) or len(args) > 3:
55-
raise TypeError(f"expected 2 or 3 arguments, found {len(args)}")
56-
pt = args[-2]
57-
x = args[-1]
58-
if not isinstance(pt, self.Point):
59-
raise ValueError(
60-
f"the {ordinal(len(args) - 2)} argument (point) has incorrect type, "
61-
f"expected {self.Point} but found {type(pt)}"
62-
)
63-
if not isinstance(x, self.Element):
64-
raise ValueError(
65-
f"the {ordinal(len(args) - 1)} argument (element) has incorrect type, "
66-
f"expected {self.Element} but found {type(x)}"
67-
)
68-
if len(args) == 3:
69-
res = args[0]
70-
if not isinstance(res, self.Point):
71-
raise ValueError(
72-
"the 1st argument (result) has incorrect type, "
73-
f"expected {self.Point} but found {type(res)}"
74-
)
75-
if len(args) == 3 and self.Point is list:
76-
raise NotImplementedError("not yet implemented")
77-
78-
self._init_cxx_obj(x, pt)
79-
return to_py(
80-
self.Element, self._cxx_obj(*(to_cxx(arg) for arg in args))
39+
from .transf import PPerm as _PPerm
40+
41+
42+
class _ImageAction(_CxxWrapper):
43+
"""
44+
This is a protected base class for ImageRightAction and ImageLeftAction.
45+
"""
46+
47+
Element = _TypeVar("Element")
48+
Point = _TypeVar("Point")
49+
50+
def __init__(self: _Self, *args, point=None, element=None) -> None:
51+
"""
52+
TODO
53+
Note to self <point> and <element> are the types of the objects used by
54+
this function not examples of such objects.
55+
"""
56+
super().__init__(
57+
*args,
58+
required_kwargs=("element", "point"),
59+
point=point,
60+
element=element,
61+
)
62+
if _to_cxx(self) is not None:
63+
return
64+
if len(args) != 0:
65+
raise ValueError(f"expected 0 positional arguments, but found {len(args)}")
66+
self.py_template_params = (
67+
type(_to_cxx(element)),
68+
type(_to_cxx(point)),
8169
)
70+
self.init_cxx_obj()
71+
72+
def __call__(self: _Self, *args):
73+
return _to_py(_to_cxx(self)(*(_to_cxx(x) for x in args)))
8274

8375

8476
class ImageRightAction(_ImageAction):
85-
# pylint: disable=too-few-public-methods, unused-private-member
86-
"""
77+
"""TODO update
8778
Construct a ImageRightAction instance.
8879
8980
:Keyword Arguments:
@@ -93,18 +84,22 @@ class ImageRightAction(_ImageAction):
9384

9485
_py_template_params_to_cxx_type = {
9586
(_BMat8, _BMat8): _ImageRightActionBMat8BMat8,
96-
(PPerm, PPerm): {
97-
(_PPerm1, _PPerm1): _ImageRightActionPPerm1PPerm1,
98-
},
99-
(PPerm, list): {
100-
(_PPerm1, list): _ImageRightActionPPerm1List,
101-
},
87+
(_PPerm1, _PPerm1): _ImageRightActionPPerm1PPerm1,
88+
(_PPerm1, list): _ImageRightActionPPerm1List,
10289
}
10390

91+
_cxx_type_to_py_template_params = dict(
92+
zip(
93+
_py_template_params_to_cxx_type.values(),
94+
_py_template_params_to_cxx_type.keys(),
95+
)
96+
)
10497

105-
class ImageLeftAction(_ImageAction): # pylint: disable=invalid-name
106-
# pylint: disable=too-few-public-methods, unused-private-member
107-
"""
98+
_all_wrapped_cxx_types = {*_py_template_params_to_cxx_type.values()}
99+
100+
101+
class ImageLeftAction(_ImageAction):
102+
"""TODO update
108103
Construct a ImageLeftAction instance.
109104
110105
:Keyword Arguments:
@@ -114,7 +109,14 @@ class ImageLeftAction(_ImageAction): # pylint: disable=invalid-name
114109

115110
_py_template_params_to_cxx_type = {
116111
(_BMat8, _BMat8): _ImageLeftActionBMat8BMat8,
117-
(PPerm, PPerm): {
118-
(_PPerm1, _PPerm1): _ImageLeftActionPPerm1PPerm1,
119-
},
112+
(_PPerm1, _PPerm1): _ImageLeftActionPPerm1PPerm1,
120113
}
114+
115+
_cxx_type_to_py_template_params = dict(
116+
zip(
117+
_py_template_params_to_cxx_type.values(),
118+
_py_template_params_to_cxx_type.keys(),
119+
)
120+
)
121+
122+
_all_wrapped_cxx_types = {*_py_template_params_to_cxx_type.values()}

libsemigroups_pybind11/detail/cxx_wrapper.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ def copy_cxx_mem_fns(cxx_class: pybind11_type, py_class: CxxWrapper) -> None:
277277
########################################################################
278278

279279

280+
# TODO remove usage of this just call to_py_new directly
280281
def may_return_wrapped_cxx_obj(method):
281282
"""
282283
Decorator for methods that might return a wrapped C++ object. If a wrapped
@@ -287,9 +288,6 @@ def may_return_wrapped_cxx_obj(method):
287288

288289
@wraps(method)
289290
def wrapper(self, *args):
290-
result = method(self, *args)
291-
if type(result) in _CXX_WRAPPED_TYPE_TO_PY_TYPE:
292-
return _CXX_WRAPPED_TYPE_TO_PY_TYPE[type(result)](result)
293-
return result
291+
return to_py_new(method(self, *args))
294292

295293
return wrapper

src/action.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,15 @@ Returns a Gabow object for strongly connected components.
468468
:rtype:
469469
Gabow
470470
)pbdoc");
471+
472+
thing.def(
473+
"apply",
474+
[](Action_ const& self,
475+
const_reference_point_type pt,
476+
Element const& x) { return self.apply(pt, x); },
477+
R"pbdoc(
478+
TODO
479+
)pbdoc");
471480
} // bind_action
472481

473482
template <typename Element, typename Point>

src/adapters.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,30 @@ right action of the element *x*.
6262
.. doctest::
6363
6464
>>> from libsemigroups_pybind11 import PPerm, ImageRightAction
65-
>>> func = ImageRightAction(Point=PPerm, Element=PPerm)
6665
>>> x = PPerm([0, 1, 2, 3, 5, 6, 9], [9, 7, 3, 5, 4, 2, 1], 10)
6766
>>> pt = PPerm([0, 1, 2, 3, 5, 6], [0, 1, 2, 3, 5, 6], 10)
67+
>>> func = ImageRightAction(point=pt, element=x)
6868
>>> func(pt, x)
6969
PPerm([2, 3, 4, 5, 7, 9], [2, 3, 4, 5, 7, 9], 10)
70+
>>> func(_, x)
71+
PPerm([1, 3, 4, 5], [1, 3, 4, 5], 10)
72+
>>> func(_, x)
73+
PPerm([4, 5, 7], [4, 5, 7], 10)
74+
>>> func(_, x)
75+
PPerm([4], [4], 10)
76+
>>> func(_, x)
77+
PPerm([], [], 10)
78+
>>> func(_, x)
79+
PPerm([], [], 10)
7080
)pbdoc")
7181
.def(py::init<>())
72-
.def("__call__",
73-
[](ImageRightAction_ const& self,
74-
Point& res,
75-
Point const& pt,
76-
Element const& x) -> void { self(res, pt, x); })
82+
// The following doesn't yet work because mostly it's not possible to
83+
// change <res> in place.
84+
// .def("__call__",
85+
// [](ImageRightAction_ const& self,
86+
// Point& res,
87+
// Point const& pt,
88+
// Element const& x) -> void { self(res, pt, x); })
7789
.def("__call__",
7890
[](ImageRightAction_ const& self,
7991
Point const& pt,
@@ -88,7 +100,7 @@ right action of the element *x*.
88100
template <typename Element, typename Point>
89101
void bind_imageleftaction(py::module& m, std::string const& name) {
90102
using ImageLeftAction_ = ImageLeftAction<Element, Point>;
91-
// TODO doc
103+
92104
py::class_<ImageLeftAction_>(m,
93105
name.c_str(),
94106
R"pbdoc(
@@ -114,18 +126,20 @@ left action of the element *x*.
114126
.. doctest::
115127
116128
>>> from libsemigroups_pybind11 import PPerm, ImageLeftAction
117-
>>> func = ImageLeftAction(Point=PPerm, Element=PPerm)
118129
>>> x = PPerm([0, 1, 2, 3, 5, 6, 9], [9, 7, 3, 5, 4, 2, 1], 10)
119130
>>> pt = PPerm([0, 1, 2, 3, 5, 6], [0, 1, 2, 3, 5, 6], 10)
131+
>>> func = ImageLeftAction(point=pt, element=x)
120132
>>> func(pt, x)
121133
PPerm([2, 3, 6, 9], [2, 3, 6, 9], 10)
122134
)pbdoc")
123135
.def(py::init<>())
124-
.def("__call__",
125-
[](ImageLeftAction_ const& self,
126-
Point& res,
127-
Point const& pt,
128-
Element const& x) { self(res, pt, x); })
136+
// The following doesn't yet work because mostly it's not possible to
137+
// change <res> in place.
138+
// .def("__call__",
139+
// [](ImageLeftAction_ const& self,
140+
// Point& res,
141+
// Point const& pt,
142+
// Element const& x) { self(res, pt, x); })
129143
.def("__call__",
130144
[](ImageLeftAction_ const& self,
131145
Point const& pt,

0 commit comments

Comments
 (0)