Skip to content

Commit

Permalink
update indexed grammars tests and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bygu4 committed Dec 29, 2024
1 parent 32963bd commit 1dddebb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
8 changes: 4 additions & 4 deletions pyformlang/indexed_grammar/indexed_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .production_rule import ProductionRule
from .consumption_rule import ConsumptionRule
from .end_rule import EndRule
from .utils import exists, addrec_bis
from .utils import addrec_bis
from ..objects.cfg_objects.utils import to_variable


Expand Down Expand Up @@ -47,9 +47,9 @@ def __init__(self,
self._marked[non_terminal_a].add(temp)
# Mark all end symbols
for non_terminal_a in non_terminals:
if exists(self._rules.rules,
lambda x: isinstance(x, EndRule)
and x.left_term == non_terminal_a):
if any(map(lambda x: isinstance(x, EndRule)
and x.left_term == non_terminal_a,
self._rules.rules)):
self._marked[non_terminal_a].add(frozenset())

@property
Expand Down
19 changes: 9 additions & 10 deletions pyformlang/indexed_grammar/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ class TestIndexedGrammar:

def test_consumption_rules(self):
""" Tests the consumption rules """
conso = ConsumptionRule("end", "C", "T")
terminals = conso.terminals
consumption = ConsumptionRule("end", "C", "T")
terminals = consumption.terminals
assert terminals == {"end"}
representation = str(conso)
representation = str(consumption)
assert representation == "C [ end ] -> T"

def test_duplication_rules(self):
""" Tests the duplication rules """
dupli = DuplicationRule("B0", "A0", "C")
assert dupli.terminals == set()
assert str(dupli) == \
"B0 -> A0 C"
duplication = DuplicationRule("B0", "A0", "C")
assert duplication.terminals == set()
assert str(duplication) == "B0 -> A0 C"

def test_end_rule(self):
""" Tests the end rules """
Expand All @@ -39,9 +38,9 @@ def test_end_rule(self):

def test_production_rules(self):
""" Tests the production rules """
produ = ProductionRule("S", "C", "end")
assert produ.terminals == {"end"}
assert str(produ) == "S -> C [ end ]"
production = ProductionRule("S", "C", "end")
assert production.terminals == {"end"}
assert str(production) == "S -> C [ end ]"

def test_rules(self):
""" Tests the rules """
Expand Down
18 changes: 2 additions & 16 deletions pyformlang/indexed_grammar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@

# pylint: disable=cell-var-from-loop

from typing import Callable, List, Set, Iterable, Any


def exists(list_elements: List[Any],
check_function: Callable[[Any], bool]) -> bool:
"""exists
Check whether at least an element x of l is True for f(x)
:param list_elements: A list of elements to test
:param check_function: The checking function (takes one parameter and \
return a boolean)
"""
for element in list_elements:
if check_function(element):
return True
return False
from typing import List, Set, Iterable, Any


def addrec_bis(l_sets: Iterable[Any],
Expand Down Expand Up @@ -58,7 +44,7 @@ def addrec_ter(l_sets: List[Any], marked_left: Set[Any]) -> bool:
# End condition, nothing left to process
temp_in = [x[0] for x in l_sets]
exists_after = [
exists(l_sets[index + 1:], lambda x: x[0] == l_sets[index][0])
any(map(lambda x: x[0] == l_sets[index][0], l_sets[index + 1:]))
for index in range(len(l_sets))]
exists_before = [l_sets[index][0] in temp_in[:index]
for index in range(len(l_sets))]
Expand Down

1 comment on commit 1dddebb

@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.py6544 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.py410100% 
pyformlang/indexed_grammar/tests
   __init__.py00100% 
   test_indexed_grammar.py2490100% 
   test_rules.py350100% 
pyformlang/objects
   __init__.py50100% 
   base_epsilon.py1511 93%
   base_terminal.py70100% 
   formal_object.py240100% 
pyformlang/objects/cfg_objects
   __init__.py60100% 
   cfg_object.py50100% 
   epsilon.py30100% 
   production.py4111 98%
   terminal.py100100% 
   utils.py140100% 
   variable.py130100% 
pyformlang/objects/finite_automaton_objects
   __init__.py50100% 
   epsilon.py30100% 
   finite_automaton_object.py50100% 
   state.py70100% 
   symbol.py50100% 
   utils.py140100% 
pyformlang/objects/pda_objects
   __init__.py60100% 
   epsilon.py30100% 
   pda_object.py50100% 
   stack_symbol.py70100% 
   state.py70100% 
   symbol.py50100% 
   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.py3020100% 
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% 
TOTAL874110999% 

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

Please sign in to comment.