Skip to content

Commit dbb3a22

Browse files
committed
add pda module annotations, rework pda objects
1 parent 32b442c commit dbb3a22

File tree

8 files changed

+175
-136
lines changed

8 files changed

+175
-136
lines changed

pyformlang/pda/epsilon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
""" An epsilon symbol """
22

3-
from .symbol import Symbol
3+
from .stack_symbol import StackSymbol
44

55

6-
class Epsilon(Symbol):
6+
class Epsilon(StackSymbol):
77
""" An epsilon symbol """
88
# pylint: disable=too-few-public-methods
99

10-
def __init__(self):
10+
def __init__(self) -> None:
1111
super().__init__("epsilon")

pyformlang/pda/pda.py

Lines changed: 68 additions & 71 deletions
Large diffs are not rendered by default.

pyformlang/pda/pda_object.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
""" Basic PDA object representation """
2+
3+
from typing import Any
4+
5+
6+
class PDAObject:
7+
""" Basic PDA object representation """
8+
9+
def __init__(self, value: Any) -> None:
10+
self._value = value
11+
self._hash = None
12+
13+
def __hash__(self) -> int:
14+
if self._hash is None:
15+
self._hash = hash(self._value)
16+
return self._hash
17+
18+
@property
19+
def value(self) -> Any:
20+
""" Returns the value of the object """
21+
return self._value
22+
23+
def __eq__(self, other: Any) -> bool:
24+
raise NotImplementedError
25+
26+
def __repr__(self) -> str:
27+
raise NotImplementedError

pyformlang/pda/stack_symbol.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
""" A StackSymbol in a pushdown automaton """
22

3-
from typing import Optional
3+
from typing import Optional, Any
44

5+
from .symbol import Symbol
56

6-
class StackSymbol:
7+
class StackSymbol(Symbol):
78
""" A StackSymbol in a pushdown automaton
89
910
Parameters
@@ -13,13 +14,15 @@ class StackSymbol:
1314
1415
"""
1516

16-
def __init__(self, value):
17-
self._value = value
18-
self._hash = None
17+
def __init__(self, value: Any) -> None:
18+
super().__init__(value)
1919
self.index_cfg_converter: Optional[int] = None
2020

21+
def __hash__(self) -> int:
22+
return super().__hash__()
23+
2124
@property
22-
def value(self):
25+
def value(self) -> Any:
2326
""" Returns the value of the stack symbol
2427
2528
Returns
@@ -29,13 +32,10 @@ def value(self):
2932
"""
3033
return self._value
3134

32-
def __hash__(self):
33-
if self._hash is None:
34-
self._hash = hash(self._value)
35-
return self._hash
36-
37-
def __eq__(self, other):
38-
return self._value == other.value
35+
def __eq__(self, other: Any) -> bool:
36+
if isinstance(other, StackSymbol):
37+
return self._value == other.value
38+
return False
3939

40-
def __repr__(self):
40+
def __repr__(self) -> str:
4141
return "StackSymbol(" + str(self._value) + ")"

pyformlang/pda/state.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
""" A State in a pushdown automaton """
22

3+
from typing import Optional, Any
34

4-
class State:
5+
from .pda_object import PDAObject
6+
7+
8+
class State(PDAObject):
59
""" A State in a pushdown automaton
610
711
Parameters
@@ -11,19 +15,16 @@ class State:
1115
1216
"""
1317

14-
def __init__(self, value):
15-
self._value = value
16-
self._hash = None
17-
self.index_cfg_converter = None
18+
def __init__(self, value: Any) -> None:
19+
super().__init__(value)
20+
self.index_cfg_converter: Optional[int] = None
1821

19-
def __hash__(self):
20-
if self._hash is None:
21-
self._hash = hash(self._value)
22-
return self._hash
22+
def __hash__(self) -> int:
23+
return super().__hash__()
2324

