Skip to content

Commit e63bd9d

Browse files
committed
update indexed grammars tests and utils
1 parent a6104d3 commit e63bd9d

File tree

3 files changed

+15
-30
lines changed

3 files changed

+15
-30
lines changed

pyformlang/indexed_grammar/indexed_grammar.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .production_rule import ProductionRule
1616
from .consumption_rule import ConsumptionRule
1717
from .end_rule import EndRule
18-
from .utils import exists, addrec_bis
18+
from .utils import addrec_bis
1919
from ..objects.cfg_objects.utils import to_variable
2020

2121

@@ -47,9 +47,9 @@ def __init__(self,
4747
self._marked[non_terminal_a].add(temp)
4848
# Mark all end symbols
4949
for non_terminal_a in non_terminals:
50-
if exists(self._rules.rules,
51-
lambda x: isinstance(x, EndRule)
52-
and x.left_term == non_terminal_a):
50+
if any(map(lambda x: isinstance(x, EndRule)
51+
and x.left_term == non_terminal_a,
52+
self._rules.rules)):
5353
self._marked[non_terminal_a].add(frozenset())
5454

5555
@property

pyformlang/indexed_grammar/tests/test_rules.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ class TestIndexedGrammar:
1717

1818
def test_consumption_rules(self):
1919
""" Tests the consumption rules """
20-
conso = ConsumptionRule("end", "C", "T")
21-
terminals = conso.terminals
20+
consumption = ConsumptionRule("end", "C", "T")
21+
terminals = consumption.terminals
2222
assert terminals == {"end"}
23-
representation = str(conso)
23+
representation = str(consumption)
2424
assert representation == "C [ end ] -> T"
2525

2626
def test_duplication_rules(self):
2727
""" Tests the duplication rules """
28-
dupli = DuplicationRule("B0", "A0", "C")
29-
assert dupli.terminals == set()
30-
assert str(dupli) == \
31-
"B0 -> A0 C"
28+
duplication = DuplicationRule("B0", "A0", "C")
29+
assert duplication.terminals == set()
30+
assert str(duplication) == "B0 -> A0 C"
3231

3332
def test_end_rule(self):
3433
""" Tests the end rules """
@@ -39,9 +38,9 @@ def test_end_rule(self):
3938

4039
def test_production_rules(self):
4140
""" Tests the production rules """
42-
produ = ProductionRule("S", "C", "end")
43-
assert produ.terminals == {"end"}
44-
assert str(produ) == "S -> C [ end ]"
41+
production = ProductionRule("S", "C", "end")
42+
assert production.terminals == {"end"}
43+
assert str(production) == "S -> C [ end ]"
4544

4645
def test_rules(self):
4746
""" Tests the rules """

pyformlang/indexed_grammar/utils.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,7 @@
22

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

5-
from typing import Callable, List, Set, Iterable, Any
6-
7-
8-
def exists(list_elements: List[Any],
9-
check_function: Callable[[Any], bool]) -> bool:
10-
"""exists
11-
Check whether at least an element x of l is True for f(x)
12-
:param list_elements: A list of elements to test
13-
:param check_function: The checking function (takes one parameter and \
14-
return a boolean)
15-
"""
16-
for element in list_elements:
17-
if check_function(element):
18-
return True
19-
return False
5+
from typing import List, Set, Iterable, Any
206

217

228
def addrec_bis(l_sets: Iterable[Any],
@@ -58,7 +44,7 @@ def addrec_ter(l_sets: List[Any], marked_left: Set[Any]) -> bool:
5844
# End condition, nothing left to process
5945
temp_in = [x[0] for x in l_sets]
6046
exists_after = [
61-
exists(l_sets[index + 1:], lambda x: x[0] == l_sets[index][0])
47+
any(map(lambda x: x[0] == l_sets[index][0], l_sets[index + 1:]))
6248
for index in range(len(l_sets))]
6349
exists_before = [l_sets[index][0] in temp_in[:index]
6450
for index in range(len(l_sets))]

0 commit comments

Comments
 (0)