|
9 | 9 | # pylint: disable=no-name-in-module, invalid-name, unused-import, fixme
|
10 | 10 |
|
11 | 11 | """
|
12 |
| -This package provides the user-facing python part of libsemigroups_pybind11 for |
13 |
| -the stephen namespace from libsemigroups. |
| 12 | +This page contains the documentation for various helper functions for |
| 13 | +manipulating :any:`Stephen` objects. All such functions |
| 14 | +are contained in the submodule ``stephen``. |
14 | 15 | """
|
15 | 16 |
|
| 17 | +from typing_extensions import Self |
| 18 | + |
16 | 19 | from _libsemigroups_pybind11 import (
|
17 | 20 | PresentationWords as _PresentationWords,
|
18 | 21 | InversePresentationWords as _InversePresentationWords,
|
19 | 22 | StephenPresentationWords as _StephenPresentationWords,
|
20 | 23 | StephenInversePresentationWords as _StephenInversePresentationWords,
|
21 |
| - accepts, |
22 |
| - dot, |
23 |
| - is_left_factor, |
24 |
| - left_factors, |
25 |
| - number_of_left_factors, |
26 |
| - number_of_words_accepted, |
27 |
| - words_accepted, |
| 24 | + # TODO rename with stephen_ prefix |
| 25 | + accepts as _accepts, |
| 26 | + dot as _dot, |
| 27 | + is_left_factor as _is_left_factor, |
| 28 | + left_factors as _left_factors, |
| 29 | + number_of_left_factors as _number_of_left_factors, |
| 30 | + number_of_words_accepted as _number_of_words_accepted, |
| 31 | + words_accepted as _words_accepted, |
| 32 | +) |
| 33 | + |
| 34 | +from libsemigroups_pybind11 import ( |
| 35 | + Presentation as _Presentation, |
| 36 | + InversePresentation as _InversePresentation, |
| 37 | +) |
| 38 | + |
| 39 | +from .detail.decorators import copydoc as _copydoc |
| 40 | +from .detail.cxx_wrapper import ( |
| 41 | + CxxWrapper as _CxxWrapper, |
| 42 | + to_cxx as _to_cxx, |
| 43 | + copy_cxx_mem_fns as _copy_cxx_mem_fns, |
| 44 | + wrap_cxx_free_fn as _wrap_cxx_free_fn, |
| 45 | + register_cxx_wrapped_type as _register_cxx_wrapped_type, |
28 | 46 | )
|
29 | 47 |
|
| 48 | +######################################################################## |
| 49 | +# The Stephen python class |
| 50 | +######################################################################## |
| 51 | + |
30 | 52 |
|
31 | 53 | # TODO(2): Make this work with string presentations once it works
|
32 |
| -# TODO(0): Change this to a proper class similar to what's done with Kambites |
33 |
| -# (once the proper classes PR is merged) |
34 |
| -def Stephen(*args): # pylint: disable=invalid-name |
35 |
| - """Construct a Stephen instance of the type specified by its arguments. |
36 |
| -
|
37 |
| - Options for calling this function are: |
38 |
| - 1 Stephen(presentation: PresentationWords) |
39 |
| - 2 Stephen(presentation: InversePresentationWords) |
40 |
| - 3 Stephen(presentation: StephenPresentationWords) |
41 |
| - 4 Stephen(presentation: StephenInversePresentationWords) |
42 |
| -
|
43 |
| - In cases 1 and 2 a new Stephen object is constructed with the given |
44 |
| - presentation. In cases 3 and 4 the Stephen object is constructed by copying |
45 |
| - an existing Stephen object. In cases 1 and 3 a StephenPresentationWords |
46 |
| - object is returned. In cases 2 and 4 a StephenInversePresentationWords |
47 |
| - object is returned. |
48 |
| - """ |
49 |
| - |
50 |
| - if len(args) != 1: |
51 |
| - raise ValueError(f"expected 1 argument, but got {len(args)}") |
52 |
| - |
53 |
| - presentation_or_stephen = args[0] |
54 |
| - # Order important here due to inheritance |
55 |
| - if isinstance( |
56 |
| - presentation_or_stephen, |
57 |
| - (_InversePresentationWords, _StephenInversePresentationWords), |
58 |
| - ): |
59 |
| - PresentationType = _InversePresentationWords |
60 |
| - elif isinstance( |
61 |
| - presentation_or_stephen, (_PresentationWords, _StephenPresentationWords) |
62 |
| - ): |
63 |
| - PresentationType = _PresentationWords |
64 |
| - else: |
65 |
| - raise TypeError( |
66 |
| - f"expected first argument to have type in {{PresentationWords, " |
67 |
| - f"InversePresentationWords, StephenPresentationWords, " |
68 |
| - f"StephenInversePresentationWords}}, but found {type(presentation_or_stephen)}" |
69 |
| - ) |
| 54 | +class Stephen(_CxxWrapper): |
| 55 | + __doc__ = _StephenPresentationWords.__doc__ |
70 | 56 |
|
71 |
| - cpp_type = { |
72 |
| - _PresentationWords: _StephenPresentationWords, |
73 |
| - _InversePresentationWords: _StephenInversePresentationWords, |
| 57 | + _py_template_params_to_cxx_type = { |
| 58 | + (_PresentationWords,): _StephenPresentationWords, |
| 59 | + (_InversePresentationWords,): _StephenInversePresentationWords, |
74 | 60 | }
|
75 | 61 |
|
76 |
| - return cpp_type[PresentationType](presentation_or_stephen) |
| 62 | + _cxx_type_to_py_template_params = dict( |
| 63 | + zip( |
| 64 | + _py_template_params_to_cxx_type.values(), |
| 65 | + _py_template_params_to_cxx_type.keys(), |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + _all_wrapped_cxx_types = {*_py_template_params_to_cxx_type.values()} |
| 70 | + |
| 71 | + @_copydoc(_StephenPresentationWords.__init__) |
| 72 | + def __init__(self: Self, *args, **kwargs) -> None: |
| 73 | + # TODO remove the doc string |
| 74 | + """Construct a Stephen instance of the type specified by its arguments. |
| 75 | +
|
| 76 | + Options for calling this function are: |
| 77 | + 1 Stephen(presentation: PresentationWords) |
| 78 | + 2 Stephen(presentation: InversePresentationWords) |
| 79 | + 3 Stephen(presentation: StephenPresentationWords) |
| 80 | + 4 Stephen(presentation: StephenInversePresentationWords) |
| 81 | +
|
| 82 | + In cases 1 and 2 a new Stephen object is constructed with the given |
| 83 | + presentation. In cases 3 and 4 the Stephen object is constructed by copying |
| 84 | + an existing Stephen object. In cases 1 and 3 a StephenPresentationWords |
| 85 | + object is returned. In cases 2 and 4 a StephenInversePresentationWords |
| 86 | + object is returned. |
| 87 | + """ |
| 88 | + super().__init__(*args, **kwargs) |
| 89 | + if _to_cxx(self) is not None: |
| 90 | + return |
| 91 | + |
| 92 | + if len(args) != 1: |
| 93 | + raise ValueError(f"expected 1 argument, but got {len(args)}") |
| 94 | + |
| 95 | + if isinstance(args[0], (_Presentation, _InversePresentation)): |
| 96 | + self.py_template_params = (type(_to_cxx(args[0])),) |
| 97 | + else: |
| 98 | + raise TypeError( |
| 99 | + "expected the 1st argument to have type 'Presentation', " |
| 100 | + f" or 'InversePresentation', but found {type(args[0])}", |
| 101 | + ) |
| 102 | + self.init_cxx_obj(*args) |
| 103 | + |
| 104 | + def __imul__(self: Self, other: Self) -> Self: |
| 105 | + x = _to_cxx(self) |
| 106 | + x *= _to_cxx(other) |
| 107 | + return self |
| 108 | + |
| 109 | + |
| 110 | +######################################################################## |
| 111 | +# Copy mem fns from sample C++ type and register types |
| 112 | +######################################################################## |
| 113 | + |
| 114 | +_copy_cxx_mem_fns(_StephenPresentationWords, Stephen) |
| 115 | +_register_cxx_wrapped_type(_StephenPresentationWords, Stephen) |
| 116 | +_register_cxx_wrapped_type(_StephenInversePresentationWords, Stephen) |
| 117 | + |
| 118 | +######################################################################## |
| 119 | +# Helpers |
| 120 | +######################################################################## |
| 121 | + |
| 122 | +accepts = _wrap_cxx_free_fn(_accepts) |
| 123 | +dot = _wrap_cxx_free_fn(_dot) |
| 124 | +is_left_factor = _wrap_cxx_free_fn(_is_left_factor) |
| 125 | +left_factors = _wrap_cxx_free_fn(_left_factors) |
| 126 | +number_of_left_factors = _wrap_cxx_free_fn(_number_of_left_factors) |
| 127 | +number_of_words_accepted = _wrap_cxx_free_fn(_number_of_words_accepted) |
| 128 | +words_accepted = _wrap_cxx_free_fn(_words_accepted) |
0 commit comments