2425
@property
25-
def value(self):
26-
""" Returns the value of the state
26+
def value(self) -> Any:
27+
""" Returns the value of the symbol
2728
2829
Returns
2930
----------
@@ -32,10 +33,10 @@ def value(self):
3233
"""
3334
return self._value
3435

35-
def __eq__(self, other):
36+
def __eq__(self, other: Any) -> bool:
3637
if isinstance(other, State):
3738
return self._value == other.value
3839
return False
3940

40-
def __repr__(self):
41+
def __repr__(self) -> str:
4142
return "State(" + str(self._value) + ")"

pyformlang/pda/symbol.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
""" A Symbol in a pushdown automaton """
22

3+
from typing import Any
34

4-
class Symbol:
5+
from .pda_object import PDAObject
6+
7+
8+
class Symbol(PDAObject):
59
""" A Symbol in a pushdown automaton
610
711
Parameters
@@ -11,14 +15,11 @@ class Symbol:
1115
1216
"""
1317

14-
def __init__(self, value):
15-
self._value = value
16-
17-
def __hash__(self):
18-
return hash(str(self._value))
18+
def __hash__(self) -> int:
19+
return super().__hash__()
1920

2021
@property
21-
def value(self):
22+
def value(self) -> Any:
2223
""" Returns the value of the symbol
2324
2425
Returns
@@ -28,10 +29,10 @@ def value(self):
2829
"""
2930
return self._value
3031

31-
def __eq__(self, other):
32+
def __eq__(self, other: Any) -> bool:
3233
if isinstance(other, Symbol):
3334
return self._value == other.value
3435
return False
3536

36-
def __repr__(self):
37+
def __repr__(self) -> str:
3738
return "Symbol(" + str(self._value) + ")"

pyformlang/pda/transition_function.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" A transition function in a pushdown automaton """
22

3-
from typing import List
3+
from typing import Dict, List, Set, Iterator, Tuple, Optional
44

55
from .stack_symbol import StackSymbol
66
from .state import State
@@ -10,13 +10,17 @@
1010
class TransitionFunction:
1111
""" A transition function in a pushdown automaton """
1212

13-
def __init__(self):
14-
self._transitions = {}
15-
self._iter_key = None
16-
self._current_key = None
17-
self._iter_inside = None
13+
def __init__(self) -> None:
14+
self._transitions: Dict[Tuple[State, Symbol, StackSymbol],
15+
Set[Tuple[State, List[StackSymbol]]]] = {}
16+
self._iter_key: Optional[Iterator[
17+
Tuple[State, Symbol, StackSymbol]]] = None
18+
self._current_key: Optional[
19+
Tuple[State, Symbol, StackSymbol]] = None
20+
self._iter_inside: Optional[Iterator[
21+
Tuple[State, List[StackSymbol]]]] = None
1822

