Skip to content

Commit 797876d

Browse files
author
Release Manager
committed
gh-40992: some details in words/morphic.py just pep8 cleanup, some typing annotations, and some code details ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40992 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 8dc18b1 + fb033ba commit 797876d

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/sage/combinat/words/morphic.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
sage: w[10000000] # needs sage.modules
2626
'b'
2727
"""
28+
from typing import Iterator
29+
from itertools import chain
2830

2931
from sage.combinat.words.word_infinite_datatypes import WordDatatype_callable
3032
from sage.misc.lazy_import import lazy_import
@@ -38,7 +40,8 @@ class WordDatatype_morphic(WordDatatype_callable):
3840
Datatype for a morphic word defined by a morphism, a starting letter
3941
and a coding.
4042
"""
41-
def __init__(self, parent, morphism, letter, coding=None, length=Infinity):
43+
def __init__(self, parent, morphism, letter,
44+
coding=None, length=Infinity) -> None:
4245
r"""
4346
INPUT:
4447
@@ -127,7 +130,7 @@ def __init__(self, parent, morphism, letter, coding=None, length=Infinity):
127130
else:
128131
self._coding = coding
129132

130-
def __reduce__(self):
133+
def __reduce__(self) -> tuple:
131134
r"""
132135
EXAMPLES::
133136
@@ -156,7 +159,7 @@ def __reduce__(self):
156159
return self.__class__, (self._parent, self._morphism, self._letter,
157160
self._coding, self._len)
158161

159-
def representation(self, n):
162+
def representation(self, n) -> list:
160163
r"""
161164
Return the representation of the integer n in the numeration system
162165
associated to the morphism.
@@ -201,16 +204,16 @@ def representation(self, n):
201204
sage: w.representation(5) # needs sage.modules
202205
[1, 0, 0, 0]
203206
"""
204-
letters_to_int = {a:i for (i,a) in enumerate(self._alphabet)}
207+
letters_to_int = {a: i for i, a in enumerate(self._alphabet)}
205208
position = letters_to_int[self._letter]
206209
M = self._morphism.incidence_matrix()
207-
vMk = vector([1]*len(self._alphabet))
210+
vMk = vector([1] * len(self._alphabet))
208211
length_of_images = []
209212
while vMk[position] <= n:
210213
length_of_images.append(vMk)
211-
vMk_next = vMk*M
214+
vMk_next = vMk * M
212215
if vMk[position] == vMk_next[position]:
213-
raise IndexError('index (={}) out of range, the fixed point is finite and has length {}'.format(n,vMk[position]))
216+
raise IndexError(f'index (={n}) out of range, the fixed point is finite and has length {vMk[position]}')
214217
vMk = vMk_next
215218
k = len(length_of_images)
216219
letter_k = self._letter
@@ -223,10 +226,10 @@ def representation(self, n):
223226
while S <= n_k:
224227
a = m_letter_k[j]
225228
i = letters_to_int[a]
226-
pile_length = length_of_images[k-1][i]
229+
pile_length = length_of_images[k - 1][i]
227230
S += pile_length
228231
j += 1
229-
path.append(j-1)
232+
path.append(j - 1)
230233
n_k -= S - pile_length
231234
letter_k = a
232235
k -= 1
@@ -269,11 +272,9 @@ def _func(self, key):
269272
letter = self._letter
270273
for a in self.representation(key):
271274
letter = (self._morphism(letter))[a]
272-
if key == 0:
273-
return self._coding[letter]
274275
return self._coding[letter]
275276

276-
def __iter__(self):
277+
def __iter__(self) -> Iterator:
277278
r"""
278279
Return an iterator of the letters of the fixed point of ``self``
279280
starting with ``letter``.
@@ -338,7 +339,6 @@ def __iter__(self):
338339
sage: (s^7).reversal().fixed_points()
339340
[]
340341
"""
341-
from itertools import chain
342342
w = iter(self._morphism.image(self._letter))
343343
while True:
344344
try:

0 commit comments

Comments
 (0)