From 548fc06635c64ad6bfbf389459a6794e6202ca97 Mon Sep 17 00:00:00 2001 From: bygu4 Date: Mon, 9 Dec 2024 13:06:43 +0300 Subject: [PATCH] remove fastcore import, update pyright config --- .../deterministic_finite_automaton.py | 28 ++++++++----------- .../finite_automaton/finite_automaton.py | 11 -------- .../nondeterministic_transition_function.py | 8 ------ .../tests/test_epsilon_nfa.py | 12 +++++++- .../finite_automaton/transition_function.py | 11 -------- pyrightconfig.json | 1 - requirements.txt | 1 - 7 files changed, 23 insertions(+), 49 deletions(-) diff --git a/pyformlang/finite_automaton/deterministic_finite_automaton.py b/pyformlang/finite_automaton/deterministic_finite_automaton.py index 18a25c8..d17103b 100644 --- a/pyformlang/finite_automaton/deterministic_finite_automaton.py +++ b/pyformlang/finite_automaton/deterministic_finite_automaton.py @@ -139,6 +139,13 @@ def remove_start_state(self, state: Hashable) -> int: return 1 return 0 + def get_next_state(self, s_from: Hashable, symb_by: Hashable) \ + -> Optional[State]: + """ Make a call of deterministic transition function """ + s_from = to_state(s_from) + symb_by = to_symbol(symb_by) + return self._transition_function.get_next_state(s_from, symb_by) + def accepts(self, word: Iterable[Hashable]) -> bool: """ Checks whether the dfa accepts a given word @@ -168,8 +175,7 @@ def accepts(self, word: Iterable[Hashable]) -> bool: for symbol in word: if current_state is None: return False - current_state = self._transition_function.get_next_state( - current_state, symbol) + current_state = self.get_next_state(current_state, symbol) return current_state is not None and self.is_final_state(current_state) def is_deterministic(self) -> bool: @@ -213,19 +219,12 @@ def copy(self) -> "DeterministicFiniteAutomaton": """ return self._copy_to(DeterministicFiniteAutomaton()) - def get_next_state(self, s_from: Hashable, symb_by: Hashable) \ - -> Optional[State]: - """ Make a call of deterministic transition function """ - s_from = to_state(s_from) - symb_by = to_symbol(symb_by) - return self._transition_function.get_next_state(s_from, symb_by) - def _get_previous_transitions(self) -> PreviousTransitions: previous_transitions = PreviousTransitions(self._states, self._input_symbols) for state in self._states: for symbol in self._input_symbols: - next0 = self._transition_function.get_next_state(state, symbol) + next0 = self.get_next_state(state, symbol) previous_transitions.add(next0, symbol, state) return previous_transitions @@ -276,8 +275,7 @@ def minimize(self) -> "DeterministicFiniteAutomaton": done = set() new_state = to_new_states[state] for symbol in self._input_symbols: - next_node = self._transition_function.get_next_state( - state, symbol) + next_node = self.get_next_state(state, symbol) if next_node and next_node in states: next_node = to_new_states[next_node] if (next_node, symbol) not in done: @@ -431,10 +429,8 @@ def _is_equivalent_to_minimal( matches = {self_minimal.start_state: other_minimal.start_state} while to_process: current_self, current_other = to_process.pop() - if (self_minimal.is_final_state(current_self) - and not other_minimal.is_final_state(current_other)) or \ - (not self_minimal.is_final_state(current_self) - and other_minimal.is_final_state(current_other)): + if self_minimal.is_final_state(current_self) != \ + other_minimal.is_final_state(current_other): return False next_self = list(self_minimal.get_transitions_from(current_self)) next_other = list(other_minimal.get_transitions_from(current_other)) diff --git a/pyformlang/finite_automaton/finite_automaton.py b/pyformlang/finite_automaton/finite_automaton.py index 9cb8e86..e15b5e5 100644 --- a/pyformlang/finite_automaton/finite_automaton.py +++ b/pyformlang/finite_automaton/finite_automaton.py @@ -8,7 +8,6 @@ from collections import deque from networkx import MultiDiGraph from networkx.drawing.nx_pydot import write_dot -from fastcore.dispatch import typedispatch from pyformlang.fst import FST @@ -321,16 +320,6 @@ def remove_final_state(self, state: Hashable) -> int: return 1 return 0 - @typedispatch - def __call__(self, s_from: Hashable) -> Iterable[Tuple[Symbol, Set[State]]]: - """ - Gives FA transitions from given state. - Calls the transition function - """ - s_from = to_state(s_from) - return self._transition_function(s_from) - - @typedispatch def __call__(self, s_from: Hashable, symb_by: Hashable) -> Set[State]: """ Gives the states obtained after calling a symbol on a state Calls the transition function diff --git a/pyformlang/finite_automaton/nondeterministic_transition_function.py b/pyformlang/finite_automaton/nondeterministic_transition_function.py index 603b4ee..7647a1b 100644 --- a/pyformlang/finite_automaton/nondeterministic_transition_function.py +++ b/pyformlang/finite_automaton/nondeterministic_transition_function.py @@ -6,7 +6,6 @@ from typing import Dict, Set, Iterable, Tuple from copy import deepcopy -from fastcore.dispatch import typedispatch from .transition_function import TransitionFunction from ..objects.finite_automaton_objects import State, Symbol @@ -128,13 +127,6 @@ def get_number_transitions(self) -> int: counter += len(s_to) return counter - @typedispatch - def __call__(self, s_from: State) -> Iterable[Tuple[Symbol, Set[State]]]: - """ Calls the transition function as a real function """ - if s_from in self._transitions: - yield from self._transitions[s_from].items() - - @typedispatch def __call__(self, s_from: State, symb_by: Symbol) -> Set[State]: """ Calls the transition function as a real function diff --git a/pyformlang/finite_automaton/tests/test_epsilon_nfa.py b/pyformlang/finite_automaton/tests/test_epsilon_nfa.py index 362b05e..b498b8c 100644 --- a/pyformlang/finite_automaton/tests/test_epsilon_nfa.py +++ b/pyformlang/finite_automaton/tests/test_epsilon_nfa.py @@ -484,8 +484,18 @@ def test_len(self): assert len(enfa) == 1 def test_call(self): + """ Tests the call of the transition function of the ENFA """ enfa = get_enfa_example1() - assert len(list(enfa(2))) == 1 + assert enfa(2, "c") == {3} + assert not enfa(3, "a") + assert not enfa(2313, "qwe") + + def test_get_transitions_from(self): + """ Tests the transition obtaining from the given state """ + enfa = get_enfa_example1() + assert list(enfa.get_transitions_from(2)) == [("c", 3)] + assert not list(enfa.get_transitions_from(3)) + assert not list(enfa.get_transitions_from(4210)) def test_remove_epsilon_transitions(self): enfa = EpsilonNFA() diff --git a/pyformlang/finite_automaton/transition_function.py b/pyformlang/finite_automaton/transition_function.py index db5682e..e9e4b2d 100644 --- a/pyformlang/finite_automaton/transition_function.py +++ b/pyformlang/finite_automaton/transition_function.py @@ -6,7 +6,6 @@ from typing import Dict, Set, Tuple, Iterable, Iterator from abc import abstractmethod -from fastcore.dispatch import typedispatch from ..objects.finite_automaton_objects import State, Symbol @@ -38,16 +37,6 @@ def get_number_transitions(self) -> int: def __len__(self) -> int: return self.get_number_transitions() - @typedispatch - @abstractmethod - def __call__(self, s_from: State) -> Iterable[Tuple[Symbol, Set[State]]]: - """ - Calls the transition function - as a real function for given state. - """ - raise NotImplementedError - - @typedispatch @abstractmethod def __call__(self, s_from: State, symb_by: Symbol) -> Set[State]: """ diff --git a/pyrightconfig.json b/pyrightconfig.json index e281b53..7fd2a49 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -19,5 +19,4 @@ "strictParameterNoneValue": false, "reportMissingParameterType": "warning", - "reportRedeclaration": "none", } diff --git a/requirements.txt b/requirements.txt index 16ca3ff..c65ce0c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,6 @@ pylint pycodestyle pyright pydot -fastcore pygments>=2.7.4 # not directly required, pinned by Snyk to avoid a vulnerability pylint>=2.7.0 # not directly required, pinned by Snyk to avoid a vulnerability sphinx>=3.0.4 # not directly required, pinned by Snyk to avoid a vulnerability