19-
def get_number_transitions(self):
23+
def get_number_transitions(self) -> int:
2024
""" Gets the number of transitions
2125
2226
Returns
@@ -32,7 +36,7 @@ def add_transition(self,
3236
input_symbol: Symbol,
3337
stack_from: StackSymbol,
3438
s_to: State,
35-
stack_to: List[StackSymbol]):
39+
stack_to: List[StackSymbol]) -> None:
3640
""" Add a transition to the function
3741
3842
Parameters
@@ -49,7 +53,7 @@ def add_transition(self,
4953
The string of stack symbol which replace the stack_from
5054
"""
5155
temp_in = (s_from, input_symbol, stack_from)
52-
temp_out = (s_to, tuple(stack_to))
56+
temp_out = (s_to, stack_to.copy())
5357
if temp_in in self._transitions:
5458
self._transitions[temp_in].add(temp_out)
5559
else:
@@ -70,31 +74,35 @@ def copy(self) -> "TransitionFunction":
7074
temp_out[0], temp_out[1])
7175
return new_tf
7276

73-
def __iter__(self):
77+
def __iter__(self) -> Iterator[Tuple[Tuple[State, Symbol, StackSymbol],
78+
Tuple[State, List[StackSymbol]]]]:
7479
self._iter_key = iter(self._transitions.keys())
7580
self._current_key = None
7681
self._iter_inside = None
7782
return self
7883

79-
def __next__(self):
84+
def __next__(self) -> Tuple[Tuple[State, Symbol, StackSymbol],
85+
Tuple[State, List[StackSymbol]]]:
8086
if self._iter_inside is None:
81-
next_key = next(self._iter_key)
87+
next_key = next(self._iter_key) # type: ignore
8288
self._current_key = next_key
8389
self._iter_inside = iter(self._transitions[next_key])
8490
try:
8591
next_value = next(self._iter_inside)
86-
return self._current_key, next_value
92+
return self._current_key, next_value # type: ignore
8793
except StopIteration:
88-
next_key = next(self._iter_key)
94+
next_key = next(self._iter_key) # type: ignore
8995
self._current_key = next_key
9096
self._iter_inside = iter(self._transitions[next_key])
9197
return next(self)
9298

9399
def __call__(self, s_from: State,
94100
input_symbol: Symbol,
95-
stack_from: StackSymbol):
96-
return self._transitions.get((s_from, input_symbol, stack_from), {})
101+
stack_from: StackSymbol) \
102+
-> Set[Tuple[State, List[StackSymbol]]]:
103+
return self._transitions.get((s_from, input_symbol, stack_from), set())
97104

98-
def to_dict(self):
105+
def to_dict(self) -> Dict[Tuple[State, Symbol, StackSymbol],
106+
Set[Tuple[State, List[StackSymbol]]]]:
99107
"""Get the dictionary representation of the transitions"""
100108
return self._transitions

pyformlang/pda/utils.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
""" Useful functions for a PDA """
22

3+
from typing import Type, Dict, Any
4+
35
from .state import State
46
from .symbol import Symbol
57
from .stack_symbol import StackSymbol
@@ -11,26 +13,26 @@ class PDAObjectCreator:
1113
A Object in a PDA
1214
"""
1315

14-
def __init__(self):
15-
self._state_creator = {}
16-
self._symbol_creator = {}
17-
self._stack_symbol_creator = {}
16+
def __init__(self) -> None:
17+
self._state_creator: Dict[Any, State] = {}
18+
self._symbol_creator: Dict[Any, Symbol] = {}
19+
self._stack_symbol_creator: Dict[Any, StackSymbol] = {}
1820

19-
def to_state(self, given):
21+
def to_state(self, given: Any) -> State:
2022
""" Convert to a state """
2123
if isinstance(given, State):
2224
return _get_object_from_known(given, self._state_creator)
2325
return _get_object_from_raw(given, self._state_creator, State)
2426

25-
def to_symbol(self, given):
27+
def to_symbol(self, given: Any) -> Symbol:
2628
""" Convert to a symbol """
2729
if isinstance(given, Symbol):
2830
return _get_object_from_known(given, self._symbol_creator)
2931
if given == "epsilon":
3032
return Epsilon()
3133
return _get_object_from_raw(given, self._symbol_creator, Symbol)
3234

33-
def to_stack_symbol(self, given):
35+
def to_stack_symbol(self, given: Any) -> StackSymbol:
3436
""" Convert to a stack symbol """
3537
if isinstance(given, StackSymbol):
3638
return _get_object_from_known(given,
@@ -42,14 +44,17 @@ def to_stack_symbol(self, given):
4244
StackSymbol)
4345

4446

45-
def _get_object_from_known(given, obj_converter):
47+
def _get_object_from_known(given: Any,
48+
obj_converter: Dict[Any, Any]) -> Any:
4649
if given.value in obj_converter:
4750
return obj_converter[given.value]
4851
obj_converter[given.value] = given
4952
return given
5053

5154

52-
def _get_object_from_raw(given, obj_converter, to_type):
55+
def _get_object_from_raw(given: Any,
56+
obj_converter: Dict[Any, Any],
57+
to_type: Type) -> Any:
5358
if given in obj_converter:
5459
return obj_converter[given]
5560
temp = to_type(given)

0 commit comments

Comments
 (0)