Skip to content

Commit 20a8a3e

Browse files
committed
remove fastcore import, update pyright config
1 parent 98b9b85 commit 20a8a3e

File tree

7 files changed

+23
-49
lines changed

7 files changed

+23
-49
lines changed

pyformlang/finite_automaton/deterministic_finite_automaton.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ def remove_start_state(self, state: Hashable) -> int:
138138
return 1
139139
return 0
140140

141+
def get_next_state(self, s_from: Hashable, symb_by: Hashable) \
142+
-> Optional[State]:
143+
""" Make a call of deterministic transition function """
144+
s_from = to_state(s_from)
145+
symb_by = to_symbol(symb_by)
146+
return self._transition_function.get_next_state(s_from, symb_by)
147+
141148
def accepts(self, word: Iterable[Hashable]) -> bool:
142149
""" Checks whether the dfa accepts a given word
143150
@@ -167,8 +174,7 @@ def accepts(self, word: Iterable[Hashable]) -> bool:
167174
for symbol in word:
168175
if current_state is None:
169176
return False
170-
current_state = self._transition_function.get_next_state(
171-
current_state, symbol)
177+
current_state = self.get_next_state(current_state, symbol)
172178
return current_state is not None and self.is_final_state(current_state)
173179

174180
def is_deterministic(self) -> bool:
@@ -212,19 +218,12 @@ def copy(self) -> "DeterministicFiniteAutomaton":
212218
"""
213219
return self._copy_to(DeterministicFiniteAutomaton())
214220

215-
def get_next_state(self, s_from: Hashable, symb_by: Hashable) \
216-
-> Optional[State]:
217-
""" Make a call of deterministic transition function """
218-
s_from = to_state(s_from)
219-
symb_by = to_symbol(symb_by)
220-
return self._transition_function.get_next_state(s_from, symb_by)
221-
222221
def _get_previous_transitions(self) -> PreviousTransitions:
223222
previous_transitions = PreviousTransitions(self._states,
224223
self._input_symbols)
225224
for state in self._states:
226225
for symbol in self._input_symbols:
227-
next0 = self._transition_function.get_next_state(state, symbol)
226+
next0 = self.get_next_state(state, symbol)
228227
previous_transitions.add(next0, symbol, state)
229228
return previous_transitions
230229

@@ -275,8 +274,7 @@ def minimize(self) -> "DeterministicFiniteAutomaton":
275274
done = set()
276275
new_state = to_new_states[state]
277276
for symbol in self._input_symbols:
278-
next_node = self._transition_function.get_next_state(
279-
state, symbol)
277+
next_node = self.get_next_state(state, symbol)
280278
if next_node and next_node in states:
281279
next_node = to_new_states[next_node]
282280
if (next_node, symbol) not in done:
@@ -430,10 +428,8 @@ def _is_equivalent_to_minimal(
430428
matches = {self_minimal.start_state: other_minimal.start_state}
431429
while to_process:
432430
current_self, current_other = to_process.pop()
433-
if (self_minimal.is_final_state(current_self)
434-
and not other_minimal.is_final_state(current_other)) or \
435-
(not self_minimal.is_final_state(current_self)
436-
and other_minimal.is_final_state(current_other)):
431+
if self_minimal.is_final_state(current_self) != \
432+
other_minimal.is_final_state(current_other):
437433
return False
438434
next_self = list(self_minimal.get_transitions_from(current_self))
439435
next_other = list(other_minimal.get_transitions_from(current_other))

pyformlang/finite_automaton/finite_automaton.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from collections import deque
99
from networkx import MultiDiGraph
1010
from networkx.drawing.nx_pydot import write_dot
11-
from fastcore.dispatch import typedispatch
1211

1312
from pyformlang.fst import FST
1413

@@ -322,16 +321,6 @@ def remove_final_state(self, state: Hashable) -> int:
322321
return 1
323322
return 0
324323

325-
@typedispatch
326-
def __call__(self, s_from: Hashable) -> Iterable[Tuple[Symbol, Set[State]]]:
327-
"""
328-
Gives FA transitions from given state.
329-
Calls the transition function
330-
"""
331-
s_from = to_state(s_from)
332-
return self._transition_function(s_from)
333-
334-
@typedispatch
335324
def __call__(self, s_from: Hashable, symb_by: Hashable) -> Set[State]:
336325
""" Gives the states obtained after calling a symbol on a state
337326
Calls the transition function

pyformlang/finite_automaton/nondeterministic_transition_function.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from typing import Dict, Set, Iterable, Tuple
88
from copy import deepcopy
9-
from fastcore.dispatch import typedispatch
109

1110
from .state import State
1211
from .symbol import Symbol
@@ -129,13 +128,6 @@ def get_number_transitions(self) -> int:
129128
counter += len(s_to)
130129
return counter
131130

132-
@typedispatch
133-
def __call__(self, s_from: State) -> Iterable[Tuple[Symbol, Set[State]]]:
134-
""" Calls the transition function as a real function """
135-
if s_from in self._transitions:
136-
yield from self._transitions[s_from].items()
137-
138-
@typedispatch
139131
def __call__(self, s_from: State, symb_by: Symbol) -> Set[State]:
140132
""" Calls the transition function as a real function
141133

pyformlang/finite_automaton/tests/test_epsilon_nfa.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,18 @@ def test_len(self):
484484
assert len(enfa) == 1
485485

486486
def test_call(self):
487+
""" Tests the call of the transition function of the ENFA """
487488
enfa = get_enfa_example1()
488-
assert len(list(enfa(2))) == 1
489+
assert enfa(2, "c") == {3}
490+
assert not enfa(3, "a")
491+
assert not enfa(2313, "qwe")
492+
493+
def test_get_transitions_from(self):
494+
""" Tests the transition obtaining from the given state """
495+
enfa = get_enfa_example1()
496+
assert list(enfa.get_transitions_from(2)) == [("c", 3)]
497+
assert not list(enfa.get_transitions_from(3))
498+
assert not list(enfa.get_transitions_from(4210))
489499

490500
def test_remove_epsilon_transitions(self):
491501
enfa = EpsilonNFA()

pyformlang/finite_automaton/transition_function.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from typing import Dict, Set, Tuple, Iterable, Iterator
88
from abc import abstractmethod
9-
from fastcore.dispatch import typedispatch
109

1110
from .state import State
1211
from .symbol import Symbol
@@ -39,16 +38,6 @@ def get_number_transitions(self) -> int:
3938
def __len__(self) -> int:
4039
return self.get_number_transitions()
4140

42-
@typedispatch
43-
@abstractmethod
44-
def __call__(self, s_from: State) -> Iterable[Tuple[Symbol, Set[State]]]:
45-
"""
46-
Calls the transition function
47-
as a real function for given state.
48-
"""
49-
raise NotImplementedError
50-
51-
@typedispatch
5241
@abstractmethod
5342
def __call__(self, s_from: State, symb_by: Symbol) -> Set[State]:
5443
"""

pyrightconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@
1919
"strictParameterNoneValue": false,
2020

2121
"reportMissingParameterType": "warning",
22-
"reportRedeclaration": "none",
2322
}

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ numpy
1111
pylint
1212
pycodestyle
1313
pydot
14-
fastcore
1514
pygments>=2.7.4 # not directly required, pinned by Snyk to avoid a vulnerability
1615
pylint>=2.7.0 # not directly required, pinned by Snyk to avoid a vulnerability
1716
sphinx>=3.0.4 # not directly required, pinned by Snyk to avoid a vulnerability

0 commit comments

Comments
 (0)