Skip to content

Commit

Permalink
remove fastcore import, update pyright config
Browse files Browse the repository at this point in the history
  • Loading branch information
bygu4 committed Dec 9, 2024
1 parent 05835f4 commit 548fc06
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 49 deletions.
28 changes: 12 additions & 16 deletions pyformlang/finite_automaton/deterministic_finite_automaton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down
11 changes: 0 additions & 11 deletions pyformlang/finite_automaton/finite_automaton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion pyformlang/finite_automaton/tests/test_epsilon_nfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 0 additions & 11 deletions pyformlang/finite_automaton/transition_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]:
"""
Expand Down
1 change: 0 additions & 1 deletion pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
"strictParameterNoneValue": false,

"reportMissingParameterType": "warning",
"reportRedeclaration": "none",
}
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 comment on commit 548fc06

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
pyformlang
   __init__.py90100% 
pyformlang/cfg
   __init__.py30100% 
   cfg.py46622 99%
   cfg_variable_converter.py6444 94%
   cyk_table.py790100% 
   formal_grammar.py6911 99%
   llone_parser.py16333 98%
   parse_tree.py6511 98%
   recursive_decent_parser.py6122 97%
   set_queue.py150100% 
   utils.py250100% 
pyformlang/cfg/tests
   __init__.py00100% 
   test_cfg.py62622 99%
   test_llone_parser.py11711 99%
   test_production.py210100% 
   test_recursive_decent_parser.py2511 96%
   test_terminal.py330100% 
   test_variable.py180100% 
pyformlang/fcfg
   __init__.py40100% 
   fcfg.py13111 99%
   feature_production.py250100% 
   feature_structure.py19133 98%
   state.py360100% 
pyformlang/fcfg/tests
   __init__.py00100% 
   test_fcfg.py1690100% 
   test_feature_structure.py1590100% 
pyformlang/finite_automaton
   __init__.py70100% 
   deterministic_finite_automaton.py18333 98%
   deterministic_transition_function.py2411 96%
   doubly_linked_list.py350100% 
   doubly_linked_node.py100100% 
   epsilon_nfa.py21011 99%
   finite_automaton.py23111 99%
   hopcroft_processing_list.py240100% 
   nondeterministic_finite_automaton.py400100% 
   nondeterministic_transition_function.py480100% 
   partition.py400100% 
   transition_function.py320100% 
   utils.py300100% 
pyformlang/finite_automaton/tests
   __init__.py00100% 
   test_deterministic_finite_automaton.py2960100% 
   test_deterministic_transition_function.py8955 94%
   test_epsilon.py130100% 
   test_epsilon_nfa.py5950100% 
   test_nondeterministic_finite_automaton.py1600100% 
   test_nondeterministic_transition_function.py790100% 
   test_state.py280100% 
   test_symbol.py290100% 
pyformlang/fst
   __init__.py20100% 
   fst.py18611 99%
   transition_function.py3833 92%
   utils.py250100% 
pyformlang/fst/tests
   __init__.py00100% 
   test_fst.py1910100% 
pyformlang/indexed_grammar
   __init__.py90100% 
   consumption_rule.py3522 94%
   duplication_rule.py320100% 
   end_rule.py3311 97%
   indexed_grammar.py27111 99%
   production_rule.py3511 97%
   reduced_rule.py290100% 
   rule_ordering.py730100% 
   rules.py770100% 
   utils.py460100% 
pyformlang/indexed_grammar/tests
   __init__.py00100% 
   test_indexed_grammar.py2490100% 
   test_rules.py350100% 
pyformlang/objects
   __init__.py50100% 
   base_epsilon.py120100% 
   formal_object.py190100% 
pyformlang/objects/cfg_objects
   __init__.py70100% 
   cfg_object.py50100% 
   cfg_object_convertible.py90100% 
   epsilon.py30100% 
   production.py4111 98%
   terminal.py190100% 
   utils.py140100% 
   variable.py200100% 
pyformlang/objects/finite_automaton_objects
   __init__.py50100% 
   epsilon.py30100% 
   finite_automaton_object.py50100% 
   state.py150100% 
   symbol.py1411 93%
   utils.py140100% 
pyformlang/objects/pda_objects
   __init__.py60100% 
   epsilon.py30100% 
   pda_object.py50100% 
   stack_symbol.py150100% 
   state.py150100% 
   symbol.py140100% 
   utils.py2111 95%
pyformlang/objects/regex_objects
   __init__.py20100% 
   regex_objects.py630100% 
   utils.py220100% 
pyformlang/pda
   __init__.py40100% 
   pda.py31722 99%
   transition_function.py3933 92%
   utils.py5322 96%
pyformlang/pda/tests
   __init__.py00100% 
   test_pda.py3000100% 
pyformlang/regular_expression
   __init__.py30100% 
   python_regex.py26966 98%
   regex.py2811414 95%
   regex_reader.py15944 97%
pyformlang/regular_expression/tests
   __init__.py00100% 
   test_python_regex.py2780100% 
   test_regex.py4110100% 
pyformlang/rsa
   __init__.py30100% 
   box.py512525 51%
   recursive_automaton.py7299 88%
pyformlang/rsa/tests
   __init__.py00100% 
   test_rsa.py370100% 
pyformlang/tests
   __init__.py00100% 
TOTAL879610999% 

Tests Skipped Failures Errors Time
310 0 💤 0 ❌ 0 🔥 9.241s ⏱️

Please sign in to comment.