Skip to content

Commit

Permalink
Trac #27679: py3: finite state machines
Browse files Browse the repository at this point in the history
Fix the remaining Py3 issues in sage.combinat.finite_state_machine

URL: https://trac.sagemath.org/27679
Reported by: dkrenn
Ticket author(s): Daniel Krenn
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Apr 24, 2019
2 parents 9ef37ae + 3b1d45f commit 7d4bdb9
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions src/sage/combinat/finite_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,10 @@
sage: from sage.combinat.finite_state_machine import FSMState, FSMTransition
sage: C = FiniteStateMachine()
sage: def update_counter(process, state, output):
....: l = process.preview_word()
....: try:
....: l = process.preview_word()
....: except RuntimeError:
....: raise StopIteration
....: process.fsm.counter += 1 if l == 1 else -1
....: if process.fsm.counter > 0:
....: next_state = 'positive'
Expand Down Expand Up @@ -1010,13 +1013,13 @@ def full_group_by(l, key=lambda x: x):
k = key(item)
s = str(k)
if s in original_keys:
if original_keys[s]!=k:
raise ValueError("Two distinct elements with representation "
"%s " % s)
if original_keys[s] != k:
raise ValueError(
'two distinct elements with representation {}'.format(s))
else:
original_keys[s]=k
original_keys[s] = k
elements[s].append(item)
return [(original_keys[s], values ) for (s, values) in elements.items()]
return [(original_keys[s], elements[s]) for s in sorted(elements)]


def equal(iterator):
Expand Down Expand Up @@ -7339,19 +7342,19 @@ def coaccessible_components(self):
sage: A.transitions()
[Transition from 0 to 0: 0|-,
Transition from 0 to 0: 1|-,
Transition from 1 to 1: 0|-,
Transition from 1 to 2: 1|-,
Transition from 2 to 1: 0|-,
Transition from 2 to 0: 1|-]
Transition from 1 to 2: 0|-,
Transition from 1 to 0: 1|-,
Transition from 2 to 2: 0|-,
Transition from 2 to 1: 1|-]
sage: A.initial_states()
[1]
[2]
sage: A.final_states()
[1, 2]
sage: C = A.coaccessible_components()
sage: C.transitions()
[Transition from 1 to 1: 0|-,
Transition from 1 to 2: 1|-,
Transition from 2 to 1: 0|-]
[Transition from 1 to 2: 0|-,
Transition from 2 to 2: 0|-,
Transition from 2 to 1: 1|-]

.. SEEALSO::
:meth:`accessible_components`,
Expand Down Expand Up @@ -10432,12 +10435,12 @@ def asymptotic_moments(self, variable=var('n')):
sage: block_difference = transducers.sub([0, 1])(product_01x10)
sage: T = block_difference.simplification().relabeled()
sage: T.transitions()
[Transition from 0 to 1: 0|-1,
[Transition from 0 to 2: 0|-1,
Transition from 0 to 0: 1|0,
Transition from 1 to 1: 0|0,
Transition from 1 to 0: 1|1,
Transition from 2 to 1: 0|0,
Transition from 2 to 0: 1|0]
Transition from 1 to 2: 0|0,
Transition from 1 to 0: 1|0,
Transition from 2 to 2: 0|0,
Transition from 2 to 0: 1|1]
sage: moments = T.asymptotic_moments()
sage: moments['expectation']
Order(1)
Expand Down Expand Up @@ -12896,19 +12899,15 @@ def simplification(self):
sage: fsms
Transducer with 2 states
sage: fsms.transitions()
[Transition from ('A', 'C')
to ('B', 'D'): 0|1,
Transition from ('A', 'C')
to ('B', 'D'): 1|0,
Transition from ('B', 'D')
to ('A', 'C'): 0|0,
Transition from ('B', 'D')
to ('A', 'C'): 1|1]
[Transition from ('B', 'D') to ('A', 'C'): 0|0,
Transition from ('B', 'D') to ('A', 'C'): 1|1,
Transition from ('A', 'C') to ('B', 'D'): 0|1,
Transition from ('A', 'C') to ('B', 'D'): 1|0]
sage: fsms.relabeled().transitions()
[Transition from 0 to 1: 0|1,
Transition from 0 to 1: 1|0,
Transition from 1 to 0: 0|0,
Transition from 1 to 0: 1|1]
[Transition from 0 to 1: 0|0,
Transition from 0 to 1: 1|1,
Transition from 1 to 0: 0|1,
Transition from 1 to 0: 1|0]

