Skip to content

Commit

Permalink
gh-38585: adding a warning about iteration over words
Browse files Browse the repository at this point in the history
    
This warns that one cannot currently iterate over finite words with
infinite alphabets.

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.
    
URL: #38585
Reported by: Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Sep 13, 2024
2 parents 0578894 + cb6b869 commit 189a4a1
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/sage/combinat/words/words.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def _sortkey_letters(self, letter1):
rk = self.alphabet().rank
return rk(letter1)

def __eq__(self, other):
def __eq__(self, other) -> bool:
r"""
TESTS::
Expand All @@ -309,7 +309,7 @@ def __eq__(self, other):
return self is other or (type(self) is type(other) and
self.alphabet() == other.alphabet())

def __ne__(self, other):
def __ne__(self, other) -> bool:
r"""
TESTS::
Expand Down Expand Up @@ -479,16 +479,17 @@ def _word_from_word(self, data):
return self._element_classes['list'](self, data)

from sage.combinat.words.word_datatypes import (WordDatatype_str,
WordDatatype_list, WordDatatype_tuple)
WordDatatype_list,
WordDatatype_tuple)
if isinstance(data, WordDatatype_str):
return self._element_classes['str'](self, data._data)
if isinstance(data, WordDatatype_tuple):
return self._element_classes['tuple'](self, data._data)
if isinstance(data, WordDatatype_list):
return self._element_classes['list'](self, data._data)

from sage.combinat.words.word_infinite_datatypes import (WordDatatype_callable,
WordDatatype_iter)
from sage.combinat.words.word_infinite_datatypes import \
(WordDatatype_callable, WordDatatype_iter)
if isinstance(data, WordDatatype_callable):
length = data.length()
data = data._func
Expand Down Expand Up @@ -849,7 +850,7 @@ def __call__(self, data=None, length=None, datatype=None, caching=True, check=Tr
self._check(w)
return w

def _repr_(self):
def _repr_(self) -> str:
"""
EXAMPLES::
Expand All @@ -875,14 +876,14 @@ def _an_element_(self):
"""
try:
some_letters = list(self.alphabet().some_elements())
except Exception:
except (TypeError, ValueError, AttributeError, NotImplementedError):
return self([])

if len(some_letters) == 1:
return self([some_letters[0]] * 3)
else:
a, b = some_letters[:2]
return self([b, a, b])

a, b = some_letters[:2]
return self([b, a, b])

def iterate_by_length(self, l=1):
r"""
Expand Down Expand Up @@ -912,10 +913,20 @@ def iterate_by_length(self, l=1):
Traceback (most recent call last):
...
TypeError: the parameter l (='a') must be an integer
TESTS::
sage: W = FiniteWords(NN)
sage: list(W.iterate_by_length(1))
Traceback (most recent call last):
...
NotImplementedError: cannot iterate over words for infinite alphabets
"""
if not isinstance(l, (int, Integer)):
raise TypeError("the parameter l (=%r) must be an integer" % l)
cls = self._element_classes['tuple']
if not self.alphabet().is_finite():
raise NotImplementedError("cannot iterate over words for infinite alphabets")
for w in itertools.product(self.alphabet(), repeat=l):
yield cls(self, w)

Expand Down Expand Up @@ -961,7 +972,7 @@ def __iter__(self):
for l in itertools.count():
yield from self.iterate_by_length(l)

def __contains__(self, x):
def __contains__(self, x) -> bool:
"""
Test whether ``self`` contains ``x``.
Expand Down

0 comments on commit 189a4a1

Please sign in to comment.