Skip to content

Commit a8d51c1

Browse files
Joseph-Edwardsjames-d-mitchell
authored andcommitted
words: parse_relations -> parse
1 parent 004af1e commit a8d51c1

5 files changed

Lines changed: 80 additions & 28 deletions

File tree

docs/source/data-structures/words/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This pages contains links to documentation in ``libsemigroups_pybind11`` for:
3131

3232
- parsing algebraic expressions in a string;
3333

34-
- :any:`parse_relations`
34+
- :any:`parse`
3535

3636
.. toctree::
3737
:maxdepth: 1

docs/source/data-structures/words/word-funcs.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Contents
3939

4040
human_readable_index
4141
human_readable_letter
42+
parse
4243
parse_relations
4344
pow
4445
prod

src/libsemigroups_pybind11/words.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
from _libsemigroups_pybind11 import ( # pylint: disable=redefined-builtin
1313
words_human_readable_index as human_readable_index,
1414
words_human_readable_letter as human_readable_letter,
15+
words_parse as parse,
1516
words_parse_relations as parse_relations,
1617
words_pow as pow,
1718
words_prod as prod,
1819
)
1920

20-
__all__ = ["human_readable_index", "human_readable_letter", "parse_relations", "pow", "prod"]
21+
__all__ = [
22+
"human_readable_index",
23+
"human_readable_letter",
24+
"parse",
25+
"parse_relations",
26+
"pow",
27+
"prod",
28+
]

src/word-range.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
// pybind11....
3535
#include <pybind11/complex.h>
36+
#include <pybind11/eval.h> // for pybind11::exec
3637
#include <pybind11/pybind11.h> // for make_iterator, module
3738
#include <pybind11/stl.h>
3839

@@ -1354,7 +1355,55 @@ in the range ``[min, max)`` over the specified alphabet.
13541355
// Helper functions for words in words namespace
13551356
////////////////////////////////////////////////////////////////////////////
13561357

1357-
m.def("words_parse_relations",
1358+
m.def(
1359+
"words_parse_relations",
1360+
[](char const* w) {
1361+
py::exec(R"(
1362+
from warnings import warn
1363+
1364+
warn(
1365+
"words.parse_relations is deprecated, and will be removed from libsemigroups_pybind11 in v2. Instead, use words.parse",
1366+
DeprecationWarning,
1367+
2
1368+
)
1369+
)");
1370+
return literals::operator""_p(w);
1371+
},
1372+
py::arg("w"),
1373+
R"pbdoc(
1374+
:sig=(w: str) -> str:
1375+
Construct string by parsing an algebraic expression.
1376+
1377+
This function provides a means of constructing a :any:`str` from an algebraic
1378+
expression, and has the following behaviour:
1379+
1380+
* arbitrarily nested brackets;
1381+
* spaces are ignored;
1382+
* redundant matched brackets are ignored;
1383+
* only the characters in ``"()^ "`` and in ``a-zA-Z0-9`` are allowed.
1384+
1385+
:param w: the expression to parse.
1386+
:type w: str
1387+
1388+
:returns: The parsed expression.
1389+
:rtype: str
1390+
1391+
:raises LibsemigroupsError: if *w* cannot be parsed.
1392+
1393+
.. doctest::
1394+
1395+
>>> from libsemigroups_pybind11.words import parse_relations
1396+
>>> parse_relations("((ab)^3cc)^2")
1397+
'abababccabababcc'
1398+
>>> parse_relations("a^0")
1399+
''
1400+
1401+
.. deprecated:: 1.5
1402+
This will be removed from ``libsemigroups_pybind11`` in v2. Instead, use
1403+
:any:`words.parse`.
1404+
)pbdoc");
1405+
1406+
m.def("words_parse",
13581407
py::overload_cast<char const*>(&literals::operator""_p),
13591408
py::arg("w"),
13601409
R"pbdoc(
@@ -1363,6 +1412,7 @@ Construct string by parsing an algebraic expression.
13631412
13641413
This function provides a means of constructing a :any:`str` from an algebraic
13651414
expression, and has the following behaviour:
1415+
13661416
* arbitrarily nested brackets;
13671417
* spaces are ignored;
13681418
* redundant matched brackets are ignored;

tests/test_word_range.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
random_strings,
2323
random_word,
2424
)
25-
from libsemigroups_pybind11.words import (
26-
human_readable_index,
27-
human_readable_letter,
28-
parse_relations,
29-
)
25+
from libsemigroups_pybind11.words import human_readable_index, human_readable_letter, parse
3026

3127

3228
def test_number_of_words():
@@ -231,31 +227,28 @@ def test_StringRange_000():
231227
]
232228

233229

234-
def test_parse_relations():
235-
assert parse_relations("cd(ab)^2ef") == "cdababef"
236-
assert parse_relations("cd((ab)^2)^4ef") == "cdababababababababef"
237-
assert parse_relations("cd((ab)^2)^4(ef)^2") == "cdababababababababefef"
238-
assert parse_relations("a^16") == "aaaaaaaaaaaaaaaa"
239-
assert (
240-
parse_relations("a^16cd^10((ab)^2)^4(ef)^2")
241-
== "aaaaaaaaaaaaaaaacddddddddddababababababababefef"
242-
)
243-
assert parse_relations("X^3(yx^2)") == "XXXyxx"
244-
assert parse_relations("b(aX)^3x") == "baXaXaXx"
245-
assert parse_relations("((a)b^2y)^10") == "abbyabbyabbyabbyabbyabbyabbyabbyabbyabby"
230+
def test_parse():
231+
assert parse("cd(ab)^2ef") == "cdababef"
232+
assert parse("cd((ab)^2)^4ef") == "cdababababababababef"
233+
assert parse("cd((ab)^2)^4(ef)^2") == "cdababababababababefef"
234+
assert parse("a^16") == "aaaaaaaaaaaaaaaa"
235+
assert parse("a^16cd^10((ab)^2)^4(ef)^2") == "aaaaaaaaaaaaaaaacddddddddddababababababababefef"
236+
assert parse("X^3(yx^2)") == "XXXyxx"
237+
assert parse("b(aX)^3x") == "baXaXaXx"
238+
assert parse("((a)b^2y)^10") == "abbyabbyabbyabbyabbyabbyabbyabbyabbyabby"
246239

247-
assert parse_relations("()") == ""
248-
assert parse_relations("y^0") == ""
249-
assert parse_relations("") == ""
250-
assert parse_relations("a") == "a"
240+
assert parse("()") == ""
241+
assert parse("y^0") == ""
242+
assert parse("") == ""
243+
assert parse("a") == "a"
251244

252-
assert parse_relations(" ") == ""
245+
assert parse(" ") == ""
253246

254247

255248
def test_ToWord_1():
256249
to_word = ToWord()
257-
assert to_word(parse_relations("cd(ab)^2ef")) == [2, 3, 0, 1, 0, 1, 4, 5]
258-
assert to_word(parse_relations("cd((ab)^2)^4ef")) == [
250+
assert to_word(parse("cd(ab)^2ef")) == [2, 3, 0, 1, 0, 1, 4, 5]
251+
assert to_word(parse("cd((ab)^2)^4ef")) == [
259252
2,
260253
3,
261254
0,
@@ -277,7 +270,7 @@ def test_ToWord_1():
277270
4,
278271
5,
279272
]
280-
assert to_word(parse_relations("cd((ab)^2)^4(ef)^2")) == [
273+
assert to_word(parse("cd((ab)^2)^4(ef)^2")) == [
281274
2,
282275
3,
283276
0,

0 commit comments

Comments
 (0)