::

Expand Down Expand Up @@ -13592,7 +13591,7 @@ def finished(self, track_number=None):
sage: while True:
....: try:
....: word = TC2.preview_word(return_word=True)
....: except StopIteration:
....: except RuntimeError:
....: print('stop')
....: break
....: print('cache: {} {}'.format(TC2.cache, TC2))
Expand All @@ -13617,7 +13616,7 @@ def finished(self, track_number=None):
sage: TC2.preview_word()
Traceback (most recent call last):
...
StopIteration
RuntimeError: tape reached the end
sage: print('cache: {} {}'.format(TC2.cache, TC2))
cache: (deque([41]), deque([])) multi-tape at (4, 4)
sage: TC2.read(0)
Expand Down Expand Up @@ -13653,8 +13652,8 @@ def preview_word(self, track_number=None, length=1, return_word=False):

A single letter or a word.

An exception ``StopIteration`` is thrown if the tape (at least
one track) has reached its end.
A :python:`RuntimeError<library/exceptions.html#exceptions.RuntimeError>`
is thrown if the tape (at least one track) has reached its end.

Typically, this method is called from a hook-function of a
state.
Expand All @@ -13672,7 +13671,7 @@ def preview_word(self, track_number=None, length=1, return_word=False):
sage: while True:
....: try:
....: word = TC2.preview_word(return_word=True)
....: except StopIteration:
....: except RuntimeError:
....: print('stop')
....: break
....: print('read: {}'.format(word))
Expand All @@ -13698,7 +13697,7 @@ def preview_word(self, track_number=None, length=1, return_word=False):
sage: TC2.preview_word()
Traceback (most recent call last):
...
StopIteration
RuntimeError: tape reached the end
sage: print('cache: {} {}'.format(TC2.cache, TC2))
cache: (deque([41]), deque([])) multi-tape at (4, 4)
sage: TC2.preview_word(0)
Expand All @@ -13717,7 +13716,7 @@ def preview_word(self, track_number=None, length=1, return_word=False):
result = tuple(self.preview_word(n, length, return_word)
for n, _ in enumerate(self.cache))
if len(result) != len(self.cache):
raise StopIteration
raise RuntimeError('tape reached the end')
if return_word:
return tupleofwords_to_wordoftuples(result)
else:
Expand All @@ -13728,7 +13727,7 @@ def preview_word(self, track_number=None, length=1, return_word=False):
track_cache = self.cache[track_number]
while len(track_cache) < length:
if not self.read(track_number)[0]:
raise StopIteration
raise RuntimeError('tape reached the end')
if return_word:
return list(itertools.islice(track_cache, 0, length))
else:
Expand Down Expand Up @@ -15046,8 +15045,11 @@ def preview_word(self, track_number=None, length=1, return_word=False):
....: initial_states=['A'], final_states=['A'])
sage: def state_hook(process, state, output):
....: print("We are now in state %s." % (state.label(),))
....: print("Next on the tape is a %s." % (
....: process.preview_word(),))
....: try:
....: w = process.preview_word()
....: except RuntimeError:
....: raise StopIteration
....: print("Next on the tape is a %s." % (w,))
sage: inverter.state('A').hook = state_hook
sage: it = inverter.iter_process(
....: input_tape=[0, 1, 1],
Expand Down

0 comments on commit 7d4bdb9

Please sign in to comment.